Skip to content
CodeKruPro

Pattern matching with instanceof and switch

QuizMediumJava only

Given this method, which calls return the value shown? In a case ... when line, the when part is an extra condition (a guard) that must also be true. Pick all that apply.

static String kind(Object o) {
    return switch (o) {
        case null -> "null";
        case Integer i when i > 5 -> "big";
        case Integer i -> "int";
        case String s -> "str " + s.length();
        default -> "other";
    };
}
  • kind(9) returns "big"
  • kind(2) returns "big"
  • kind("ab") returns "str 2"
  • kind(null) throws NullPointerException

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in