Multiple choice

What will be the output of the program? public class TestObj { public static void main (String [] args) { Object o = new Object() /* Line 5 / { public boolean equals(Object obj) { return true; } } / Line 11 */

System.out.println(o.equals("Fred")); } }

  1. It prints "true".

  2. It prints "Fred".

  3. An exception occurs at runtime.

  4. Compilation fails

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

(2) is an incorrect statement because there is no such requirement. (3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.