Courseiva
Controlling Program FlowmediumMultiple SelectObjective-mapped

Switch Statement in Java: String Support, Default Case, and Fall-Through

Which TWO statements are true about the switch statement in Java?

Quick Answer

The correct choices are that the switch statement supports String objects and can be used as an expression that yields a value. Since Java 7, the switch statement has allowed String comparison by internally using the string's hashCode() method to select a case and then verifying equality with the equals() method, making it both efficient and type-safe for textual branching. Additionally, Java 14 introduced switch expressions with arrow syntax, enabling the switch to directly produce a value that can be assigned to a variable or returned, which is a key feature tested on the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam. A common trap is forgetting that the default case is optional in both statements and expressions, but when used in an expression, it must cover all possible values to avoid a compilation error. Memory tip: think "hash equals arrow" — String uses hashCode and equals, and arrow syntax yields a value.

⚠ Common exam trap

Oracle often tests the misconception that case values can be any variable, but the trap here is that they must be compile-time constants (final or literals), and that switch expressions require exhaustiveness, not a default case.

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

It can be used with String objects.

Java's switch statement supports String objects since Java 7, allowing comparison based on the string's hashCode() and equals() methods. Option D is correct because switch can be used as an expression (e.g., with the arrow syntax) that yields a value, which can be assigned to a variable or returned.

Answer analysis

Option-by-option breakdown

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

  • Case values can be variables that are not final.

    Why it's wrong here

    Case values must be compile-time constants.

  • It can be used with String objects.

    Why this is correct

    String is allowed in switch since Java 7.

  • It requires a default case.

    Why it's wrong here

    Default is optional.

  • It can be used as an expression that yields a value.

    Why this is correct

    Switch expressions were introduced in Java 14.

  • Each case must have a unique code block.

    Why it's wrong here

    Multiple case labels can share a code block.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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-829

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. Which THREE statements about the switch statement in Java are correct?

medium
  • A.The default case can be placed anywhere within the switch block.
  • B.Case values can be runtime expressions.
  • C.A break statement is mandatory after each case block.
  • D.Case labels can be variables if they are final and assigned a constant expression.
  • E.The switch statement can be used with int, char, String, and enum types.

Why A: The default case can be placed anywhere within the switch block according to the Java Language Specification. Option D is correct: case labels must be compile-time constant expressions; a final variable assigned a constant expression qualifies as a compile-time constant. Option E is correct: switch works with int, char, String, and enum types. Option B is incorrect because case values must be compile-time constants, not arbitrary runtime expressions. Option C is incorrect because break is not mandatory; it is optional and used to prevent fall-through.

Variation 2. Which TWO statements about the switch statement in Java are true?

easy
  • A.A break statement is optional.
  • B.A switch statement can be used as an expression in all cases.
  • C.The switch expression can be of type long.
  • D.The switch expression can be of type String.
  • E.The case values can be variables.

Why A: In a Java switch statement, the break statement is optional. If omitted, execution falls through to the next case (fall-through behavior), which can be intentional or lead to bugs. This is a core feature of the switch construct, not a requirement.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 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-829 exam.