Java OCA OCP Practice Question 2101

Question

Choose the best option based on this program:.

class Shape {
    private boolean isDisplayed;
    protected int canvasID;
    public Shape() {
         isDisplayed = false;//from   w w w  . j av a 2 s .c om
         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
    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