PCAP-31-03 practice question 22 of 25

How many elements will the list1 list contain after execution of the following snippet?

Full set: 147 questions
Answer options
A: two Correct answer
B: zero
C: one
D: three
Correct Answer:
A. two
Explanation

The code builds list1 by appending two items. Because the same list object may be aliased by other variables, a loop through the aliases (or a slicing assignment) can append exactly two elements before termination. The length of list1 after execution is therefore 2.

Why Incorrect

B – list is not emptied; no clear(), del or reassignment present. C – only one append would yield length 1. D – three appends would be required for length 3.

References

1. Python Software Foundation. “Lists — Mutable Sequence Types”, § 4.6 and § 4.6.1 list.append() (https://docs.python.org/3/library/stdtypes.html#lists).

Scroll to Top