Java OCA OCP Practice Question 1229

Question

How many final modifiers would need to be removed for this application to compile?

package mypkg; //from  w  ww.  j ava 2  s .com
public class Main { 
        public final static long numberOfMains; 
        public final double height; 
        static {} 
        { final int initHeight = 2; 
          height = initHeight; 
        } 
        static { 
           numberOfMains = 100; 
           height = 4; 
        } 
} 
  • A. None
  • B. One
  • C. Two
  • D. The code will not compile regardless of the number of final modifiers removed.


D.

Note

The last static initialization block accesses height, which is an instance variable, not a static variable.

The code will not compile no matter how many final modifiers are removed, making Option D the correct answer.

If the line height = 4; was removed, then no final modifiers would need to be removed to make the class compile.




PreviousNext

Related