Reading a Program Someone Else Wrote · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Read it before you run it

Computing · Computing Fundamentals · ages 13-14
Name ______________________   Date ____________
  1. Read this little program before you run it:

    if 8 < 3 then 40 else 60

    What value does it give back?

    • 40
    • 8
    • 60
    • It gives an error
  2. Read this little program before you run it: if 7 > 5 then 10 else 20. What value does it give back?

    • 20
    • 10
    • 7
  3. You open a short program a classmate wrote. What should you do first?

    • Say what it does before running it
    • Rewrite it from scratch at once
    • Run it ten times without reading
  4. Someone wrote this line: if 3 = 4 then "yes" else "no". Without running it, what does it produce?

    • "yes"
    • 3
    • "no"
  5. Here is another short program:

    if false then "yes" else "no"

    What kind of value does it give back?

    • A whole number
    • A true or false value
    • A piece of text
    • It gives an error
  6. Program: if true then 5 else 8. What kind of value does it give back?

    • A true or false value
    • A piece of text
    • A whole number
  7. Program: if score >= 50 then "pass" else "retry". The pass mark must rise from 50 to 60. Which part must change?

    • The word then
    • The test: score >= 50 to score >= 60
    • The text "retry"
  8. Read this program a classmate wrote:

    if speed > 30 then "slow down" else "ok"

    The limit must drop from 30 to 25. Which part of the line must change?

    • The else answer: change "ok" to "fine"
    • The then answer: change "slow down" to "stop"
    • The test: change speed > 30 to speed > 25
    • Nothing, the line already handles it
  9. Read before running: if "cat" = "dog" then 1 else 0. What does it give back, and why?

    • 1, because every if starts with the then answer
    • 0, because the two words are not the same
    • It breaks, because words cannot be compared
  10. Read this program before running it:

    if "sun" = "sun" then 1 else 0

    What does it give back, and why?

    • 0, because words cannot be compared
    • 1, because the two words are the same
    • 0, because the test is false
    • It gives an error
LightMySky · lightmysky.comW1-mt_VdxhuxjBSn-s1

Answer key

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

Read it before you run it W1-mt_VdxhuxjBSn-s1

  1. 60 · The test 8 < 3 is false, so the program picks the value after else, which is 60.
  2. 10 · The test 7 > 5 is true, so the program picks the value after then, which is 10.
  3. Say what it does before running it · Predicting from reading shows you understand the test and branches. Running blindly only shows outputs.
  4. "no" · 3 is not equal to 4, so the test is false and the program picks the else branch.
  5. A piece of text · Both answers are text in quotes, so whichever branch runs, the program gives back text.
  6. A whole number · Both branches hold whole numbers, so every path returns one.
  7. The test: score >= 50 to score >= 60 · The pass mark lives in the test, so changing that number moves exactly the asked behaviour.
  8. The test: change speed > 30 to speed > 25 · The limit lives inside the test, so only the number in speed > 30 changes.
  9. 0, because the two words are not the same · The test is false since the words differ, so the program returns the else value 0.
  10. 1, because the two words are the same · "sun" equals "sun", so the test is true and the program gives back 1.
Worksheet · LightMySky