Followers

Saturday, 29 September 2012

Overloading


Question 1

 
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D extends C {
  void m1(D d) {System.out.print("D");}
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D();
    d1.m1(a1); d1.m1(b1); d1.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: DDD
d.  Prints: ABCD
e.   Compile-time error
f.   Run-time error
g.  None of the above

Question 2

class A {}  class B extends A {}  class C extends B {}
class D {
  void m1(A a) {System.out.print("A");}
  void m1(B b) {System.out.print("B");}
  void m1(C c) {System.out.print("C");}
  public static void main(String[] args) {
    A c1 = new C(); B c2 = new C(); C c3 = new C(); D d1 = new D();
    d1.m1(c1); d1.m1(c2); d1.m1(c3);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 3

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A c1 = new C(); B c2 = new C(); C c3 = new C(); C c4 = new C();
    c4.m1(c1); c4.m1(c2); c4.m1(c3);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 4

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
What is the result of attempting to compile and run the program?
a.   Prints: A
b.  Prints: B
c.   Prints: C
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 5

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); A b1 = new B(); A c1 = new C(); C c4 = new C();
    a1.m1(c4); b1.m1(c4); c1.m1(c4);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 6

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new B(); C c1 = new C(); A c2 = new C();
    c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 7

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new A(); C c1 = new A(); C c2 = new C();
    c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 8

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new B(); C c1 = new C(); C c2 = new A();
    c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above



                            Answers

No.
Answer
Remark
1
Prints: ABC 
The method invocation expression d1.m1(a1) uses reference d1 of type D to invoke method m1. Since the reference d1 is of type D, the class D is searched for an applicable implementation of m1. The methods inherited from the superclasses, C, B and A, are included in the search. The argument, a1, is a variable declared with the type A; so method A.m1(A a) is invoked.  
2
Prints: ABC 
Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object. The method invocation expression d1.m1(c1) uses reference d1 of type D to invoke method m1 on an instance of type D. The argument, c1, is a reference of type A and the run-time type of the referenced object is C. The argument type is determined by the declared type of the reference variable c1--not the run-time type of the object referenced by c1. The declared type of c1 is type A; so the method m1(A a) is selected. The declared type of c2 is type B; so the method invocation expression d1.m1(c2) invokes method m1(B b). The declared type of c3 is type C; so the method invocation expression d1.m1(c3) invokes method m1(C c).  
3
Prints: ABC 
Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object. The method invocation expression c4.m1(c1) uses reference c4 of type C to invoke method m1 on an instance of type C. The argument, c1, is a reference of type A and the run-time type of the referenced object is C. The argument type is determined by the declared type of the reference variable c1--not the run-time type of the object referenced by c1. The declared type of c1 is type A; so the method A.m1(A a) is selected. The declared type of c2 is type B; so the method invocation expression c4.m1(c2) invokes method B.m1(B b). The declared type of c3 is type C; so the method invocation expression c4.m1(c3) invokes method C.m1(C c).  
4
Prints: A 
The reference c1 is of the superclass type, A; so it can be used to invoke only the method m1 declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c1. A method invocation conversion promotes the argument referenced by c2 from type C to type A, and the method declared in class A is executed. Class A declares only one method, m1. The single parameter is of type A. Class B inherits the method declared in class A and overloads the method name with a new method that has a single parameter of type B. Both methods sharing the overloaded name, m1, can be invoked using a reference of type B; however, a reference of type A can be used to invoke only the method declared in class A. Class C inherits the methods declared in classes A and B and overloads the method name with a new method that has a single parameter of type C. All three methods sharing the overloaded name, m1, can be invoked using a reference of type C; however, a reference of type B can be used to invoke only the method declared in class B and the method declared in the superclass A. The method invocation expression c1.m1(c2) uses reference c1 of type A to invoke method m1. Since the reference c1 is of type A, the search for an applicable implementation of m1 is limited to class A. The subclasses, B and C, will not be searched; so the overloading methods declared in the subclasses can not be invoked using a reference of the superclass type.  
5
Prints: AAA 
The declared type of the reference variables, a1, b1 and c1, is the superclass type, A; so the three reference variables can be used to invoke only the method m1(A a) that is declared in the superclass, A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using a reference variable of the superclass type, A. A method invocation conversion promotes the argument referenced by c4 from type C to type A, and the method declared in class A is executed.  
6
Prints: AAA 
The reference c2 is of the superclass type, A; so it can be used to invoke only the method, m1, declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c2.  
7
Compile-time error 
The declarations of b1 and c1 cause compile-time errors, because a reference of a subclass type can not refer to an instance of the superclass type.  
8
Compile-time error 
The declaration of c2 causes a compile-time error, because a reference of a subclass type can not refer to an instance of the superclass class.  




Inheritance


Question 1

Which of the following statements are true?
a.   A constructor can invoke another constructor of the same class using the alternate        constructor invocation, "this(argumentListopt);".
b.  A constructor can invoke itself using the alternate constructor invocation,          "this(argumentListopt);".
c.   The alternate constructor invocation, "this(argumentListopt);", can legally appear          anywhere in the constructor body.
d.  A constructor can invoke the constructor of the direct superclass using the superclass   constructor invocation, "super(argumentListopt);".
e.   The number of constructor invocations that may appear in any constructor body can     equal but not exceed the number of alternate constructors declared in the same class.
f.   A constructor is not permitted to throw an exception.

Question 2

Suppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?
a.   Static variables declared in this class or any superclass.
b.  Instance variables declared in this class or any superclass.
c.   Static methods declared in this class or any superclass.
d.  Instance methods declared in this class or any superclass.
e.   The keyword this.
f.   The keyword super.

Question 3

class A {void m1(String s1) {}}
class B extends A {
  void m1(String s1) {}  // 1
  void m1(boolean b) {}  // 2
  void m1(byte b) throws Exception {}  // 3
  String m1(short s) {return new String();} //4
  private void m1(char c) {} // 5
  protected void m1(int i) {} // 6
}
What is the result of attempting to compile the program?
a.   Compile-time error at line 1
b.  Compile-time error at line 2
c.   Compile-time error at line 3
d.  Compile-time error at line 4
e.   Compile-time error at line 5
f.   Compile-time error at line 6
g.  None of the above

Question 4

class A {void m1() {System.out.print("A.m1");}}
class B extends A {
  void m1() {System.out.print("B.m1");}
  static void m1(String s) {System.out.print(s+",");}
}
class C {
  public static void main (String[] args) {B.m1("main"); new B().m1();}
}
What is the result of attempting to compile and run the program?
a.   Prints: main,B.m1
b.  Compile-time error
c.   Run-time error
d.  None of the above

Question 5

Which of the following are true statements?
a.   The relationship between a class and its superclass is an example of a "has-a"     relationship.
b.  The relationship between a class and its superclass is an example of an "is-a"      relationship.
c.   The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship.
d.  The relationship between a class and an object referenced by a field within the class is an example of an "is-a" relationship.

Question 6

class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
  String s1 = "B.s1";
  public static void main(String args[]) {
    B x = new B(); A y = (A)x;
    System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}
What is the result of attempting to compile and run the program?
a.   Prints: B.s1 A.s2 B.s1 A.s2
b.  Prints: B.s1 A.s2 A.s1 A.s2
c.   Prints: A.s1 A.s2 B.s1 A.s2
d.  Prints: A.s1 A.s2 A.s1 A.s2
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 7

class C {
  void printS1() {System.out.print("C.printS1 ");}
  static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
  void printS1(){System.out.print("D.printS1 ");}
  void printS2() {System.out.print("D.printS2 ");}
  public static void main (String args[]) {
    C c = new D(); c.printS1(); c.printS2();
}}
What is the result of attempting to compile and run the program?
a.   Prints: C.printS1 C.printS2
b.  Prints: C.printS1 D.printS2
c.   Prints: D.printS1 C.printS2
d.  Prints: D.printS1 D.printS2
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 8

 
class E {
  void printS1(){System.out.print("E.printS1 ");}
  static void printS2() {System.out.print("E.printS2");}
}
class F extends E {
  void printS1(){System.out.print("F.printS1 ");}
  static void printS2() {System.out.print("F.printS2");}
  public static void main (String args[]) {
    E x = new F(); x.printS1(); x.printS2();
}}
What is the result of attempting to compile and run the program?
a.   Prints: E.printS1 E.printS2
b.  Prints: E.printS1 F.printS2
c.   Prints: F.printS1 E.printS2
d.  Prints: F.printS1 F.printS2
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 9

class P {
  static void printS1(){System.out.print("P.printS1 ");}
  void printS2() {System.out.print("P.printS2 ");}
  void printS1S2(){printS1();printS2();}
}
class Q extends P {
  static void printS1(){System.out.print("Q.printS1 ");}
  void printS2(){System.out.print("Q.printS2 ");}
  public static void main(String[] args) {
    new Q().printS1S2();
}}
What is the result of attempting to compile and run the program?
a.   Prints: P.printS1 P.printS2
b.  Prints: P.printS1 Q.printS2
c.   Prints: Q.printS1 P.printS2
d.  Prints: Q.printS1 Q.printS2
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 10

class R {
  private void printS1(){System.out.print("R.printS1 ");}
  protected void printS2() {System.out.print("R.printS2 ");}
  protected void printS1S2(){printS1();printS2();}
}
class S extends R {
  private void printS1(){System.out.print("S.printS1 ");}
  protected void printS2(){System.out.print("S.printS2 ");}
  public static void main(String[] args) {
    new S().printS1S2();
}}
What is the result of attempting to compile and run the program?
a.   Prints: R.printS1 R.printS2
b.  Prints: R.printS1 S.printS2
c.   Prints: S.printS1 R.printS2
d.  Prints: S.printS1 S.printS2
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 11

class T {
  private int i1, i2;
  void printI1I2() {System.out.print("T, i1="+i1+", i2="+i2);}
  T(int i1, int i2) {this.i1=i1; this.i2=i2;}
}
class U extends T {
  private int i1, i2;
  void printI1I2() {System.out.print("U, i1="+i1+", i2="+i2);}
  U(int i1, int i2) {this.i1=i1; this.i2=i2;}
  public static void main(String[] args) {
    T t = new U(1,2); t.printI1I2();
}}
What is the result of attempting to compile and run the program?
a.   Prints: U, i1=1, i2=2
b.  Prints: T, i1=1, i2=2
c.   Prints: U, i1=null, i2=null
d.  Prints: T, i1=null, i2=null
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 12

 
interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
  String s1 = "C";
  void printIt() {
    System.out.print(((A)this).s1 + ((B)this).s1 +
                     ((C)this).s1 + ((I)this).s1);
  }
  public static void main (String[] args) {new C().printIt();}
}
What is the result of attempting to compile and run the program?
a.   Prints: ABCI
b.  Run-time error
c.   Compile-time error
d.  None of the above

Question 13

 
abstract class D {String s1 = "D"; String getS1() {return s1;}}
class E extends D {String s1 = "E"; String getS1() {return s1;}}
class F {
  public static void main (String[] s) {
    D x = new E(); System.out.print(x.s1 + x.getS1());
}}
What is the result of attempting to compile and run the program?
a.   Prints: DD
b.  Prints: DE
c.   Prints: ED
d.  Prints: EE
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 14

class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    C c = new C(); c.m(); B b = c; b.m(); A a = b; a.m();
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CBA
d.  Prints: CCC
e.   Compile-time error
f.   Run-time error
g.  None of the above

Question 15

 
class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
  static void m1(A x) {System.out.print(x.s1);}
  static void m1(B x) {System.out.print(x.s1);}
  static void m1(C x) {System.out.print(x.s1);}
  public static void main(String[] args) {
    A a; B b; C c; a = b = c = new C(); m1(a); m1(b); m1(c);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CBA
d.  Prints: CCC
e.   Compile-time error
f.   Run-time error
g.  None of the above

Question 16

class Leg{}
class Fur{}
abstract class Pet {
  public abstract void eat();
  public abstract void sleep();
}
class Dog extends Pet {
  Leg leftFront = new Leg(), rightFront = new Leg();
  Leg leftRear = new Leg(), rightRear = new Leg();
  Fur fur = new Fur();
  public Fur shed() {return fur;}
  public void eat() {}
  public void sleep() {}
}
class Cat extends Dog {
  public void ignoreOwner() {}
  public void climbTree() {}
}
Which of the following statements is not a true statement?
a.   A Cat object inherits an instance of Fur and four instances of Leg from the Dog           superclass.
b.  A Cat object is able to sleep and eat.
c.   A Cat object is able to climb a tree.
d.  The relationship between Dog and Pet is an example of an appropriate use of    inheritance.
e.   The relationship between Cat and Dog is an example of an appropriate use of    inheritance.
f.   None of the above.

Question 17

 
class A {
  A() {System.out.print("CA ");}
  static {System.out.print("SA ");}
}
class B extends A {
  B() {System.out.print("CB ");}
  static {System.out.print("SB ");}
  public static void main (String[] args) {B b = new B();}
}
What is the result of attempting to compile and run the above program?
a.   Prints: SA CA SB CB
b.  Prints: SA SB CA CB
c.   Prints: SB SA CA CB
d.  Prints: SB CB SA CA
e.   Runtime Exception
f.   Compiler Error
g.  None of the above

Question 18

 
class A {String s1="A";}
class B extends A {String s1="B";}
class C extends B {String s1="C";}
class D extends C {
  String s1="D";
  void m1() {
    System.out.print(this.s1  + ",");      // 1
    System.out.print(((C)this).s1  + ","); // 2
    System.out.print(((B)this).s1  + ","); // 3
    System.out.print(((A)this).s1);        // 4
  }
  public static void main (String[] args) {
    new D().m1(); // 5
}}
What is the result of attempting to compile and run the program?
a.   Prints: D,D,D,D
b.  Prints: D,C,B,A
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Compile-time error at 3.
f.   Compile-time error at 4.
g.  Compile-time error at 5.
h.  Run-time error
i.   None of the above

Question 19

class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
  String s1="A";
  class B extends SuperB {  // 1
    String s1="B";
    void m1() {
      System.out.print(this.s1 + ",");   // 2
      System.out.print(super.s1 + ",");  // 3
      System.out.print(A.this.s1 + ","); // 4
      System.out.print(A.super.s1);      // 5
    }
  }
  public static void main (String[] args) {
    new A().new B().m1();  // 6
}}
What is the result of attempting to compile and run the program?
a.   Prints: B,SuperB,B,SuperB
b.  Prints: B,SuperB,A,SuperA
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Compile-time error at 3.
f.   Compile-time error at 4.
g.  Compile-time error at 5.
h.  Compile-time error at 6.
i.   Run-time error
j.   None of the above

Question 20

 
class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
class D extends C {
  void m1() {System.out.print("D");}
  void m2() {
    m1();
    ((C)this).m1(); // 1
    ((B)this).m1(); // 2
    ((A)this).m1(); // 3
  }
  public static void main (String[] args) {
    new D().m2(); // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: DCBA
b.  Prints: DDDD
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Compile-time error at 3.
f.   Compile-time error at 4.
g.  Run-time error
h.  None of the above

Question 21

class A {public void m1() {System.out.print("A1");}}
class B extends A {
  public void m1() {System.out.print("B1");}
  public void m2() {System.out.print("B2");}
}
class C {
  public static void main(String[] args) {
    A a1 = new B();
    a1.m1();      // 1
    a1.m2();      // 2
   ((B)a1).m1();  // 3
   ((B)a1).m2();  // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: A1B2B1B2
b.  Prints: B1B2B1B2
c.   Compile-time error at 1
d.  Compile-time error at 2
e.   Compile-time error at 3
f.   Compile-time error at 4
g.  Run-time error
h.  None of the above


                                         Answers


No.
Answer
Remark
1
a  d 
A constructor can invoke another constructor of the same class using the alternate constructor invocation, "this(argumentListopt);".  A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation, "super(argumentListopt);"
If an alternate constructor invocation appears in the body of the constructor, then it must be the first statement. The same is true for a superclass constructor invocation. A compile-time error is generated if a constructor attempts to invoke itself either directly or indirectly.  
2
b  d  e  f 
Instance variables declared in this class or any superclass.  Instance methods declared in this class or any superclass.  The keyword this.  The keyword super
If the superclass constructor invocation, "super(argumentListopt);", appears explicitly or implicitly, then it must be the first statement in the body of the constructor. Until the superclass constructor invocation runs to completion, no other statements are processed within the body of the constructor. The same is true of the constructors of any superclass. (Note: The primordial class, Object, does not have a superclass, so the constructors do not include a superclass constructor invocation statement.) Suppose class B is a subclass of A. The process of creating and initializing an instance of B includes the creation and initialization of an instance of the superclass A and an instance of the superclass Object. The superclass constructor invocation statement appearing in a constructor of B is invoked before the completion of the constructors of the superclasses A and Object. A superclass constructor invocation statement appearing in B can not refer to the non-static members of the superclasses, because the process of initializing those non-static superclass members is not complete when the superclass constructor invocation occurs in B. The same is true of the non-static members of B.  
3
None of the above 
If a superclass method is not private and is accessible to code in a subclass, then any subclass method that has the same signature as the superclass method must also have the same return type. In other words, if a superclass method is overridden or hidden in a subclass, then the overriding or hiding subclass method must have the same return type as the superclass method. No such restriction applies to method overloading. If two methods share an overloaded method name but not the same parameter list, then the two methods need not have the same return type. Class A declares one method: The name is m1 and the single parameter is of type String. Class B extends A and declares six methods that overload the name m1. The method, B.m1(String s1), overrides the superclass method, A.m1(String s1). The overriding subclass method must have the same return type as the superclass method, but the methods that overload the name m1 are free to have different return types. An overriding subclass method is not permitted to throw any checked exception that is not listed or is not a subclass of any of those listed in the throws clause of the superclass method. No such restriction applies to method overloading. If two methods share an overloaded method name but not the same parameter list, then the two methods are free to have differing throws clauses.  
4
Prints: main,B.m1 
Suppose a superclass method is not private and is accessible to code in a subclass. If the superclass method is declared static, then any subclass method sharing the same signature must also be declared static. Similarly, if the superclass method is not declared static, then any subclass method sharing the same signature must not be declared static. The rules governing method overloading are different. If a superclass method is declared static, then any subclass method that overloads the superclass method is free to be declared static or non-static. Similarly, if a method is declared non-static, then any overloading method is free to be declared static or non-static. Method B.m1() shares the same signature as the non-static superclass method A.m1(), so B.m1() must also be non-static. The method B.m1(String s) overloads the method name m1, but does not share the same signature with any superclass method; therefore, B.m1(String s) can be declared static even though the other methods of the same name are non-static.  
5
b  c 
The relationship between a class and its superclass is an example of an "is-a" relationship.  The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship. 
Inheritance is an example of an "is-a" relationship, because the subclass "is-a" specialized type of the superclass. The relationship between a class and an object referenced by a field declared within the class is an example of a "has-a" relationship, because the class "has-a" object.  
6
Prints: B.s1 A.s2 A.s1 A.s2 
The variables of a subclass can hide the variables of a superclass or interface. The variable that is accessed is determined at compile-time based on the type of the reference--not the run-time type of the object. The two references x and y refer to the same instance of type B. The name x.s1 uses a reference of type B; so it refers to the variable s1 declared in class B. The name y.s1 uses a reference of type A; so it refers to the variable s1 declared in class A.  
7
Compile-time error 
Suppose a superclass method is not private and is accessible to code in a subclass. If the superclass method is declared static, then any subclass method sharing the same signature must also be declared static. Similarly, if the superclass method is declared non-static, then any subclass method sharing the same signature must also be declared non-static. The attempted declaration of the non-static method D.printS2 generates a compile-time error; because the superclass method, C.printS2, is static.  
8
Prints: F.printS1 E.printS2 
A static method is selected based on the compile-time type of the reference--not the run-time type of the object. A non-static method is selected based on the run-time type of the object--not the compile-time type of the reference. Both method invocation expressions, x.printS1() and x.printS2(), use a reference of the superclass type, E, but the object is of the subclass type, F. The first of the two expressions invokes an instance method on an object of the subclass type; so the overriding subclass method is selected. The second invokes a static method using a reference of the superclass type; so the superclass method is selected.  
9
Prints: P.printS1 Q.printS2 
Suppose a method m1 is invoked using the method invocation expression m1(). If m1 is a static member of the class where the invocation expression occurs, then that is the implementation of the method that is invoked at run-time regardless of the run-time type of the object. If m1 is non-static, then the selected implementation is determined at run-time based on the run-time type of the object. The program invokes method printS1S2 on an instance of class Q. The body of method printS1S2 contains two method invocation expressions, printS1() and printS2(). Since method printS1 is static, the implementation declared in class P is invoked. Since printS2 is non-static and the run-time type of the object is Q, the invoked method is the one declared in class Q.  
10
Prints: R.printS1 S.printS2 
A private method of a superclass is not inherited by a subclass. Even if a subclass method has the same signature as a superclass method, the subclass method does not override the superclass method. Suppose a non-static method m1 is invoked using the method invocation expression m1(). If m1 is a private member of the class T where the invocation expression occurs, then the implementation in class T is selected at run-time regardless of the run-time type of the object. If the non-static method m1 is not private, then the selected implementation is determined at run-time based on the run-time type of the object. The program invokes the non-static method printS1S2 on an instance of class S, so the run-time type is S. The body of method R.printS1S2 contains two method invocation expressions, printS1() and printS2(). Since class R contains a private implementation of the instance method printS1, it is the implementation that is selected regardless of the run-time type of the object. Since printS2 is not private and not static, the selected implementation of printS2 depends on the run-time type of the object. The method printS1S2 is invoked on an instance of class S; so the run-time type of the object is S, and the implementation of printS2 declared in class S is selected.  
11
Compile-time error 
The two-parameter constructor of U does not explicitly invoke the two-parameter constructor of T; therefore, the constructor of U will try to invoke a no-parameter constructor of T, but none exists.  
12
Prints: ABCI 
Suppose that a class extends a superclass, X, or implements an interface, X. The field access expression ((X)this).hiddenField is used to access the hidden field, hiddenField, that is accessible within the superclass or interface, X.  
13
Prints: DE 
At run-time, the actual field that is accessed depends on the compile-time type of the reference--not the run-time type of the object. The compile-time type of the reference x in the name x.s1 is D; so the selected field is the one declared in class D. A non-static method is selected based on the run-time type of the object--not the compile-time type of the reference. The same reference variable x is used in the method invocation expression x.getS1(), and the compile-time type is still D. At run-time, the actual type of the object is E; so the selected method is the one declared in class E.  
14
Prints: CBA 
Class C extends B, and B extends A. The static method C.m hides method B.m, and B.m hides A.m. In the method invocation expression c.m(), the compile-time type of the reference c is C. A static method is invoked based on the compile-time type of the reference; so the method invocation expression c.m() invokes the method m declared in class C. The compile-time type of the reference b is B; so the method invocation expression b.m() invokes the method m declared in class B. The compile-time type of the reference a is A; so the method invocation expression a.m() invokes the method m declared in class A.  
15
Prints: ABC 
In all three cases, the object passed to method m1 is an instance of class C; however, the type of the reference is different for each method. Since fields are accessed based on the type of the reference, the value printed by each method is different even though the same instance is used for each method invocation.  
16
The relationship between Cat and Dog is an example of an appropriate use of inheritance. 
An appropriate inheritance relationship includes a subclass that "is-a" special kind of the superclass. The relationship between the Dog subclass and the Pet superclass is an example of an appropriate inheritance relationship, because a Dog "is-a" Pet. The relationship between the Cat subclass and the Dog superclass is not an example of an appropriate use of inheritance, because a Cat is not a special kind of a Dog. The goal of the OO paradigm is to develop software models that are accurate and reusable. If the software model is not accurate, then it probably is not reusable and the goals of the OO paradigm are not achieved. Code reuse and maintenance becomes increasingly difficult when inheritance is used to model inappropriate relationships. For example, suppose that somebody implements a herdSheep method in the Dog class. The Cat subclass would inherit the method and suddenly each instance of Cat would acquire the unwanted capability to make an attempt to herd sheep. It is difficult to imagine that a Cat would perform well in that role, so additional maintenance would be required to resolve the problem.  
17
Prints: SA SB CA CB 
The static initializer of the super class runs before the static initializer of the subclass. The body of the superclass constructor runs to completion before the body of the subclass constructor runs to completion.  
18
Prints: D,C,B,A 
A field of a superclass can be inherited by a subclass if the superclass field is not private and not hidden by a field declaration in the subclass and is accessible to code in the subclass. The field D.s1 hides C.s1, and C.s1 hides B.s1, and B.s1 hides A.s1. The keyword this serves as a reference to the object on which a method has been invoked. In the field access expression this.s1 appearing on line 1, the keyword this denotes a reference to the object of type D on which method m1 has been invoked. In the field access expression ((C)this).s1 appearing on line 2, the reference denoted by the keyword this is cast from type D to type C. The field that is accessed at run-time depends on the compile-time type of the reference; so the field access expression ((C)this).s1 refers the the variable s1 declared in class C.  
19
Prints: B,SuperB,A,SuperA 
The expression A.this.s1 is an example of a qualified this expression. It accesses the variable s1 declared in class A. The expression A.super.s1 is equivalent to ((SuperA)A.this).s1. It accesses the variable s1 declared in class SuperA.  
20
Prints: DDDD 
The instance method that is invoked depends on the run-time type of the object--not the compile-time type of the reference. In each case, the method m1 is invoked on an object of type D; so the implementation of m1 in type D is selected each time.  
21
Compile-time error at 2 
The method invocation expression a1.m2() generates a compile-time error, because the named method, m2, is declared in class B, but the reference is of the superclass type, A. The reference a1 is of type A; so a1 is able to access only those methods that are declared in class A and subclass methods that override those of class A. Only one method, m1, is declared in A; so a reference of type A can be used to invoke A.m1 or an overriding implementation of m1 that is declared in a subclass of A. Class B extends A and overrides method m1. A reference of type A can be used to invoke method m1 on an instance of type B. Class B declares an additional method, m2, that does not override a method of class A; so a reference of type A can not invoke B.m2.  


Sunday, 16 September 2012

conditional operators


Question 1

class EBH201 {
  public static void main (String[] args) {
    int a = 1 || 2 ^ 3 && 5;
    int b = ((1 || 2) ^ 3) && 5;
    int c = 1 || (2 ^ (3 && 5));
    System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0,0
b.  Prints: 0,0,3
c.   Prints: 0,3,0
d.  Prints: 0,3,3
e.   Prints: 3,0,0
f.   Prints: 3,0,3
g.  Prints: 3,3,0
h.  Prints: 3,3,3
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 2

class EBH202 {
  static boolean a, b, c;
  public static void main (String[] args) {
    boolean x = (a = true) || (b = true) && (c = true);
    System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 3

class EBH203 {
  static boolean a, b, c;
  public static void main (String[] args) {
    boolean x = a || (b = true) && (c = true);
    System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 4

class EBH204 {
  static boolean m1(String s, boolean b) {
    System.out.print(s + (b ? "T" : "F"));
    return b;
  }
  public static void main(String[] args) {
    m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}
What is the result of attempting to compile and run the program?
a.   Prints: ATBFCT
b.  Prints: ATBFCTDF
c.   Prints: BFCTAT
d.  Prints: BTCTDFAT
e.   Run-time error
f.   Compile-time error
g.  None of the above



Answers



No.
Answer
Remark
1
Compile-time error 
Both operands of the conditional and operator and the conditional or operator must be of type boolean.  
2
Prints: true,false,false 
The conditional and expression is not evaluated, because the left hand operand of the conditional or expression is true. The original expression is as follows: x = (a = true) || (b = true) && (c = true). The left hand operand of the conditional or expression is the result of the assignment expression, (a = true). Since the left hand operand of the conditional or expression is true, the right hand operand will not be evaluated. In this case, the right hand operand is the conditional and expression. Consequently, neither operand of the conditional and expression is evaluated and the variables, b and c, maintain their default values of false.  
3
Prints: false,true,true 
The right hand operand of the conditional or operator is evaluated only if the left hand operand is false. The right hand operand of the conditional and operator is only evaluated if the left hand operand is true. In this case, the left hand operand of the conditional or operator is false, so the right hand operand must also be evaluated. The left hand operand of the conditional and operator is the result of the conditional or expression, true, so the right hand operand is evaluated.  
4
Prints: BFCTAT 
The right operand of the conditional or operator is evaluated only if the left hand operand is false. In this case, the left operand of the first conditional or operator is false so the right hand operand is evaluated. No further evaluation of the expression is necessary so the right hand operand of the second conditional or operator is not evaluated.  




Operators

Question 1

class EBH019 {
  public static void main (String args[]) {
    int i1 = 0xffffffff, i2 = i1 << 1;
    int i3 = i1 >> 1, i4 = i1 >>> 1;
    System.out.print(Integer.toHexString(i2) + ",");
    System.out.print(Integer.toHexString(i3) + ",");
    System.out.print(Integer.toHexString(i4));
}}
What is the result of attempting to compile and run the program?
a.   Prints: ffffffff,ffffffff,ffffffff
b.  Prints: ffffffff,ffffffff,7fffffff
c.   Prints: ffffffff,7fffffff,ffffffff
d.  Prints: ffffffff,7ffffffe,7ffffffe
e.   Prints: fffffffe,ffffffff,ffffffff
f.   Prints: fffffffe,ffffffff,7fffffff
g.  Prints: fffffffe,7fffffff,ffffffff
h.  Prints: fffffffe,7fffffff,7fffffff
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 2

class EBH011 {
  public static void main (String[] args) {
    float a = Float.POSITIVE_INFINITY;
    double b = Double.POSITIVE_INFINITY;
    double c = Double.NaN;
    System.out.print((a == b)+","+(c == c)+","+(c != c));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 3

class EBH012 {
  public static void main (String[] args) {
    byte x = 3, y = 5;
    System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false
b.  Prints: false,true
c.   Prints: true,false
d.  Prints: true,true
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 4

class EBH013 {
  public static void main (String[] args) {
    byte x = 3, y = 5;
    System.out.print((~x == -x - 1)+","+(~y == -y - 1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false
b.  Prints: false,true
c.   Prints: true,false
d.  Prints: true,true
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 5

class EBH014 {
  public static void main (String[] args) {
    byte x = 3, y = 5;
    System.out.print((y % x) + ",");
    System.out.print(y == ((y/x)*x + (y%x)));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1,true
b.  Prints: 2,true
c.   Prints: 1,false
d.  Prints: 2,false
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 6

class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
  public static void main (String[] args) {
    Color color1 = new Red(); Red color2 = new Red();
    boolean b1 = color1 instanceof Color;
    boolean b2 = color1 instanceof Blue;
    boolean b3 = color2 instanceof Blue;
    System.out.print(b1+","+b2+","+b3);
}}
What is the result of attempting to compile and run the program?
a.   false,false,false
b.  false,false,true
c.   false,true,false
d.  false,true,true
e.   true,false,false
f.   true,false,true
g.  true,true,false
h.  true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 7

class EBH020 {
  public static void main (String[] args) {
    int a = 1 | 2 ^ 3 & 5;
    int b = ((1 | 2) ^ 3) & 5;
    int c = 1 | (2 ^ (3 & 5));
    System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0,0
b.  Prints: 0,0,3
c.   Prints: 0,3,0
d.  Prints: 0,3,3
e.   Prints: 3,0,0
f.   Prints: 3,0,3
g.  Prints: 3,3,0
h.  Prints: 3,3,3
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 8

class EBH025 {
  public static void main (String args[]) {
    int i1 = 0xffffffff, i2 = i1 << 33;
    int i3 = i1 << (33 & 0x1f);
    System.out.print(Integer.toHexString(i2) + ",");
    System.out.print(Integer.toHexString(i3));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0
b.  Prints: 0,fffffffe
c.   Prints: 0,ffffffff
d.  Prints: ffffffff,ffffffff
e.   Prints: ffffffff,fffffffe
f.   Prints: fffffffe,ffffffff
g.  Prints: fffffffe,fffffffe
h.  Run-time error
i.   Compile-time error
j.   None of the above

Question 9

class EBH001 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(1) - m(2) + m(3) * m(4));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1, 2, 3, 4, 8,
b.  Prints: 1, 2, 3, 4, 11,
c.   Prints: 3, 4, 1, 2, 11,
d.  Run-time error
e.   Compile-time error
f.   None of the above

Question 10

class EBH002 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(1) + m(2) % m(3) * m(4));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1, 2, 3, 4, 0,
b.  Prints: 1, 2, 3, 4, 3,
c.   Prints: 1, 2, 3, 4, 9,
d.  Prints: 1, 2, 3, 4, 12,
e.   Prints: 2, 3, 4, 1, 9,
f.   Prints: 2, 3, 4, 1, 3,
g.  Run-time error
h.  Compile-time error
i.   None of the above

Question 11

class EBH005 {
  public static void main (String[] s) {
    byte b = 127; b <<= 2;System.out.println(b);
}}
What is the result of attempting to compile and run the program?
a.   Prints: -4
b.  Prints: -3
c.   Prints: -2
d.  Prints: 0
e.   Prints: 1
f.   Prints: 127
g.  Prints: 508
h.  Run-time error
i.   Compile-time error
j.   None of the above

Question 12

class EBH007{
  public static void main (String[] s) {
    byte b = 5; System.out.println(b<<33);
}}
What is the result of attempting to compile and run the program?
a.   Prints: -1
b.  Prints: 0
c.   Prints: 1
d.  Prints: 5
e.   Prints: 10
f.   Run-time error
g.  Compile-time error
h.  None of the above

Question 13

class EBH015 {
  public static void main (String[] args) {
    System.out.print((new Object() instanceof Object)+",");
    System.out.print((new Object() instanceof String)+",");
    System.out.print((new String() instanceof Object));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 14

class EBH023 {
  static String m1(boolean b){return b?"T":"F";}
  public static void main(String [] args) {
    boolean b1 = false?false:true?false:true?false:true;
    boolean b2 = false?false:(true?false:(true?false:true));
    boolean b3 = ((false?false:true)?false:true)?false:true;
    System.out.println(m1(b1) + m1(b2) + m1(b3));
}}
What is the result of attempting to compile and run the program?
a.   Prints: FFF
b.  Prints: FFT
c.   Prints: FTF
d.  Prints: FTT
e.   Prints: TFF
f.   Prints: TFT
g.  Prints: TTF
h.  Prints: TTT
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 15

class EBH024 {
  public static void main(String[] args) {
    int i1 = 15;
    String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
    String b2 = (i1>30)?"Red":((i1>20)?"Green":((i1>10)?"Blue":"Violet"));
    System.out.println(b1 + "," + b2);
 
}}
What is the result of attempting to compile and run the program?
a.   Prints: Red,Red
b.  Prints: Green,Green
c.   Prints: Blue,Blue
d.  Prints: Violet,Violet
e.   Prints: Blue,Violet
f.   Prints: Violet,Blue
g.  Prints: Blue,Green
h.  Prints: Green,Blue
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 16

class EBH003 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}
What is the result of attempting to compile and run the program?
a.    Prints: -2, 3, 0, 3, 0, 6,
b.   Prints: -2, 3, 0, 2, 1, 4,
c.    Prints: -2, 3, 0, 2, 2, 5,
d.   Prints: -2, 3, 0, 3, 2, 6,
e.    Prints: -1, 3, 0, 3, 2, 7,
f.    Prints: -2, 0, 3, 3, 0, 6,
g.   Prints: -1, 0, 3, 2, 1, 4,
h.   Prints: -2, 0, 3, 2, 2, 5,
i.    Prints: -2, 0, 3, 3, 2, 6,
j.    Prints: -1, 0, 3, 3, 2, 7,
k.   Run-time error
l.    Compile-time error

m.  None of the above


Question 17

class EBH021 {
  public static void main(String[] args) {
    System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0
b.  Prints: -1,4
c.   Prints: 0x1f,8
d.  Prints: 31,16
e.   Run-time error
f.   Compile-time error
g.  None of the above




Answers



No.
Answer
Remark
1
Prints: fffffffe,ffffffff,7fffffff 
If the left-hand operand of a shift operator, <<, >> and >>>, is of type int, then the shift distance is always within the range of 0 to 31, inclusive; and is specified by the least significant 5 bits of the right hand operand. Similarly, if the left-hand operand of a shift operator is of type long, then the shift distance is always within the range of 0 to 63, inclusive; and is specified by the least significant 6 bits of the right hand operand. The left shift operator, <<, shifts each bit of the left operand to the left a distance specified by the shift distance. A number of bits that is equal to the shift distance are shifted out of the left-most bit position and are lost. A number of bits that is equal to the shift distance are shifted in at the right. The signed right shift operator, >>, shifts each bit of the left operand to the right a distance specified by the shift distance. A number of bits that is equal to the shift distance are shifted out of the right-most bit position and are lost. A number of bits that is equal to the shift distance are shifted in at the left. The value of each bit that is shifted in at the left is equal to the value of the sign bit. The signed right shift operator maintains the sign of the left operand. The unsigned right shift operator, >>>, is similar to the signed right shift operator except for the fact that each bit shifted in at the left is zero.  
2
Prints: true,false,true 
The positive infinity of type float is promoted to the positive infinity of type double. NaN is not equal to anything including itself.  
3
Prints: true,true 
The sign of an integral numeric type is changed by inverting all of the bits and by adding one.  
4
Prints: true,true 
The bitwise complement operator produces the same result as changing the sign and subtracting one. Please note that the operand must be an integral type. The bitwise complement operator can not be applied to a floating-point value.  
5
Prints: 2,true 
Suppose the left operand were divided by the right operand. The remainder operator returns the remainder of the division operation. For integral types, the identity, (y == ((y/x)*x+(y%x))), is always true.  
6
Compile-time error 
The type of the reference color2 is Red. Since Red is not a subclass or a superclass of Blue, the expression color2 instanceof Blue is rejected at compile-time. Please note: The expression, x instanceof T, produces a compile-time error whenever the cast expression (T)x produces a compile-time error. If the program had been able to compile and run, the expression color1 instanceof Color would evaluate to true at run-time. The reference color1 refers to an instance of type Red. Since Red is a subclass of Color, the expression color1 instanceof Color would evaluate to true at run-time. The expression, color1 instanceof Blue would evaluate to false at run-time. The reference, color1, is of type Color. Since Color is a superclass of Blue, the expression, color1 instanceof Blue, is accepted at compile-time. The type of the object instance referenced by color1 is Red. Since Red is not Blue or a subclass of Blue, the expression, color1 instanceof Blue, would be false at run-time.  
7
Prints: 3,0,3 
Java evaluates operands from left to right while respecting operator precedence. The order of operator precedence starting with the lowest is as follows: |, ^, &. Although complete memorization of the operator precedence chart is not necessary, it is a good idea to memorize the three levels that appear in this question.  
8
Prints: fffffffe,fffffffe 
For each of the three shift operators, <<, >> and >>>, the shift distance is specified by the right hand operand. If the left operand is of type int, then the shift distance is always within the range 0 to 31, inclusive; and the following expression is always true: (int1 << shift) == (int1 << (shift & 0x1f)). The hexadecimal representation of decimal 31 is 0x1f and the binary representation is 0001 1111. The hexadecimal representation of decimal 33 is 0x21 and the binary representation is 0010 0001. The expression i1 << (33 & 0x1f) is equivalent to (0xffffffff << (0x21 & 0x1f)). Evaluation of the right hand operand of the shift operator produces (0xffffffff << 1). The final result is 0xfffffffe. Similarly, if the left operand is of type long, then the shift distance is always within the range 0 to 63, inclusive; and the following expression is always true: (long1 << shift) == (long1 << (shift & 0x3f)).  
9
Prints: 1, 2, 3, 4, 11, 
The expression can be simplified as follows: j = 1 - 2 + 3 * 4 = 11. The original expression is as follows: m(m(1) - m(2) + m(3) * m(4)). Simplification step one. Evaluate each operand from left to right: m(1 - 2 + 3 * 4). Step two. Add parentheses to indicate operator precedence: m(1 - 2 + (3 * 4)). Step three. Evaluate the inner-most parentheses: m(1 - 2 + 12). Step four: Work through the expression from left to right. j = 11.  
10
Prints: 1, 2, 3, 4, 9, 
The expression can be simplified as follows: 1 + 2 % 3 * 4 = 9. The original expression is as follows: m(m(1) + m(2) % m(3) * m(4)). Simplification step one. Evaluate each operand from left to right: m(1 + 2 % 3 * 4). Step two. Add parentheses to indicate operator precedence and associativity: m(1 + ((2 % 3) * 4). Step three. Evaluate the inner-most parentheses: m(1 + (2 * 4)). Step four. Evaluate the inner-most parentheses: m(1 + 8). The result is 9.  
11
Prints: -4 
If the left-hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all four bytes are shifted. When a variable of type int with a value of 127 is shifted two bits to the left, the result is 508. The compound assignment operator includes an implicit cast to the type of the left-hand operand. The expression, E1 op= E2, is equivalent to E1=(T)((E1) op (E2)), where T is the type of the left hand operand. Therefore, when 508 is cast to an eight bit byte, the three most significant bytes (24 bits) are discarded leaving only the least significant byte (8 bits). The result is the binary value, 11111100, which is the two's complement representation of negative four.  
12
Prints: 10 
If the left-hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all four bytes are shifted. If the promoted type of the left-hand operand is of type int, then the shift distance is always within the range of 0 to 31, inclusive; and is specified by the least significant 5 bits of the right-hand operand. In this case, the shift distance is 33, and the five least significant bits are 00001; so the shift distance is one bit. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are used.  
13
Prints: true,false,true 
The left operand of the instanceof operator must be null or a reference to an instance of an Object or a subclass of Object. The right operand of the instanceof operator must be a class type, interface type or array type. If the left operand is a reference to an instance of the type specified by the right operand or if the left operand is a reference to an instance of a subclass of the type specified by the right operand, then instanceof returns true.  
14
Prints: FFT 
The expression used to assign variable b1 is equivalent to the expression used to assign variable b2. The results demonstrate that the conditional operator (?:) groups from right-to-left.  
15
Prints: Blue,Blue 
The expression used to assign variable b1 is equivalent to the expression used to assign variable b2. The results demonstrate that the conditional operator (?:) groups from right-to-left.  
16
Prints: -2, 3, 0, 2, 2, 5, 
The expression can be simplified as follows: -2+3+0+2+2=5. The original expression is as follows: m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1)). Expr 1: ~1 = -1 - 1 = -2. Expr 2: 1|2 = 0001 | 0010 = 0011 = 3. Expr 3: 1&2 = 0001 & 0010 = 0000. Expr 4: 1^3 = 0001 ^ 0011 = 0010 = 2. Expr 5: 1<<1 = 0001 << 1 = 0010 = 2. Note: The bitwise expressions were demonstrated using only four bits of the 32 bit int type values.  
17
None of the above 
Prints 31,0. The expression (-1 & 0x1f) is equal to (0xffffffff & 0x1f), and both are equal to the hex value 0x1f or decimal 31. The expression (8 << -1) is equivalent to (8 << 0xffffffff). If the left hand operand of a shift expression is of type int, then the right hand operand is implicitly masked with the value 0x1f. In other words, the expression (8 << -1) is equivalent to (8 << (-1 & 0x1f)). By replacing -1 with the hexadecimal representation we have (8 << (0xffffffff & 0x1f)). By evaluating the right hand operand we have (8 << 31). When 8 is shifted 31 bits to the left, the result is zero since the only non-zero bit is lost as it is shifted beyond the most significant bit of the int data type.  

�F=mP�0 �unt: 1'>  Prints: -1
b.  Prints: 0
c.   Prints: 1
d.  Prints: 5
e.   Prints: 10
f.   Run-time error
g.  Compile-time error
h.  None of the above

Question 13

class EBH015 {
  public static void main (String[] args) {
    System.out.print((new Object() instanceof Object)+",");
    System.out.print((new Object() instanceof String)+",");
    System.out.print((new String() instanceof Object));
}}
What is the result of attempting to compile and run the program?
a.   Prints: false,false,false
b.  Prints: false,false,true
c.   Prints: false,true,false
d.  Prints: false,true,true
e.   Prints: true,false,false
f.   Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 14

class EBH023 {
  static String m1(boolean b){return b?"T":"F";}
  public static void main(String [] args) {
    boolean b1 = false?false:true?false:true?false:true;
    boolean b2 = false?false:(true?false:(true?false:true));
    boolean b3 = ((false?false:true)?false:true)?false:true;
    System.out.println(m1(b1) + m1(b2) + m1(b3));
}}
What is the result of attempting to compile and run the program?
a.   Prints: FFF
b.  Prints: FFT
c.   Prints: FTF
d.  Prints: FTT
e.   Prints: TFF
f.   Prints: TFT
g.  Prints: TTF
h.  Prints: TTT
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 15

class EBH024 {
  public static void main(String[] args) {
    int i1 = 15;
    String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
    String b2 = (i1>30)?"Red":((i1>20)?"Green":((i1>10)?"Blue":"Violet"));
    System.out.println(b1 + "," + b2);
 
}}
What is the result of attempting to compile and run the program?
a.   Prints: Red,Red
b.  Prints: Green,Green
c.   Prints: Blue,Blue
d.  Prints: Violet,Violet
e.   Prints: Blue,Violet
f.   Prints: Violet,Blue
g.  Prints: Blue,Green
h.  Prints: Green,Blue
i.   Run-time error
j.   Compile-time error
k.  None of the above

Question 16

class EBH003 {
  static int m(int i) {System.out.print(i + ", "); return i;}
  public static void main(String s[]) {
    m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}
What is the result of attempting to compile and run the program?
a.    Prints: -2, 3, 0, 3, 0, 6,
b.   Prints: -2, 3, 0, 2, 1, 4,
c.    Prints: -2, 3, 0, 2, 2, 5,
d.   Prints: -2, 3, 0, 3, 2, 6,
e.    Prints: -1, 3, 0, 3, 2, 7,
f.    Prints: -2, 0, 3, 3, 0, 6,
g.   Prints: -1, 0, 3, 2, 1, 4,
h.   Prints: -2, 0, 3, 2, 2, 5,
i.    Prints: -2, 0, 3, 3, 2, 6,
j.    Prints: -1, 0, 3, 3, 2, 7,
k.   Run-time error
l.    Compile-time error
m.  None of the above

Question 17

class EBH021 {
  public static void main(String[] args) {
    System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,0
b.  Prints: -1,4
c.   Prints: 0x1f,8
d.  Prints: 31,16
e.   Run-time error
f.   Compile-time error
g.  None of the above