ProgrammingVery hardProduct company OA

What is printed?

What is printed?
a = [1, 2]; b = a; c = a[:]
t = (a, c)
a += [3]
b = b + [4]
c *= 2
print(t[0] is a, b is a, t[1] is c, len(t[0]), len(b), len(t[1]))
  1. A. True True True 4 4 4
  2. B. False False True 2 4 4
  3. C. True False False 3 4 2
  4. D. True False True 3 4 4

Correct answer: D. True False True 3 4 4

For a list, += and *= call __iadd__/__imul__ and mutate the object in place, so every other reference (the tuple slots) sees the change and identity is preserved: t[0] is a with 3 items, t[1] is c with 4 items. b = b + [4] instead builds a new 4-element list and rebinds b, so b is no longer a, and a stays at 3 items. Output: True False True 3 4 4. Treating b + [4] as in-place gives the all-True option.

More Python Programming questions

Exams that ask this topic