Selection with if, elif and else · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

One road per run

Computing · Programming · ages 14-16
Name ______________________   Date ____________
  1. An else branch runs when no test above it is true.

    Circle one:   True   False

  2. The temperature is 20. The program runs: if temperature < 32, print a snow jacket warning. What prints?

    • The snow jacket warning
    • Nothing at all
    • An error message
  3. How many branches run in one pass through a chain?

    • All of them
    • None of them
    • Exactly one
  4. A grade chain tests A at 90, B at 70, else Fail. The mark is 82. What prints?

    • Fail
    • A
    • B
  5. A program labels a number positive, negative or neutral with a chain. The number is -5. What prints?

    • positive
    • negative
    • neutral
  6. Why must the A test sit above the B test in a grade chain?

    • The program crashes otherwise
    • A mark like 95 would match the lower test first and stop
    • Marks would print twice
  7. A whole if and else only sets ok to true when users > 100. Which line replaces it?

    • ok = users + 100
    • ok = users > 100
    • ok stays empty
  8. A chain tests mark >= 70 before mark >= 90, and a 95 prints B. What is the fix?

    • Test the highest boundary first
    • Delete the else
    • Swap print for input
LightMySky · lightmysky.comW1-mt_zylJ-p9g1g-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

One road per run W1-mt_zylJ-p9g1g-s1

  1. True · else is the catcher for everything the tests above missed.
  2. The snow jacket warning · 20 is below 32, so the if test is true and its block runs.
  3. Exactly one · One road per run: the first true branch, or the else.
  4. B · 82 misses A, matches B, and the chain stops there.
  5. negative · -5 fails the above-zero test and passes the below-zero test.
  6. A mark like 95 would match the lower test first and stop · The chain stops at the first match, so a loose test on top steals strong marks.
  7. ok = users > 100 · The test itself produces the Boolean, so store it directly.
  8. Test the highest boundary first · Order decides priority, so the strictest test must come first.
Worksheet · LightMySky