Java OCA OCP Practice Question 3250

Question

Consider the following program:

public class Outer {
        private int mem = 10;
        class Inner {
                private int imem = new Outer().mem;   // ACCESS1
        }/*w  w  w .ja  va2  s  .c  om*/

        public static void main(String []s) {
                System.out.println(new Outer().new Inner().imem); // ACCESS2
        }
}

Which one of the following options is correct?

  • a) When compiled, this program will result in a compiler error in line marked with comment ACCESS1 .
  • b) When compiled, this program will result in a compiler error in line marked with comment ACCESS2.
  • c) When executed, this program prints 10.
  • d) When executed, this program prints 0.


c)

Note

An inner class can access even the private members of the outer class.

Similarly, the private variable belonging to the inner class can be accessed in the outer class.




PreviousNext

Related