Advantages of a compiler

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

Advantages of a compiler

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why does a compiled program usually run faster than an interpreted one?
A The translation was already done before it runs, so no translating happens at run time
B The compiler translates each line again every time the program runs
2. A company gives customers a compiled executable file. What do the customers NOT need?
A A copy of the compiler
B A copy of the executable file
3. Which is an advantage of distributing a program as an executable file?
A The source code stays private
B Customers can easily read and edit the source code
4. Which statement about compiler error reporting is correct?
A The compiler stops at the first error and says nothing about the rest
B The compiler gives a report of the errors found in the whole program
5. Which is a true advantage of a compiler?
A The program must be translated again every time it is run
B Once compiled, the program can run again and again without being translated

Match the Pairs

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

Left columnRight column
1game.exe ALine-by-line translation at run time is how an interpreter works. A compiled program is translated once beforehand, so it runs faster.
2compiler.translate(source) -> report Bgive users: game.exe (keep game.py)
3A compiled program runs faster because no translation happens while it is running CAn executable does not need the compiler to run. The translation was already done, so the compiler is not needed on the user's computer.
4Source code stays private because only the executable is given to users DThe compiler translates the whole program and then gives a report of all the errors it found
5Users must install the compiler to run game.exe Etranslate once -> run many times
6Compiled programs run slowly because they are translated line by line each time FAn executable file made by a compiler that can run on its own, without the compiler or the source code

Definitions

compiled program

Fill in the Blanks

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

1.
EXE ← _______("game.c")
2.
RUN("game.___")
3.
app ← COMPILE("game.c") _____(app)
4.
// The compiler reports ___ errors at once
5.
IF compiled = ____ THEN OUTPUT "Runs faster" ENDIF

Spot the Error

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

Question 1
A c_time = 2
B i_time = 5
C print(c_time, i_time)
D print(c_time > i_time)
Question 2
A compile("game.c", "game.exe")
B uninstall(compiler)
C run("game.c")
Question 3
A exe = interpret("game.c")
B run(exe)
C print("Already translated")
Question 4
A errors = compile_all("g.c")
B if len(errors) < 0:
C print("Fix:", errors)
Question 5
A translate_at_runtime = True
B if translate_at_runtime:
C speed = "slower"
D else:
E speed = "faster"

Answer Key

Advantages of a compiler
Section A: Quick Questions
Q1
A — The translation was already done before it runs, so no translating happens at run time
Q2
A — A copy of the compiler
Q3
A — The source code stays private
Q4
B — The compiler gives a report of the errors found in the whole program
Q5
B — Once compiled, the program can run again and again without being translated
Section B: Match the Pairs
Q1
game.exe → An executable file made by a compiler that can run on its own, without the compiler or the source code
Q2
compiler.translate(source) -> report → The compiler translates the whole program and then gives a report of all the errors it found
Q3
A compiled program runs faster because no translation happens while it is running → translate once -> run many times
Q4
Source code stays private because only the executable is given to users → give users: game.exe (keep game.py)
Q5
Users must install the compiler to run game.exe → An executable does not need the compiler to run. The translation was already done, so the compiler is not needed on the user's computer.
Q6
Compiled programs run slowly because they are translated line by line each time → Line-by-line translation at run time is how an interpreter works. A compiled program is translated once beforehand, so it runs faster.
Section C: Definitions
Q1
compiled program — A program translated all at once into an executable. It runs faster, needs no compiler to run, keeps the source code private, and gives a whole-program error report.
Section D: Fill in the Blanks
Q1
EXE ← COMPILE("game.c")
Q2
RUN("game.exe")
Q3
app ← COMPILE("game.c") SHARE(app)
Q4
// The compiler reports all errors at once
Q5
IF compiled = TRUE THEN OUTPUT "Runs faster" ENDIF
Section E: Spot the Error
Q1
D — A compiled program has no translation at run time, so it runs faster and takes less time. The check should be c_time < i_time (compiled time is smaller), not >.
Q2
C — The executable runs without the compiler. The source file game.c cannot be run directly once the compiler is gone. The line should be run("game.exe").
Q3
A — A compiler translates the whole program once and creates an executable file. An interpreter does not create one. Line 1 should be exe = compile("game.c").
Q4
B — A compiler gives a whole-program error report. A list length can never be less than 0, so the errors would never be shown. It should be if len(errors) > 0:.
Q5
A — A compiled program was translated before it runs, so no translation happens at run time. The value should be False, which gives the faster result.