When executing System.out.println(a2), the toString() method in the Object class is invoked. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); When executing System.out.println(a1), the toString() method in the Object class is invoked. When executing System.out.println(a1), the toString() method in the A class is invoked. True or false?

Respuesta :

Answer:

in the code snippet below, c. When System.out.println(a2) is executing, the toString() method is called.

d. When System.out.println(a1) is executing, the toString() method in class A gets called.

Explanation:

public class Test{

 public static void main(String[] args){

   Object a1 = new A();

   Object a2 = new Object();

   System.out.println(a1);

   System.out.println(a2);

 }

}

class A{

 int x;

 @Override

 public String toString(){

   return "A's x is " + x;

 }  

}

ACCESS MORE
EDU ACCESS