What will be the output of this program?
class Color { int red, green, blue; void Color() { red = 10;// w w w. ja v a 2 s .com green = 10; blue = 10; } void printColor() { System.out.println("red: " + red + " green: " + green + " blue: " + blue); } public static void main(String [] args) { Color color = new Color(); color.printColor(); } }
B.
Remember that a constructor does not have a return type; if a return type is provided, it is treated as a method in that class.
In this case, since Color had void return type, it became a method named Color()
in the Color class, with the default Color constructor provided by the compiler.
By default, data values are initialized to zero, hence the output.