What will be the output of executing the following Python list operations?

a = [1, 2, 3]
b = a
b.append(4)
print(len(a))

Answer and explanation

Correct answer: C. 4

  1. Step 1: In Python, variable assignment `b = a` assigns an object reference, not a deep or shallow copy.
  2. Step 2: Both `a` and `b` reference the identical underlying list in memory. Appending 4 via `b` modifies the list to `[1, 2, 3, 4]`, making `len(a)` equal to 4.
Answer
4.
Why not the others: 3 assumes `a` remains unmodified; 1 and Error misunderstand Python object reference semantics.

Practise more Algorithms and Python Programming.

Free questions with an explanation for every one, and full timed EmSAT Achieve (UAE) mock tests.