Java OCA OCP Practice Question 3006

Question

Given:

interface Shape {
   int patent = 12345;

   Shape doShape();//w  w  w . j ava  2s  .  com
}

public class Main implements Shape {
   int patent = 34567;

   public static void main(String[] args) {
      new Main().doShape();
   }

   Main doShape() {
      System.out.println(++patent);
      return new Main();
   }
}

If javac is invoked twice:

javac -source 1.4 Main.java  
javac Main.java 

And, if "java Main" is invoked whenever Main compiles, what is the result?

  • A. First, compilation fails, then 12346
  • B. First, compilation fails, then 34568
  • C. First, 12346, then compilation fails.
  • D. First, 34568, then compilation fails.
  • E. First, 12346, then 34568
  • F. Compilation fails on both javac invocations.


F	 is correct.

Note

Regardless of the Java version, the implementing doShape() method must be declared public.

If it was declared public, the answer would be B.

The Java 1.4 compilation would fail because doShape() is using a covariant return, which wasn't available until Java 5.




PreviousNext

Related