Chapter 0 · Section 2 practice
Containers and Object Identity
These questions review the examples in the companion slides. Choose an answer, then check the explanation.
Question 1
nums = [10, 20, 30, 40]; alias = nums; alias.append(50). What is nums now?
[10, 20, 30, 40]- It raises an error —
numsis protected [10, 20, 30, 40, 50][50, 10, 20, 30, 40]
Question 2
nums = [10, 20, 30, 40]; alias = nums; fresh = [10, 20, 30, 40]. What do nums == fresh, nums is fresh, alias is nums give?
True True TrueTrue False TrueFalse False TrueTrue False False
Question 3
coords = (34.05, -118.25); coords[0] = 0. What happens?
coordsbecomes(0, -118.25)coordsbecomes(0,)- A
ValueError - A
TypeError— tuples do not support item assignment
Question 4
What does bag[0] do for a set bag?
- Returns the smallest element
- Returns the first element inserted
- Raises
TypeError: 'set' object is not subscriptable - Returns
0
Question 5
Before “gpa” is added, student = {“name”: “Alex”, “age”: 20, “skills”: […]}. What do student[“gpa”] and student.get(“gpa”, 0.0) do?
- Both return
None - The first raises
KeyError; the second returns0.0 - Both raise
KeyError - The first returns
0.0; the second raisesKeyError
Question 6
text = “tiger”. What is text.isalpha (no parentheses)?
TrueFalse- The method object itself — not yet called
- A
SyntaxError