Java OCA OCP Practice Question 2097

Question

Choose the best option based on this program:.

class Shape {
    public Shape() {
         System.out.println("Shape constructor");
     }//ww  w .  j a  va  2 s  .co  m
     public class Color {
         public Color() {
             System.out.println("Color constructor");
         }
     }
}

public class Main {
     public static void main(String []args) {
        Shape.Color black = new Shape().Color(); // #1
     }
}
  • A. Compiler error: the method Color() is undefined for the type shape
  • B. Compiler error: invalid inner class
  • C. Works fine: shape constructor, Color constructor
  • D. Works fine: Color constructor, shape constructor


A.

Note

You need to create an instance of outer class shape in order to create an inner class instance, as in new Shape().new Color();.




PreviousNext

Related