Courseiva
Controlling Program FlowmediumMultiple ChoiceObjective-mapped

1Z0-829 Controlling Program Flow Practice Question

Given: Object obj = null; String s = switch(obj) { case null -> "null"; case String str -> str; default -> "other"; }; What is the result?

⚠ Common exam trap

Test-takers frequently assume a switch expression throws NullPointerException on a null selector, but Java 14+ explicitly allows 'case null' to handle null values, making the code valid and producing the expected result.

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

"null"

In Java 14+, switch expressions can handle null values directly using a 'case null' label. Since obj is null, the switch matches the 'case null' branch and returns the string "null". Option D is correct because the code compiles and executes without error, producing "null".

Answer analysis

Option-by-option breakdown

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

  • Compilation error

    Why it's wrong here

    Incorrect; the code compiles.

  • NullPointerException

    Why it's wrong here

    Incorrect; the null case prevents NPE.

  • "other"

    Why it's wrong here

    Incorrect; the default is not reached.

  • "null"

    Why this is correct

    Correct: the null case matches and returns "null".

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

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.