75.1k views
1 vote
Assume the class BankAccount has been created, and the following statement correctly creates an instance of the class:

BankAccount account = new BankAccount(5000.0);
What is TRUE about the following statement?
System.out.println(account);

a. The account object's toString method will be implicitly called.
b. A compiler error will occur.
c. The method will display unreadable binary data on the screen.
d. A runtime error will occur.

1 Answer

1 vote

Answer:

C

Step-by-step explanation:

All Java objects have a toString() method, which is invoked when you try to print an object. The output will be some unreadable binary data because the toString method of the object has not explicitly define in our BankAccount class. To get a readable text as output, the toString method of our BankAccount class need to be over ride.

Adding the below snippet to the BankAccount class will yield a readable output.

@Override

public String toString() {

return nameOfField;

// nameOfField is the name of field defined in BankAccount class which is passed as parameter when creating an instance of the object.

}

User CaRDiaK
by
6.0k points