Practise Syntax, logic and runtime errors. 12 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.
Build a paper on this topic
Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.
Question 1Easy4 marks
This program contains three syntax errors.
01 name = input("Enter your name: ")
02 if name = "Sam":
03 print("Welcome back, Sam")
04 else
05 print("Hello, " + name
(a) For each syntax error, give the line number and write the corrected line.[3]
(b) Define the term 'syntax error'.[1]
Show the answer and mark scheme
(a) Answer: Line 02:
if name == "Sam":Line 04:
else:Line 05:
print("Hello, " + name)- line 02:
if name == "Sam": - line 04:
else: - line 05:
print("Hello, " + name)
(b) Answer: A mistake in the rules (grammar) of the programming language, which stops the program from being translated and run.
- an error that breaks the rules / grammar of the programming language (so the program cannot be translated / run)
Question 2Medium6 marks
This program should ask for five numbers and output their mean. It contains a syntax error, a logic error and a runtime error.
01 total = 0
02 for count in range(5)
03 number = int(input("Enter a number: "))
04 total = number
05 average = total / 5
06 print("Average: " + average)
(a) Identify the line that contains the syntax error and write the corrected line.[2]
(b) Identify the line that contains the logic error and write the corrected line.[2]
(c) Line 06 causes a runtime error. Give the reason for this error.[1]
(d) Write a corrected version of line 06.[1]
Show the answer and mark scheme
(a) Answer: Line 02:
for count in range(5):- line 02
for count in range(5):
(b) Answer: Line 04:
total = total + number- line 04
total = total + number
(c) Answer: A string cannot be joined to a real number with +.
- a string and a number / float cannot be joined (concatenated) with +
(d) Answer: print("Average: " + str(average))print("Average: " + str(average)) / print("Average:", average)
Question 3Hard8 marks
A shop gives a discount on orders. Orders of £100 or more get 20% off. Orders of £50 or more, but under £100, get 10% off. Other orders get no discount. This program should output the price to pay.
01 total = float(input("Order total: "))
02 if total >= 50:
03 discount = 0.1
04 elif total >= 100:
05 discount = 0.2
06 else:
07 discount = 0
08 pay = total * discount
09 print("Price to pay:", pay)
(a) The order total is £120. State the output of the program as it is written.[1]
(b) The program contains two logic errors. Identify each error and explain how to correct it.[4]
(c) After the errors have been corrected, the program is tested.
Give three order totals that together test every discount band, including a boundary value. For each, give the expected price to pay.[3]
Show the answer and mark scheme
(a) Answer: Price to pay: 12.0Price to pay: 12.0 (accept 12)
(b) Answer: The conditions are in the wrong order, so an order of £100 or more is caught by total >= 50: check total >= 100 first. Line 08 calculates the discount instead of the price: use pay = total * (1 - discount).
- the conditions are in the wrong order: an order of £100 or more is caught by
total >= 50 / the 20% branch can never run - check
total >= 100 first (swap the two conditions and their discounts) - line 08 calculates the amount of discount rather than the price to pay
pay = total * (1 - discount) / pay = total - total * discount
(c) Answer: For example: £30 → £30.00; £50 → £45.00; £150 → £120.00
- a total below £50 with price equal to the total
- a total from £50 to £99.99 with price = total × 0.9
- a total of £100 or more with price = total × 0.8