Java OCA OCP Practice Question 396

Question

What is the minimal modification that will make this code compile correctly?

 1. final class MyClass 
 2. { //from  w  w  w . j a v  a2  s . c o  m
 3.     int xxx; 
 4.     void yyy() { xxx = 1; } 
 5. } 
 6. 
 7. 
 8. class MyClass2 extends MyClass 
 9. { 
10.     final MyClass finalref = new MyClass(); 
11. 
12.     final void yyy() 
13.     { 
14.         System.out.println("In method yyy()"); 
15.         finalref.xxx = 12345; 
16.     } 
17. } 
  • A. On line 1, remove the final modifier.
  • B. On line 10, remove the final modifier.
  • C. Remove line 15.
  • D. On lines 1 and 10, remove the final modifier.
  • E. The code will compile as is. No modification is needed.


A.

Note

The code will not compile because on line 1.

class MyClass is declared final and may not be subclassed.

The instance variable finalref is final, so it may not be modified.

It can reference only the object created on line 10.

However, the data within that object is not final, so nothing is wrong with line 15.




PreviousNext

Related