ProgrammingHardTCS CodeVita · GATE CS · Advanced

What does the following Python 3 code print? try: s = "abc" print(s[10]) except IndexError: print("IndexError")

  1. A. IndexError
  2. B. None
  3. C. ''
  4. D. KeyError

Correct answer: A. IndexError

A single out-of-range index on a string raises IndexError (unlike slicing, which would just return an empty string).

More Python Programming questions

Exams that ask this topic