Java OCA OCP Practice Question 2483

Question

Given:

2. public class Main {  
3.   private int y = 8;  
4.   public static void main(String[] args) {  
5.     new Main().go();  
6.   }  //from   ww  w  .j  a va 2s.c  om
7.   void go() {  
8.     int x = 7;  
9.     MyInner ip = new MyInner();  
10.     class MyInner {  
11.       void doit() { System.out.println(y + x); }  
12.     }  
13.     ip.doit();  
14.   }  
15. }    

What is the result? (Choose all that apply.)

  • A. Compilation succeeds.
  • B. Compilation fails due to an error on line 3.
  • C. Compilation fails due to an error on line 8.
  • D. Compilation fails due to an error on line 9.
  • E. Compilation fails due to an error on line 10.
  • F. Compilation fails due to accessing x on line 11.
  • G. Compilation fails due to accessing y on line 11.


D and F are correct.

Note

Method-local inner classes can use variables from their enclosing methods, only if they are marked "final".

A method-local inner class can be instantiated only after it has been declared.

It is legal to use private members from the enclosing class.




PreviousNext

Related