Interpreter

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

Interpreter

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement describes how an interpreter works?
A It translates one line, runs it, then moves to the next line
B It translates the whole program first, then runs it all at once
2. Does an interpreter produce an executable file?
A Yes, it saves a standalone executable file for later
B No, it runs the code directly and makes no executable file
3. A student gives a friend a Python program to run using an interpreter. What must the friend have?
A The source code and an interpreter
B Only the source code, because it is already translated
4. A program is run 5 times using an interpreter. How many times is the code translated?
A 5 times, once on every run
B Once only, and the translation is reused
5. What type of program does an interpreter translate?
A A machine code program that is already ready to run
B A high-level language program

Match the Pairs

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

Left columnRight column
1Line 1 translated -> Line 1 runs -> Line 2 translated -> Line 2 runs ARun 1: translate lines 1-3 and run Run 2: translate lines 1-3 and run
2python game.py Bgame.py + interpreter -> output (no game.exe created)
3An interpreter produces no executable file, so the source code is still needed on every run CThe interpreter is needed on every run, because nothing is saved and the source code is translated again each time.
4Translation repeats on every run, because the interpreter translates each line again every time the program is run DThis describes a compiler. An interpreter translates and runs one line at a time and makes no executable file.
5An interpreter translates the whole program first and then creates an executable file to run EAn interpreter translates one line and executes it straight away, then moves on to the next line
6After the first run, the interpreter is no longer needed because the program has been translated FThe interpreter is started and given the source code, so it translates and runs game.py every time

Definitions

interpreter

Fill in the Blanks

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

1.
___ line in program: translate(line) execute(line)
2.
for line in program: _________(line) execute(line)
3.
for line in program: translate(line) _______(line)
4.
exeFileCreated = _____
5.
runProgram(sourceCode, ___________)

Spot the Error

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

Question 1
A COMPILE all lines into a file
B TRANSLATE that line
C RUN that line straight away
D MOVE to the next line
Question 2
A READ next line of source code
B TRANSLATE every line at once
C RUN it straight away
Question 3
A IF lines remain in source
B TRANSLATE the next line
C RUN the line
D ENDWHILE
Question 4
A LOAD source and interpreter
B TRANSLATE and RUN each line
C SAVE executable file to disk
D OUTPUT results on screen
Question 5
A print('Start')
B print('Middle')
C print('End)

Answer Key

Interpreter
Section A: Quick Questions
Q1
A — It translates one line, runs it, then moves to the next line
Q2
B — No, it runs the code directly and makes no executable file
Q3
A — The source code and an interpreter
Q4
A — 5 times, once on every run
Q5
B — A high-level language program
Section B: Match the Pairs
Q1
Line 1 translated -> Line 1 runs -> Line 2 translated -> Line 2 runs → An interpreter translates one line and executes it straight away, then moves on to the next line
Q2
python game.py → The interpreter is started and given the source code, so it translates and runs game.py every time
Q3
An interpreter produces no executable file, so the source code is still needed on every run → game.py + interpreter -> output (no game.exe created)
Q4
Translation repeats on every run, because the interpreter translates each line again every time the program is run → Run 1: translate lines 1-3 and run Run 2: translate lines 1-3 and run
Q5
An interpreter translates the whole program first and then creates an executable file to run → This describes a compiler. An interpreter translates and runs one line at a time and makes no executable file.
Q6
After the first run, the interpreter is no longer needed because the program has been translated → The interpreter is needed on every run, because nothing is saved and the source code is translated again each time.
Section C: Definitions
Q1
interpreter — A translator that translates a high-level program one line at a time and runs each line straight away. It makes no executable file, so the source code and interpreter are needed on every run.
Section D: Fill in the Blanks
Q1
for line in program: translate(line) execute(line)
Q2
for line in program: translate(line) execute(line)
Q3
for line in program: translate(line) execute(line)
Q4
exeFileCreated = False
Q5
runProgram(sourceCode, interpreter)
Section E: Spot the Error
Q1
A — An interpreter does not compile the whole program into a file. It reads one line at a time, so line 1 should be 'READ the first line of source'.
Q2
B — An interpreter translates one line at a time, not every line at once. Line 2 should be 'TRANSLATE that one line'.
Q3
A — The interpreter repeats translate-and-run for every line, so it needs a WHILE loop. An IF would only handle one line.
Q4
C — An interpreter produces no executable file. Only a compiler does that. Line 3 should say that no executable file is created.
Q5
C — The closing quote is missing, which is a syntax error. The interpreter would run lines 1 and 2, then stop and report an error at line 3. Fix: print('End').