Skip to content
Sharp

Language Upgrades · interview question

Which of the following demonstrates the correct usage of pattern matching for instanceof in Java?

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

Correct answer

A. If (obj instanceof String s) { System.out.println(s.length()); }

Why

Pattern matching for instanceof allows direct use of the matched type variable within the block. Option 1 is correct as it demonstrates the use of the pattern variable 's' directly without casting.

Test yourself on Language Upgrades

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

Start free test
All Language Upgrades questions →