Java OCA OCP Practice Question 3158

Question

What will be the output of the given program?

class Shape {
     private boolean isDisplayed;
     protected int canvasID;
     public Shape() {
             isDisplayed = false;/*w w w.j  ava 2s  .c o  m*/
             canvasID = 0;
     }
     public class Color {
             public void display() {
                     System.out.println("isDisplayed: "+isDisplayed);
                     System.out.println("canvasID: "+canvasID);
             }
     }
}

public class Main {
     public static void main(String []args) {
             Shape.Color black = new Shape().new Color();
             black.display();
     }
}
A.   Compiler error: an inner class can only access public members of the outer class.

B.    Compiler error: an inner class cannot access private members of the outer class.

C.    Runs and prints this output:

      isDisplayed: false/*from ww  w . ja  v  a 2 s.  c o  m*/
      canvasID: 0

D.    Compiles fine but crashes with a runtime exception.


C.

Note

An inner class can access all members of an outer class, including the private members of the outer class.




PreviousNext

Related