ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def kind(v):
    match v:
        case 0 | 1:
            return "bit"
        case int(n) if n < 0:
            return "neg"
        case [x, *rest]:
            return f"seq{len(rest)}"
        case {"k": val}:
            return f"map{val}"
        case str():
            return "str"
        case _:
            return "other"
print(kind(1), kind(-4), kind((7, 8, 9)), kind({"k": 2, "z": 0}), kind("ab"), kind(2.5))
  1. A. bit neg seq3 other str other
  2. B. bit neg seq2 other seq1 other
  3. C. bit other seq2 map2 str other
  4. D. bit neg seq2 map2 str other

Correct answer: D. bit neg seq2 map2 str other

Sequence patterns match tuples as well as lists, but never str. Mapping patterns match if the listed keys are present; extra keys are allowed. A string therefore reaches case str().

More Python Programming questions

Exams that ask this topic