These questions follow Data in Programs β the tracing habit comes from Predict the Output. Trace on paper first; the computer checks.
Questions
- 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 - Predict the output, character by character β spaces count.
first = "Rob" last = "Ott" print(first + last) - Predict the output. Does changing
aafterwards changeb?a = 3 b = a a = 7 print(b) - Find the bug.
prize = "100"thentotal = prize + 50β and line 2 crashes. Name the problem, then fix it two different ways. - Write a three-line snippet: your name in one variable, your favourite number in another, then print a sentence using both.
- Challenge. Variables
redandbluehold values. Swap them β afterwards, each must hold the otherβs old value. - Data types. State the data type (
int,float,str, orbool) of each: (a)42, (b)3.14, (c)"True", (d)True, (e)"0". Explain whytype("True")is different fromtype(True)and how each is used in a program. - 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
Answer 1
scoreis15. In Python,=is an instruction β work out the right side, store it on the left β so line 2 adds 5 to what is there.
Answer 2
RobOttβ no space, because+glues strings together exactly as they are. WantRob Ott? Add it yourself:first + " " + last.
Answer 3
It prints
3. Line 2 copies the value 3 intobβ it does not tiebtoaforever. Reassigningalater changes nothing.
Answer 4
prizeholds the text"100", and adding text to a number is aTypeError. Fix 1:prize = 100. Fix 2:total = int(prize) + 50.
Answer 5
One version β yours will differ:
name = "Priya",number = 17, thenprint(name + " picks " + str(number) + " every time.").
Answer 6
A third variable holds one value while the other moves:
spare = red,red = blue,blue = spare. Try it without the spare and watch a value get overwritten β that is why it exists.
Answer 7
(a)
int(integer), (b)float(decimal floating-point number), (c)str(text string), (d)bool(Boolean truth value), (e)str(text string, because quotes surround it)."True"is text for printing or matching words;Trueis a boolean truth value used directly in conditionals (if True:) to decide execution flow.
Answer 8
(a)
42β exponent first (), then multiply (), then add (). (b)112β brackets first (), exponent (), then multiply (). (c)18β division () and modulo (), then left-to-right (). Without brackets ina + b / 2, division outranks addition, dividing onlybby 2 rather than the sum.
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