Tuesday 24 July 2012

Java Classes - Objective Questions - Part 6

1)

class GFC200 {}
class GFC201 {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  static void m(GFC200 x) {System.out.print("GFC200");}
  public static void main(String[] args) {m(null);}
}

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

Prints Object
Prints String
Prints GFC200
Compile-time error

Answer: D

The type of the argument is null and could be converted to any of the types Object, String or GFC200, by method invocation conversion. All three methods are applicable, but none of the three is more specific than both of the other two. The ambiguity results in a compile-time type error.

2)

interface A {
  void m1();              // 1
  public void m2();       // 2
  protected void m3();    // 3
  private void m4();      // 4
  abstract void m5();     // 5
}

Compile-time errors are generated at which lines?

A. Line 1
B. Line 2
C. Line 3 & 4
D. Line 5

Answer: C

Inside an interface functions can have only public and abstract modifiers.

3)

interface Z {void m1();}  // 1
class D implements Z {public final void m1() {}}    // 2
class E implements Z {public synchronized void m1() {}} // 3
class G implements Z {public native void m1();}     // 4

A Compile-time error is generated at which line?

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

Answer: D

The modifiers, final, synchronized and native, can not appear in the declaration of an abstract method, but they can be added to an implementation of an abstract method.Hence all of the above are true.

4)

public class Main{
Main(){
this();
System.out.println("Main");
}
public static void main(String[] args) {
         Main  m=new Main();
}
}
//What is the result of attempting to compile and run the program?

A. Prints Main
B. Prints Main infinitely
C. Prints Nothing
D. Compile-time error

Answer: D

this() will call the default constructor.Here it will create an infinite loop.Hence the result is compile-time
error.

5)

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 DDDD
B. Prints DCBA
C. Compile-time error
D. Run-time error

Answer: A

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.

6)

class GFC205 {}class GFC206 extends GFC205 {}
class GFC207 extends GFC206 {
   static void m(GFC205 x, GFC205 y) {System.out.print("GFC205,GFC205");}
   static void m(GFC205 x, GFC206 y) {System.out.print("GFC205,GFC206");}
   static void m(GFC206 x, GFC206 y) {System.out.print("GFC206,GFC206");}
   public static void main(String[] args) {
    GFC207 gfc207 = new GFC207(); m(gfc207, gfc207);
}}

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

A. Prints: GFC205,GFC205
B. Prints: GFC205,GFC206
C. Prints: GFC206,GFC205
D. Prints: GFC206,GFC206

Answer: D

Type GFC207 is a subclass of types GFC206 and GFC205, so any of the four methods are applicable to the method invocation expression, m(gfc207, gfc207). The most specific of the four, m(GFC206 x, GFC206 y), is chosen.

7)

class GFC211 {}  class GFC212 extends GFC211 {}
class GFC213 extends GFC212 {
  static void m(GFC211 x, GFC212 y) {System.out.print("GFC211,GFC212");}
  static void m(GFC212 x, GFC211 y) {System.out.print("GFC212,GFC211");}
  static void m(GFC212 x, GFC212 y) {System.out.print("GFC212,GFC212");}
  static void m(GFC211 x, GFC213 y) {System.out.print("GFC211,GFC213");}
  public static void main(String[] args) {
    GFC213 gfc213 = new GFC213(); m(gfc213, gfc213);}}

A. Prints: GFC211,GFC211
B. Prints: GFC211,GFC212
C. Prints: GFC212,GFC211
D. Compile-time error

Answer: D

The method invocation expression, m(gfc213, gfc213), is ambiguous; because, no applicable method is more specific than all of the others.m(GFC212 x, GFC212 y) is more specific than m(GFC211 x, GFC212 y) and m(GFC212 x, GFC211 y) but it is not more specific than  m(GFC211 x, GFC213 y).

8)

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

Answer: C

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.

9)

public class Basics {}   // 1
public class Basics2 {}  // 2

Suppose these are top-level class declarations and not nested class declarations;
and suppose that all of the declarations are contained in one file named Basics.java.
A compile-time error is generated at which line?

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

Answer: B

Only one class in a source code file can be declared public.The file name should be same as that of public class name. The other classes may not be public. Therefore, the declarations for class Basics2 generate compile-time error.

No comments:

Post a Comment