Java OCA OCP Practice Question 2091

Question

Choose the correct option based on the following program:.

class Color {
     int red, green, blue;

     Color() {/*from   w ww.  ja v  a  2 s .co m*/
         this(10, 10, 10);
     }

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

     public String toString() {
         return "The color is: " + red + green + blue;
     }

     public static void main(String [] args) {
         System.out.println(new Color());
     }
}
  • A. Compiler error: incompatible types
  • B. Compiles fine, and when run, it prints the following: the color is: 30
  • C. Compiles fine, and when run, it prints the following: the color is: 101010
  • D. Compiles fine, and when run, it prints the following: the color is: red green blue


C.

Note

The toString() implementation has the expression "the color is:" + red + blue + green.

Since the first entry is string, the + operation becomes the string concatenation operator with resulting string "the color is: 10".

There is a concatenation operator + and so on until finally it prints "the color is: 101010".




PreviousNext

Related