Skip to content
Sharp

Language Upgrades · interview question

Which of the following is a correct use of a switch expression in Java?

Multiple choiceEasychapter-12-curatedpremiumlanguage-upgradespattern-matching
Choose one answer
Answer and explanation

Correct answer

D. Int result = switch (value) { case 1 -> 10; case 2 -> 20; default -> 0; };

Why

The correct syntax for a switch expression uses '->' to define the result for each case, making it more concise and easier to read compared to the traditional switch statement.

Test yourself on Language Upgrades

A free test with instant, explained results. No account needed.

Start free test
All Language Upgrades questions →