Chapter 0 Β· Section 3 practice

Branches, Loops and Comprehensions

0 of 5 answered

These questions review the examples in the companion slides. Choose an answer, then check the explanation.

Question 1

score = 95. if score >= 60: grade = β€œC” then elif score >= 90: grade = β€œA”. What is grade?

  • "C" β€” the first true branch wins
  • "A" β€” the best matching branch wins
  • Both run; grade ends as "A"
  • A SyntaxError β€” thresholds must be increasing

Question 2

What does list(range(1, 5)) return?

  • [1, 2, 3, 4, 5]
  • [1, 2, 3, 4]
  • [0, 1, 2, 3, 4]
  • [1, 5]

Question 3

for x in range(1, 11): if x % 3 == 0: continue; print(x, end=” β€œ). Output?

  • 1 2
  • 3 6 9
  • 1 2 4 5 7 8 10
  • 1 2 3 4 5 6 7 8 9 10

Question 4

What does [x**2 for x in range(10) if x % 2 == 0] return?

  • [0, 4, 16, 36, 64]
  • [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  • [1, 9, 25, 49, 81]
  • [0, 4, 16, 36, 64, 100]

Question 5

How many elements does [β€œeven” if x % 2 == 0 else β€œodd” for x in range(6)] have?

  • 3
  • 0
  • 6 β€” every element gets a label
  • It is a SyntaxError