Courseiva
Controlling Program FlowmediumMultiple ChoiceObjective-mapped

1Z0-829 Controlling Program Flow Practice Question

Given an enum Direction { NORTH, SOUTH, EAST, WEST } and a variable d of type Direction, which code snippet correctly uses a switch expression to map each direction to an abbreviation (N, S, E, W) without using a default branch?

⚠ Common exam trap

Test-takers frequently confuse switch statements with switch expressions, assuming colon syntax with yield is valid, or they forget that switch expressions must be exhaustive when no default is provided, leading them to choose an option with missing cases or an unnecessary default.

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

switch (d) { case NORTH -> "N"; case SOUTH -> "S"; case EAST -> "E"; case WEST -> "W"; }

It uses the arrow syntax in a switch expression, which requires that all possible enum values are covered without a default branch. Since Direction has exactly four constants and each is mapped to a yield value via the arrow, the switch is exhaustive and compiles successfully.

Answer analysis

Option-by-option breakdown

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

  • switch (d) { case NORTH -> "N"; case SOUTH -> "S"; case EAST -> "E"; case WEST -> "W"; }

    Why this is correct

    Correct arrow syntax covering all enum values.

  • switch (d) { case NORTH: yield "N"; case SOUTH: yield "S"; case EAST: yield "E"; case WEST: yield "W"; }

    Why it's wrong here

    Colon syntax with yield is not valid for a switch expression; arrow syntax is required.

  • switch (d) { case NORTH -> "N"; case SOUTH -> "S"; case EAST -> "E"; }

    Why it's wrong here

    Missing case for WEST, so the switch is not exhaustive and will not compile.

  • switch (d) { case NORTH: yield "N"; case SOUTH: yield "S"; case EAST: yield "E"; default: yield "W"; }

    Why it's wrong here

    Uses colon syntax incorrectly for a switch expression.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.