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 questionsCorrect 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).
Keep practicing
Take the free PCAP-31-03 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCAP-31-03 practice questions
- Question 3How many elements will the list2 list contain after execution of the following snippet? l…
- Question 6Select the valid fun () invocations: (select two answers) def fun (a, b=0): return a*b
- Question 7Is it possible to safely check if a class object has a certain attribute0
- Question 10Which of the equations are True? (Select two answers)
- Question 13Which of the following lambda function definitions are correct? (Select two answers)
- Question 15Which of the following invocations are valid? (Select two answers)
