Exam practice
Every question from the end of every unit, with hints and answers to open. After you check an answer, mark how you did, and come back to the ones you didn’t get.
10 questions
Unit 1 · From Blocks to Python
Name Python’s four types of value from this unit, with two examples of each.
Answer
- int, a whole number:
12,-5 - float, a number with a decimal point:
7.5,3.0 - str, a string of characters in quotes:
"Sita","12" - bool, true or false:
True,False
What is the value of each expression? i. 9 + 6 / 3 ii. (9 + 6) / 3 iii. 2 ** 3 * 2 iv. 50 // 8 v. 50 % 8 vi. "ab" * 3
Answer
- i.
11.0: the division comes first, 6 / 3 = 2.0, then 9 + 2.0 = 11.0 (a float, because/gives a float). - ii.
5.0: the brackets first, 15 / 3. - iii.
16: the power first, 2 ** 3 = 8, then 8 × 2. - iv.
6: 8 goes into 50 six whole times. - v.
2: 50 − 6 × 8 = 2 left over. - vi.
"ababab": a string times a whole number repeats it.
What is the difference between = and == in Python? Give an example of each.
Answer
= sets a variable: it puts the value on its right into the variable on its left. Example: age = 13 puts 13 into age.== compares two values and gives True or False. Example: age == 13 is True if age holds 13, and False if it doesn’t. It doesn’t change age.
Write the output of this program.
a = 5b = 2a = a + bb = a * bprint(a, b)print(a // b, a % b)print(f"{a} and {b}")Answer
Output
7 140 77 and 14After line 3, a is 5 + 2 = 7. Line 4 uses the new a: 7 × 2 = 14. Then 7 // 14 is 0 (14 doesn’t go into 7 at all), and 7 % 14 is 7 (all of it is left over).
Why does this program print 333 instead of 9? Correct it.
n = input("Pick a number: ")print(n * 3)(The user typed 3.)
Answer
input() always gives a string, so n holds the text "3", and a string times 3 is the string repeated three times: 333. Turn the answer into a number as it is typed:
n = int(input("Pick a number: "))print(n * 3)Now it prints 9.
Each line below stops with an error. Name the kind of error, and correct the line.
- i.
print("Namaste) - ii.
total = 5 + "5" - iii.
Print(10) - iv.
x = int("ten") - v.
print(price)(no variable price was set)
Answer
- i.SyntaxError (the string is never closed):
print("Namaste"). - ii.TypeError (a number and a string can’t be added):
total = 5 + 5, orint("5")if the 5 comes as text. - iii.NameError (
Printwith a capital P is not known):print(10). - iv.ValueError (“ten” isn’t digits):
x = int("10"), orx = 10. - v.NameError: set price first, for example
price = 50, on a line before it.
Write a program that asks how many students are going on a trip, and prints how many 40-seat buses are completely full, and how many students are in the last bus. Test it with 130.
Answer
students = int(input("How many students? "))full_buses = students // 40last_bus = students % 40print("Full buses:", full_buses)print("Students in the last bus:", last_bus)Output
How many students? 130Full buses: 3Students in the last bus: 10130 ÷ 40 = 3 remainder 10. (If the last number is 0, there is no extra bus; you will make the program say so in Unit 2.)
Rewrite this block program in Python, and give its output when the user types 4.
when ▶ Run is pressedset price to 30ask “How many plates of momo?” and keep the answer in platesset bill to plates × pricechange bill by 20say join “Total with the delivery charge: Rs ” billAnswer
price = 30plates = int(input("How many plates of momo? "))bill = plates * pricebill = bill + 20print(f"Total with the delivery charge: Rs {bill}")Output
How many plates of momo? 4Total with the delivery charge: Rs 140(The prices are made up.) The change block becomes bill = bill + 20, and int() is needed because input() gives a string.
A student wrote this program to find the average of three marks, but it prints the wrong answer. Find the mistake, explain it, and correct it.
a = 60b = 70c = 80average = a + b + c / 3print(average)Answer
It prints 156.66666666666666 instead of 70. Python divides before it adds, so it works out c / 3 first (26.67) and adds a and b to that. The sum must be in brackets:
average = (a + b + c) / 3Now it prints 70.0. There is no error message, because the program does exactly what it says; this kind of mistake is found only by testing with numbers whose answer you know.
Explain how you read a Python error message, using this one as an example.
Error message
Traceback (most recent call last): File "main.py", line 4, in <module> total = pens * price ^^^^NameError: name 'pens' is not definedAnswer
Start with the last line. It gives the kind of error, NameError, and what went wrong: Python doesn’t know a name called pens. Then look at the line number above it: line 4 of the program (main.py is the program’s file name), which is printed under it, total = pens * price, with ^^^^ under pens. So on line 4 Python met the name pens and no variable of that name had been set.
The mistake may be on line 4 (the name misspelled, perhaps it should be pen), or earlier (the line that should set pens is missing or misspelled, such as pnes = 3). Check the lines above, fix one thing, and run the program again.
