ProgrammingHardAccenture · Cognizant · Python

What does the following Python 3 code print? try: x = int("12a") except ValueError: print("ValueError")

  1. A. 12
  2. B. 0
  3. C. TypeError
  4. 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

Exams that ask this topic