ProgrammingVery hardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
// Java 16 or later
Object o = "hello";
if (o instanceof String s && s.length() > 3) System.out.print(s.length());   // L1
if (o instanceof String t || t.isEmpty())    System.out.print("x");          // L2
  1. A. L1 only
  2. B. Both
  3. C. Neither
  4. D. L2 only

Correct answer: D. L2 only

A pattern variable is in scope only where the match is definitely true. After && it is; on the right of || the left side was false, so t is not bound there.

More OOP with C++ and Java questions

Exams that ask this topic