Which answer choice can replace line 6 so the code continues to produce the same output?
3: List<String> m = new ArrayList<>(); 4: m.add("A"); 5: m.add("B"); 6: System.out.println(m);
A. System.out.println(m.asString); B. System.out.println(m.asString()); C. System.out.println(m.toString); D. System.out.println(m.toString());
D.
The toString()
method is declared in the Object class.
Therefore it is available to be called in any Java class and is overridden in some.
Java automatically calls the toString()
method when you print an object, making Option D correct.
Option C is incorrect because toString()
is a method, not a variable.