ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
a = [1, 2, 3, 4]
try:
    print(a[len(a) / 2])
except TypeError:
    print('TypeError')
print(a[len(a) // 2])
  1. A. 2 | 3
  2. B. TypeError | 2
  3. C. TypeError | 3
  4. D. 2 | 2

Correct answer: C. TypeError | 3

len(a) / 2 is the float 2.0 in Python 3, and list indices must be integers, so the first print raises TypeError. The floor division gives 2, and a[2] is 3.

More Python Programming questions

Exams that ask this topic