OCA Java SE 8 Method - OCA Mock Question Method 18








Question

How many compiler errors are in the following code?

     1: public class Main { 
     2:   private static final String v1; 
     3:   private static final String v2; 
     4:   private static final String v3; 
     5:   private static final String v4 = "v4"; 
     6:   static { 
     7:     v1 = "left"; 
     8:     v2 = "right"; 
     9:   } 
     10:   static { 
     11:     v4 = "v4"; 
     12:     v2 = "right"; 
     13:   } 
     14:   public static void main(String[] args) { 
     15:     v3 = "v3"; 
     16:   } 
     17: } 
  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5




Answer



E.

Note

Line 4 doesn't compile because v3 is not set in either of these locations.

Line 15 doesn't compile because final variables are not allowed to be set after that point.

Line 11 doesn't compile because v4 is set twice.

Line 12 doesn't compile because v2 is set twice as well.

static final variables can only be set once, and it must be in the declaration or in a static initialization block.