ProgrammingHardIBM · Deloitte · Product Company SDE
What does the following Python 3 code print? try: a = (1, 2, 3) a[0] = 9 except TypeError: print("TypeError")
- A. AttributeError
- B. TypeError
- C. (9, 2, 3)
- D. 9
Correct answer: B. TypeError
Tuples are immutable, so item assignment raises TypeError, which is caught and reported here.
More Python Programming questions
- What does the following Python 3 code print? try: d = {} print(d["missing"]) except KeyError: print("KeyError")
- What does the following Python 3 code print? try: s = "abc" print(s[10]) except IndexError: print("IndexError")
- What does the following Python 3 code print? try: x = int("12a") except ValueError: print("ValueError")
- What does the following Python 3 code print? try: def f(a, b): return a + b f(1) except TypeError: print("TypeError")
- What does the following Python 3 code print? try: x = [1, 2] + (3, 4) except TypeError: print("TypeError")
- What does the following Python 3 code print? class MyErr(Exception): pass try: raise MyErr("custom") except MyErr as e: print("caught", e)