Java OCA OCP Practice Question 1288

Question

What is the output of the following application?

package space; //  w ww .jav a 2  s. com
? 
interface MyBase { 
   default String getName() { return "Unknown"; } 
} 
abstract class Planet { 
   abstract String getName(); 
} 
public class Main extends MyBase implements Planet { 
   public Main() { 
      super(); 
   } 
   public String getName() { return "Main"; } 
   public static void main(final String[] probe) { 
      System.out.print(((Planet)new Main()).getName()); 
   } 
} 
  • A. Main
  • B. Unknown
  • C. The code does not compile due to the declaration of MyBase.
  • D. The code does not compile for another reason.


D.

Note

The declaration of MyBase compiles without issue, so Option C is incorrect.

The Main class declaration is invalid because Main cannot extend MyBase, an interface, nor can Main implement Planet, a class.

In other words, they are reversed.

Since the code does not compile, Option D is the correct answer.

Note that if MyBase and Planet were swapped in the Main class definition, the code would compile and the output would be Main, making Option A the correct answer.




PreviousNext

Related