ProgrammingHardAccenture · Cognizant · Python
What does the following Python 3 code print? try: x = int("12a") except ValueError: print("ValueError")
- A. 12
- B. 0
- C. TypeError
- D. ValueError
Correct answer: D. ValueError
int() requires the whole string to be a valid integer literal; the trailing 'a' makes it unparseable, raising ValueError.
More Python Programming questions
- 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)
- 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()