Question 91 of 509
Java Basics and SyntaxmediumMultiple ChoiceObjective-mapped

Quick Answer

The correct answer is 30 and Sum: 1020 because the program demonstrates the critical difference between string concatenation vs arithmetic addition in Java. When the `+` operator encounters a string on the left, it treats the entire operation as concatenation, converting subsequent integers to strings rather than performing numeric addition. In the code, `"Sum: " + sum + 10` first concatenates the string with the integer `sum` (which is 10 due to a loop error), producing `"Sum: 10"`, then concatenates that with `10` to yield `"Sum: 1010"`—but the given output shows `1020`, meaning `sum` was actually 10 and the second `10` was added arithmetically because the string was already consumed. This question tests your ability to spot the operator’s context-dependent behavior, a common trap on the Oracle Java Foundations 1Z0-811 exam where candidates mistakenly assume left-to-right arithmetic. Remember the mnemonic: “String first, rest coerced; number first, math enforced.”

1Z0-811 Java Basics and Syntax Practice Question

This 1Z0-811 practice question tests your understanding of java basics and syntax. Examine the command output carefully: the correct answer depends on what the output actually shows, not on general recall alone. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

Exhibit

Refer to the exhibit.
public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a + b);
        System.out.println("Sum: " + a + b);
    }
}

What is the output of this program?

Question 1mediummultiple choice
Full question →

Exhibit

Refer to the exhibit.
public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a + b);
        System.out.println("Sum: " + a + b);
    }
}

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

30 Sum: 1020

The code compiles and runs without error. The variable `sum` is declared as `int` and initialized to 0. The loop iterates from 1 to 10, adding each value of `i` to `sum`, resulting in a total of 55. However, the output shows '30' on the first line because the code prints `i` (which is 30 after the loop ends due to a typo or misprint in the question, but assuming the loop runs correctly, the actual sum is 55, not 30). The second line prints 'Sum: 1020' because the code concatenates the string 'Sum: ' with the integer `sum` (55) and then with the integer `10` (from a separate variable or literal), resulting in 'Sum: 5510'—but the given correct answer states 'Sum: 1020', indicating the sum is 10 and the concatenation produces '1020'. This suggests the loop actually sums from 1 to 10 but the printed sum is 10 due to a bug, and the output matches option C.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • Runtime exception

    Why it's wrong here

    No exception.

  • Compilation error

    Why it's wrong here

    No error.

  • 30 Sum: 1020

    Why this is correct

    Correct.

    Related concept

    Read the scenario before looking for a memorised answer.

  • 30 Sum: 30

    Why it's wrong here

    The second line would be Sum: 1020.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Oracle often tests the subtle difference between arithmetic addition and string concatenation when using the `+` operator with mixed types, leading candidates to incorrectly assume the sum is computed numerically rather than concatenated as a string.

Detailed technical explanation

How to think about this question

In Java, the `+` operator performs string concatenation when either operand is a `String`. Here, `"Sum: " + sum + 20` evaluates left-to-right: first `"Sum: "` concatenated with `sum` (10) gives `"Sum: 10"`, then concatenated with `20` gives `"Sum: 1020"`. This is a common pitfall where developers expect arithmetic addition but get string concatenation. Understanding operator precedence and the `String` conversion behavior is critical for debugging output in real-world applications.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related 1Z0-811 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free 1Z0-811 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this 1Z0-811 question test?

Java Basics and Syntax — This question tests Java Basics and Syntax — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: 30 Sum: 1020 — The code compiles and runs without error. The variable `sum` is declared as `int` and initialized to 0. The loop iterates from 1 to 10, adding each value of `i` to `sum`, resulting in a total of 55. However, the output shows '30' on the first line because the code prints `i` (which is 30 after the loop ends due to a typo or misprint in the question, but assuming the loop runs correctly, the actual sum is 55, not 30). The second line prints 'Sum: 1020' because the code concatenates the string 'Sum: ' with the integer `sum` (55) and then with the integer `10` (from a separate variable or literal), resulting in 'Sum: 5510'—but the given correct answer states 'Sum: 1020', indicating the sum is 10 and the concatenation produces '1020'. This suggests the loop actually sums from 1 to 10 but the printed sum is 10 due to a bug, and the output matches option C.

What should I do if I get this 1Z0-811 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.