Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard

Question 28

Consider below code:


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

  • A

    Java

    <Some text containing @ symbol>

    <Some text containing @ symbol>

    null

  • B

    Java

    <Some text containing @ symbol>

    <Some text containing @ symbol>

  • C

    null

    Java

    <Some text containing @ symbol>

    <Some text containing @ symbol>

  • D

    Java

    Java

    Java

    null

  • E

    Java

    Java

    Java

  • F

    null

    Java

    Java

    Java

  • G

    Java

    Java

    <Some text containing @ symbol>

  • H

    null

    Java

    Java

    <Some text containing @ symbol>

  • I

    Java

    Java

    <Some text containing @ symbol>

    null

Reveal correct answer

Correct answer: H

Explanation

Variable 'arr' refers to an Object array of size 4 and null is assigned to all 4 elements of this array.

for-loop starts with i = 1, which means at 1st index String instance is stored, at 2nd index StringBuiler instance is stored and at 3rd index SpecialString instance is stored. null is stored at 0th index.

So, first null will be printed on to the console.

String and StringBuilder classes override toString() method, which prints the text stored in these classes. SpecialString class doesn't override toString() method and hence when instance of SpecialString is printed on to the console, you get: <fully qualified name of SpecialString class>@<hexadecimal representation of hashcode>.

Therefore output will be:

null

Java

Java

<Some text containing @ symbol>

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