Lists · Class 11 · CBSE · Science Computer Science

What is printed by the following Python code? a = [10, 20, 30]; b = a; c = a[:]; b[1] = 99; c.append(40); print(a, c)

Practice question from Lists (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · MCQ · Q-F66236

What is printed by the following Python code? a = [10, 20, 30]; b = a; c = a[:]; b[1] = 99; c.append(40); print(a, c)

  1. A.[10, 99, 30] [10, 20, 30, 40]Correct answer
  2. B.[10, 20, 30] [10, 20, 30, 40]
  3. C.[10, 99, 30, 40] [10, 20, 30]
  4. D.[10, 99, 30] [10, 99, 30, 40]

Solution

The assignment b = a creates an alias, so changing b[1] also changes a[1]. The slice c = a[:] creates a separate list, so appending to c does not affect a.

More from Lists

All of Lists

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed