nerdexam
Oracle

1Z0-803 · Question #211

Give: What is the result?

The correct answer is A. 1525. The result is determined by Java's string concatenation rules, where adding an integer to a string or a string to an integer performs string concatenation.

Working with Java Data Types

Question

Give:

What is the result?

Exhibit

1Z0-803 question #211 exhibit

Options

  • A1525
  • B13
  • CCompilation fails
  • DAn exception is thrown at runtime
  • EThe program fails to execute due to runtime error

How the community answered

(45 responses)
  • A
    71% (32)
  • B
    2% (1)
  • C
    9% (4)
  • D
    13% (6)
  • E
    4% (2)

Why each option

The result is determined by Java's string concatenation rules, where adding an integer to a string or a string to an integer performs string concatenation.

A1525Correct

In an expression like `1 + "5" + 2 + "5"`, Java evaluates from left to right. `1 + "5"` converts `1` to a string and concatenates, resulting in "15". Then, "15" + `2` converts `2` to a string and concatenates, resulting in "152". Finally, "152" + "5" concatenates to produce "1525".

B13

This result (13) would only occur if all operands were integers and regular arithmetic addition was performed.

CCompilation fails

The expression `1 + "5" + 2 + "5"` is syntactically valid in Java and compiles successfully due to automatic type coercion for string concatenation.

DAn exception is thrown at runtime

No exception is thrown at runtime for this type of valid string concatenation operation.

EThe program fails to execute due to runtime error

The program will execute successfully as the operations are valid Java syntax and runtime behavior.

Concept tested: Java string concatenation and type coercion

Source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.18.1

Topics

#primitive types#type conversion#string concatenation#operators

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice