Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Easy

Question 6

Below is the code of Test.java file:

What will be the result of compiling and executing Test class?

  • A Some text containing @ symbol
  • B Compilation error
  • C Anonymous
  • D Runtime exception
Reveal correct answer

Correct answer: C

Explanation

System.out.println(new Object()); invokes the toString() method defined in Object class, which prints fully qualified class name, @ symbol and hexadecimal value of hash code [Similar to java.lang.Object@15db9742]


In the given code, an instance of anonymous class extending Object class is passed to System.out.println() method and the anonymous class overrides the toString() method.

Thus, at runtime overriding method is invoked, which prints "Anonymous" to the console.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need