Courseiva
Primitives, Strings and OperatorshardMultiple ChoiceObjective-mapped

1Z0-811 Primitives, Strings and Operators Practice Question

Exhibit

Refer to the exhibit.
public class Test {
    public static void main(String[] args) {
        String s1 = new String("Java");
        String s2 = "Java";
        System.out.println(s1 == s2);
    }
}
Output: false

Given the following code,

String s1 = new String("example"); String s2 = "example"; System.out.println(s1 == s2);

Why does the code output false?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

s1 is created with 'new' thus not in string pool, s2 is literal in pool, so different references

The code likely creates s1 using the 'new' operator, which forces the creation of a new String object in the heap, not in the string pool. s2 is a string literal, which is interned and placed in the string pool. The '==' operator compares object references, so s1 and s2 refer to different objects, hence the comparison returns false.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • s1 is created with 'new' thus not in string pool, s2 is literal in pool, so different references

    Why this is correct

    Correct: this explains the difference in references.

  • s1 and s2 refer to different objects in the heap

    Why it's wrong here

    Incomplete: s2 may also be in the pool, not necessarily on heap; the main reason is that new creates a distinct object.

  • The String class does not override equals

    Why it's wrong here

    Incorrect: String does override equals(), but == is used here.

  • The == operator compares value not reference

    Why it's wrong here

    Incorrect: == compares references for objects.

About these practice questions

Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer writes the following code: String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2); What is the output?

easy
  • A.Compilation fails
  • B.false
  • C.true
  • D.Hello

Why C: String literals in Java are interned, meaning both s1 and s2 refer to the same String object in the string constant pool. The == operator compares object references, not content, so since both variables point to the same interned string, the comparison yields true.

Variation 2. What is the output of the following code? String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2);

medium
  • A.Hello
  • B.true
  • C.Compilation error
  • D.false

Why B: String literals are interned, so both references point to the same object in the string pool, and == compares references, resulting in true.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.