Java OCA OCP Practice Question 1025

Question

What is the output of the following code?

1: class Printable  
2:   public void printName(double input) { System.out.print("Printable"); } 
3: } // www  .  j  ava 2  s.  co  m
4: public class Main extends Printable { 
5:   public void printName(int input) { System.out.print("Main"); } 
6:   public static void main(String[] args) { 
7:     Main m = new Main(); 
8:     m.printName(4); 
9:     m.printName(9.0); 
10:   } 
11: } 
A.  MainPrintable 
B.  PrintableMain 
C.  MainMain 
D.  PrintablePrintable 
E.  The code will not compile because of line 5. 
F.  The code will not compile because of line 9. 


A.

Note

The code compiles and runs without issue, so options E and F are incorrect.

The printName() method is an overload in Main, not an override, so both methods may be called.

The call on line 8 references the version that takes an int as input defined in the Main class, and the call on line 9 references the version in the Printable class that takes a double.

Therefore, MainPrintable is output and option A is the correct answer.




PreviousNext

Related