Chapter 0 · Section 2 practice

Containers and Object Identity

0 of 6 answered

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 — nums is 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 True
  • True False True
  • False False True
  • True False False

Question 3

coords = (34.05, -118.25); coords[0] = 0. What happens?

  • coords becomes (0, -118.25)
  • coords becomes (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 returns 0.0
  • Both raise KeyError
  • The first returns 0.0; the second raises KeyError

Question 6

text = “tiger”. What is text.isalpha (no parentheses)?

  • True
  • False
  • The method object itself — not yet called
  • A SyntaxError