Tuesday 24 July 2012

Java Classes - Objective Questions - Part 5

1)

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
}}

A. Prints: A1B2
B. Prints: B1B2
C. Compile-time error at 1
D. Compile-time error at 2

Answer: D

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.

2)

class GFC215 {
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}

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

A. Prints float,float
B. Prints float,double
C. Prints double,float
D. Prints double,double

Answer: A

The arguments of the method invocation expressions, m(a1) and m(b1), are of types int and long respectively. A method invocation conversion can widen an argument of type int or long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i).

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

Answer: D

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.

4)

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.

5)

interface A {void m1();}                      // 1
class B implements A {public void m1() {}}    // 2
class C implements A {protected void m1() {}} // 3
class D implements A {private void m1() {}}   // 4
class E implements A {void m1() {}}           // 5

Compile-time error is not generated at which line?

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

Answer: A

While implementing an interface the visibility of all the implemented functions should be of type 'public'.

6)

class GFC202 {}
class GFC203 extends GFC202 {}
class GFC204 {
  static void m(GFC202 x) {System.out.print("GFC202");}
  static void m(GFC203 x) {System.out.print("GFC203");}
  public static void main(String[] args) {m(null);}
}

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

A. Prints: GFC202
B. Prints: GFC203
C. Compile-time error
D. Run-time error

Answer: B

The type of the argument is null and could be converted to either type GFC202 or GFC203 by method invocation conversion; so both methods are applicable. The more specific of the two, m(GFC203 x), is chosen.

7)

interface A {
  int a = 1;                        // 1
  public int b = 2;                 // 2
  public static int c = 3;          // 3
  public static final int d = 4;    // 4
}

Which field declaration results in a compile-time error?

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

Answer: D

Data members inside the interface are of type 'public static final'.It is need not to mention them externally.All the above form of declarations are true.

8)

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

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

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

Answer: A

Here if you want to access the field 'name' declared at interface I10 use 'I10.name'.

9)

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: D.printS1 D.printS2
C. Compile-time error
D. Run-time error

Answer: C

We can't override the static methods.

10)

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 F.printS1 E.printS2
C. Prints F.printS1 F.printS2
D. Compile-time error

Answer: B

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