ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
print(str(()), str([()]), repr(('a',)), str(('a', 1)))
  1. A. () () ('b') ('a', 1)
  2. B. () [()] ('a',) ('a', 1)
  3. C. () [()] ('c') ('a', 1)
  4. D. () [] ('a',) ('a', 2)

Correct answer: B. () [()] ('a',) ('a', 1)

str of an empty tuple is (), and a list containing it prints as [()]. A one-element tuple keeps its trailing comma in repr, and the two-element tuple shows both values.

More Python Programming questions

Exams that ask this topic