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))- A. bit neg seq3 other str other
- B. bit neg seq2 other seq1 other
- C. bit other seq2 map2 str other
- 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
- What is printed? python RED = "r" def f(c): match c: case RED: return "red:" + RED print(f("g"), RED)
- What is printed? python data = [3, 8, 2, 9, 4] if (n := len(data)) > 4: print(n, end=" ") print([y for x in data if (y := x 2) > 10], y)
- What is printed? python a = [5, 3, 5, 1] a.remove(5) a.insert(10, 7) a.insert(-10, 0) print(a, a.index(5), a.pop(1))
- What is printed? python a = [1, 2, 3, 4] b = a a = a.extend([5]) or a print(a is b, b[-1], [].extend([1]))
- What is printed? python s = "Mississippi" print(s.count("ss"), s.count("issi"), s.replace("ss", "S"), s.lower().index("s", 4))
- What is printed? python print("a" "b" 2, ("a" + "b") 2, "-".join(["a", "b"]) 2, "x".join("123"))