Java OCA OCP Practice Question 1031

Question

What is the output of the following code?

1: abstract class Printable { 
2:   public final void print() { 
         System.out.println("Printable print");  
     } //from   w  ww  .j  av a  2 s. c  o  m
3:     public static void main(String[] args) { 
4:       Printable p = new Square(); 
5:       p.print(); 
6:     } 
7: } 
8: public class Square extends Printable { 
9:   public void print() { System.out.println("Square print"); } 
10:} 
  • A. Printable print
  • B. Square print
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.
  • E. The code will not compile because of line 9.


E.

Note

The code doesn't compile, so options A and B are incorrect.

The issue with line 9 is that print() is marked as final in the superclass Printable, which means it cannot be overridden. There are no errors on any other lines, so options C and D are incorrect.




PreviousNext

Related