Java OCA OCP Practice Question 2497

Question

What is the result of the following program?

1:    public abstract class Message { 
2:       public String recipient; 
3:       public abstract final void sendMessage(); 
4:       public static void main(String[] args) { 
5:          Message m = new TextMessage(); 
6:          m.recipient = "1234567890"; 
7:          m.sendMessage(); // w  w w. j  av a 2 s.  c  o  m
8:       } 
9:       static class TextMessage extends Message { 
10:         public final void sendMessage() { 
11:            System.out.println("Text message to " + recipient); 
12:         } 
13:      } 
14:   } 
  • A. Text message to null.
  • B. Text message to 1234567890.
  • C. A compiler error occurs on line 1.
  • D. A compiler error occurs on line 3.
  • E. A compiler error occurs on line 7.
  • F. A compiler error occurs on another line.


D.

Note

The code does not compile because a method is not allowed to be both abstract and final.

If final were removed, the answer would be B.

An abstract class may contain an abstract method.

A static nested class may extend other classes.




PreviousNext

Related