ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
try:
try:
raise KeyError("k")
except ValueError:
print("vl")
except KeyError:
print("kl")- A. vl
- B. kl
- C. vl kl
- D. KeyError traceback
Correct answer: B. kl
The inner except only matches ValueError, so it does not catch the raised KeyError; the KeyError propagates out to the outer try, where the outer except KeyError catches it and prints 'kl'.
More Python Programming questions
- What is printed? python x = 5 print(f"{x=}")
- What is printed? python print(f"{'hi':>10}|") print(f"{'hi':<10}|") print(f"{'hi':^10}|")
- What is printed? python print(f"{10 / 3:.3f}") print(f"{10 // 3}") print(f"{2 10:,}")
- What is printed? python x = [] print(bool(x), bool([0]), bool({}), bool({0: 0}))
- What is printed? python import itertools print(list(itertools.islice(itertools.count(5, 2), 4)))
- What is printed? python import itertools print(list(itertools.chain([1, 2], [3, 4]))) print(list(itertools.chain.fromiterable([[1, 2], [3…