Multiple choice technology programming languages

What is the output for the following lines of code? 1: public class Q8 2: { 3: int i = 20; 4: static 5: { 6: int i = 10; 7: 8: } 9: public static void main(String[] args) 10: { 11: Q8 a = new Q8(); 12: System.out.println(a.i); 13: } 14: }

  1. Compilation error, variable "i" declared twice.

  2. Compilation error, static initializers for initialization purpose only.

  3. Prints 10.

  4. Prints 20.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The static initializer block declares a local variable i that shadows the instance variable. This is valid Java - local variables can shadow instance variables within their scope. The instance variable i remains unchanged (20). When main runs, a.i prints the instance variable value, which is 20.