These questions follow Data in Programs β€” the tracing habit comes from Predict the Output. Trace on paper first; the computer checks.

Questions

  1. After these lines run, what is stored in score? Explain why = here cannot mean what it means in maths class.
    score = 10
    score = score + 5
  2. Predict the output, character by character β€” spaces count.
    first = "Rob"
    last = "Ott"
    print(first + last)
  3. Predict the output. Does changing a afterwards change b?
    a = 3
    b = a
    a = 7
    print(b)
  4. Find the bug. prize = "100" then total = prize + 50 β€” and line 2 crashes. Name the problem, then fix it two different ways.
  5. Write a three-line snippet: your name in one variable, your favourite number in another, then print a sentence using both.
  6. Challenge. Variables red and blue hold values. Swap them β€” afterwards, each must hold the other’s old value.
  7. Data types. State the data type (int, float, str, or bool) of each: (a) 42, (b) 3.14, (c) "True", (d) True, (e) "0". Explain why type("True") is different from type(True) and how each is used in a program.
  8. Order of operations. Determine the exact value of each expression, showing the evaluation order: (a) 10 + 4 * 2 ** 3, (b) (10 + 4) * 2 ** 3, (c) 20 - 6 // 2 + 5 % 2. Explain why parentheses are required when calculating an average (a + b) / 2.

Answers

Curriculum connection

C1.3

identify various types of data and explain how they are used within programs

Link to original

C1.4

determine the appropriate expressions and instructions to use in a programming statement, taking into account the order of operations

Link to original

C2.1

use variables, constants, expressions, and assignment statements to store and manipulate numbers and text in a program

Link to original