Java OCA OCP Practice Question 2081

Question

Determine the output of this program:.

class Color {
     int red, green, blue;

     Color() {//from   w  w w  .  ja  va  2 s.  c om
             Color(10, 10, 10);
     }

     Color(int r, int g, int b) {
             red = r;
             green = g;
             blue = b;
     }

     void printColor() {
              System.out.println("red: " + red + " green: " + green + " blue: " +
                blue);
     }

     public static void main(String [] args) {
             Color color = new Color();
             color.printColor();
     }
}
  • A. Compiler error: cannot find symbol
  • B. Compiles without errors, and when run, it prints: red: 0 green: 0 blue: 0
  • C. Compiles without errors, and when run, it prints: red: 10 green: 10 blue: 10
  • D. Compiles without errors, and when run, crashes by throwing NullPointerException


A

Note

The compiler looks for the method Color() when it reaches this statement: Color(10, 10, 10);.

The right way to call another constructor is to use the this keyword as follows: this(10, 10, 10);.




PreviousNext

Related