ProgrammingHardTCS NQT · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? try: x = [1, 2] + (3, 4) except TypeError: print("TypeError")
- A. [1, 2, 3, 4]
- B. [1, 2, (3, 4)]
- C. TypeError
- D. (1, 2, 3, 4)
Correct answer: C. TypeError
The + operator cannot combine a list and a tuple directly; mixing sequence types this way raises TypeError.
More Python Programming questions
- What does the following Python 3 code print? class MyErr(Exception): pass try: raise MyErr("custom") except MyErr as e: print("caught", e)
- What does the following Python 3 code print? def f(): for i in range(3): if i == 1: continue print(i, end="") print() f()
- What does the following Python 3 code print? def f(): for i in range(5): if i == 3: break else: print("else-no-break") print(i) f()
- What does the following Python 3 code print? def f(): for i in range(3): pass else: print("else-ran") f()
- What does the following Python 3 code print? while False: print("never") else: print("while-else")
- What does the following Python 3 code print? x = 1 while x < 5: x += 1 if x == 3: break else: print("no-break") print(x)