Java OCA OCP Practice Question 3156

Question

Consider this program to answer the following question:

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

class Main {
     public static void main(String []args) {
              Shape.Color black = new Shape().Color(); // #1
     }
}

What will be the output of the program?

  • A . Compile error: the method Color() is undefined for the type Shape.
  • B. Compile 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.




PreviousNext

Related