Syntax errors and run-time errors reported by translators

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Syntax errors and run-time errors reported by translators

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line contains a syntax error? A: print("Hello" B: print("Hello")
A A: print("Hello" (missing closing bracket)
B B: print("Hello")
2. A student types pritn("Hi") instead of print("Hi"). What kind of error is this?
A Run-time error
B Syntax error (misspelt keyword)
3. What does a translator usually report when it finds a syntax error?
A Nothing, because syntax errors are never reported
B The error and the line number where it was found
4. A program runs, then crashes on the line result = 10 / 0. What type of error is this?
A Run-time error (dividing by zero)
B Syntax error
5. When does a run-time error happen?
A Only while the program is running
B Before the program starts, while the code is being translated

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1print("Hi" ASyntax error: the colon is missing after the condition. The translator reports it and shows the line before the program runs.
2result = 10 / 0 Bpritn("Score")
3A misspelt keyword breaks the rules of the language, so the translator reports a syntax error CA run-time error: the code is valid, but the program crashes when this line runs
4A logic error gives the wrong output but no error message, because the code follows the rules DA syntax error: the closing bracket is missing, so the translator reports the error and its line number
5if age > 18 print("Adult") Eaverage = a + b / 2
6total = 5 print(totl) FRun-time error: the variable name is misspelt, so the program stops when this line runs. The fix is to type total.

Definitions

syntax error

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
# "iff x > 5:" is a syntax error __ x > 5: print("Big")
2.
print("Total" # Translator reports: ______ error, line 1
3.
a = 10 b = 0 print(a _ b) # run-time error
4.
print(10 / 0) # ________ error, not syntax
5.
# Want the average of 4 and 6 print(4 + 6 / 2) # _____ error: prints 7

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A name = input("Name: "
B print("Hello " + name)
C print("Welcome")
Question 2
A age = 15
B limit = 18
C if age < limit
D print("Too young")
Question 3
A x = 10
B y = 2
C whlie x > y:
D x = x - y
Question 4
A total = 100
B people = 0
C share = total / people
D print(share)
Question 5
A a = 6
B b = 8
C avg = a + b / 2
D print(avg)

Answer Key

Syntax errors and run-time errors reported by translators
Section A: Quick Questions
Q1
A — A: print("Hello" (missing closing bracket)
Q2
B — Syntax error (misspelt keyword)
Q3
B — The error and the line number where it was found
Q4
A — Run-time error (dividing by zero)
Q5
A — Only while the program is running
Section B: Match the Pairs
Q1
print("Hi" → A syntax error: the closing bracket is missing, so the translator reports the error and its line number
Q2
result = 10 / 0 → A run-time error: the code is valid, but the program crashes when this line runs
Q3
A misspelt keyword breaks the rules of the language, so the translator reports a syntax error → pritn("Score")
Q4
A logic error gives the wrong output but no error message, because the code follows the rules → average = a + b / 2
Q5
if age > 18 print("Adult") → Syntax error: the colon is missing after the condition. The translator reports it and shows the line before the program runs.
Q6
total = 5 print(totl) → Run-time error: the variable name is misspelt, so the program stops when this line runs. The fix is to type total.
Section C: Definitions
Q1
syntax error — Code that breaks the rules of the language, like a missing bracket. The translator reports it and its line. A run-time error only occurs when the program runs.
Section D: Fill in the Blanks
Q1
# "iff x > 5:" is a syntax error if x > 5: print("Big")
Q2
print("Total" # Translator reports: syntax error, line 1
Q3
a = 10 b = 0 print(a / b) # run-time error
Q4
print(10 / 0) # run-time error, not syntax
Q5
# Want the average of 4 and 6 print(4 + 6 / 2) # logic error: prints 7
Section E: Spot the Error
Q1
A — The closing bracket is missing, which breaks the rules of the language. This is a syntax error and the translator reports it with its line number. It should be name = input("Name: ").
Q2
C — An if statement needs a colon at the end of the line. Without it the translator reports a syntax error on this line. It should be if age < limit:.
Q3
C — The keyword while is misspelt. The translator does not recognise it and reports a syntax error on this line. It should be while x > y:.
Q4
B — With people set to 0, line 3 divides by zero. The code follows the rules of the language, so there is no syntax error. The program crashes with a run-time error only when it runs. It should be people = 4.
Q5
C — Without brackets, b / 2 is worked out first, so the answer is wrong. This is a logic error: the program runs and no error message appears, but the output is incorrect. It should be avg = (a + b) / 2.