Java OCA OCP Practice Question 2093

Question

Choose the best option based on the following program:.


class Color {
   int red, green, blue;

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

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

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

   public static void main(String[] args) {
      // implicitly invoke toString method
      System.out.println(new Color());
   }
}
  • A. Compiler error: attempting to assign weaker access privileges; toString was public in Object
  • B. Compiles fine, and when run, it prints the following: the color is: red = 10 green = 10 blue = 10
  • C. Compiles fine, and when run, it prints the following: the color is: red = 0 green = 0 blue = 0
  • D. Compiles fine, and when run, it throws ClassCastException


A.

Note

no access modifier is specified for the toString() method.

Object's toString() method has a public access modifier; you cannot reduce the visibility of the method.

It will result in a compiler error.




PreviousNext

Related