Tuesday 24 July 2012

Java Classes - Objective Questions - Part 4

1)

interface I10 {String name = "I10"; String s10 = "I10.s10";}
interface I20 {String name = "I20"; String s20 = "I20.s20";}
class C20 implements I10, I20 {         // 1
  public static void main(String[] args) {
    System.out.print(I10.s10+",");      // 2
    System.out.print(I20.s20+",");      // 3
    System.out.print(I20.name);         // 4
}}

What is the result of attempting to compile and run the program?

A. Prints: I10.s10,I20.s20,I10
B. Prints: I10.s10,I20.s20,I20
C. compile-time error
D. run-time error

Answer: B

Class C20 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C20; then, no compile-time error occurs. Although line 4 may appear to generate the compile-time error it does not, because name is accessed directly as a member of interface I20. Therefore, the compiler does not encounter an ambiguity.

2)

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 CCC
B. Prints AAA
C. Prints CBA
D. Compile-time error

Answer: 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.Similarly for others also.

3)

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

Answer: B

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.

4)

class A {}  class B extends A {}
class C extends B {
  static void m(A x, A y) {System.out.print("AA");}
  static void m(A x, B y) {System.out.print("AB");}
  static void m(B x, A y) {System.out.print("BA");}
  static void m(B x, B y) {System.out.print("BB");}
  public static void main(String[] args) {
    A a1; B b1; m(null,null); m(a1=null,b1=null); m(b1, a1);
}}

What is the result of attempting to compile and run the program?

A. Prints: BBABAB
B. Prints: BBABBA
C. Prints: BBBBAB
D. Prints: BBBBBA

Answer: B

Type B is a subclass of type A, and method m(B x, B y) is more specific than the other three; because any invocation of it could be handled by any of the other three without causing a compile-time type error. All four methods are applicable to the first method invocation expression, m(null,null).

5)

interface A {
  void m1();            // 1
  public void m2();     // 2
  protected void m3();  // 3
  private void m4();    // 4
}
Compile-time errors are generated at which lines?

A. Line 1
B. Line 2
C. Line 3 &4
D. None of the above.

Answer: C

Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.

6)

Suppose that an interface, I1, is not a member of an enclosing class or interface. Which of
the following modifiers can be applied to interface I1?

A. abstract
B. final
C. private
D. protected

Answer: A

An interface can have only two modifiers ie public and abstract.By default all the interfaces are abstract.
If you use public then the interface should be stored in a file named interface name appends .java.

7)

interface A {String s1 = "A"; String m1();}
interface B implements A {String s1 = "B"; String m1();}
class C implements B {
  public String m1() {return s1;}
  public static void main(String[] args) {
    A a = new C(); System.out.print(a.m1());
}}

What is the result of attempting to compile and run the program?

A. Prints: A
B. Prints: B
C. compile-time error
D. run-time error

Answer: C

One interface can't implement another interface .But one interface can extends one or more interfaces.

8)

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();}
}

A. Prints: ABCI
B. Compile-time error
C. Run-time error
D. None of the above

Answer: A

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.

9)

interface Z {void m1();}  // 1
class A implements Z {void m1() {}} // 2
class B implements Z {public void m1() {}} // 3
abstract class C implements Z {public abstract void m1();} // 4

A Compile-time error is generated at which line?

A. Line 2
B. Line 3
C. Line 4
D. None of the above.

Answer: A

Implemented functions should be of type 'public'.Hence compile-time error at Line 2.

10)

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 Q.printS1 Q.printS2
B. Prints P.printS1 P.printS2
C. Prints P.printS1 Q.printS2
D. Compile-time error

Answer: C

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.

No comments:

Post a Comment