interface I{ final class C1 { //1 static int i=9;; //2 } } class C2 implements I{ public static void main(String a[]){ System.out.println(I.C1.i); ///3 }}
-
compile time error at line 1
-
compile time error at line 2
-
compile time error at line 3
-
prints 9
-
Runtime exception
In Java, an interface can define a nested class, which is implicitly public and static. A final nested class is legal, and defining a static member inside it is also valid. The code compiles without issues and successfully prints 9.
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) compile time error at line 1 - This option is incorrect. There is no compile-time error at line 1. The class declaration of C1 inside the interface I is valid.
Option B) compile time error at line 2 - This option is incorrect. There is no compile-time error at line 2. The declaration of the static variable i inside the inner class C1 is valid.
Option C) compile time error at line 3 - This option is incorrect. There is no compile-time error at line 3. The statement System.out.println(I.C1.i); is accessing the static variable i from the inner class C1 and is valid.
Option D) prints 9 - This option is correct. The output of the given code will be 9. The statement System.out.println(I.C1.i); prints the value of the static variable i from the inner class C1, which is 9.
Option E) Runtime exception - This option is incorrect. There will be no runtime exception in this code. The code will compile and execute without any exceptions.
The correct answer is D. This option is correct because the code will print the value 9.