ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
def r(*args):
    return sum(args)
l = [1, 2, 3]
print(r(*l, *l))
  1. A. 6
  2. B. 12
  3. C. TypeError
  4. D. [1, 2, 3, 1, 2, 3]

Correct answer: B. 12

A call can unpack the same iterable with * more than once; this passes 1, 2, 3, 1, 2, 3 as six positional arguments, all collected into args, and their sum is 12.

More Python Programming questions

Exams that ask this topic