0
0
Intro to Computingfundamentals~20 mins

How programs are compiled or interpreted in Intro to Computing - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Compilation and Interpretation
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main difference between a compiler and an interpreter?

Imagine you want to translate a book from one language to another. Which of the following best describes how a compiler and an interpreter work?

AA compiler translates the entire book at once before you read it; an interpreter translates the book sentence by sentence as you read.
BA compiler translates only the first chapter; an interpreter translates the whole book at once.
CA compiler translates the book into pictures; an interpreter translates it into sounds.
DA compiler reads the book aloud; an interpreter writes the book down.
Attempts:
2 left
💡 Hint

Think about when the translation happens: all at once or bit by bit?

trace
intermediate
2:00remaining
Trace the output of an interpreted program

Consider this simple program interpreted line by line. What will be printed?

Intro to Computing
x = 5
print(x)
x = x + 3
print(x)
A5\n5
B8\n8
C8\n5
D5\n8
Attempts:
2 left
💡 Hint

Follow the program line by line and note the value of x at each print.

Comparison
advanced
2:00remaining
Which program runs faster and why?

Two programs do the same task. One is compiled, the other interpreted. Which runs faster and why?

AThe interpreted program runs faster because it skips some code lines.
BBoth run at the same speed because they do the same task.
CThe compiled program runs faster because it is translated into machine code before running.
DThe interpreted program runs faster because it translates code as it runs.
Attempts:
2 left
💡 Hint

Think about when the translation happens and how that affects speed.

identification
advanced
2:00remaining
Identify the step where compilation happens in this flowchart

Look at the flowchart below describing how a program runs. Which step is the compilation step?


Flowchart showing: Source Code -> Compiler -> Machine Code -> Execution
ACompiler
BSource Code
CMachine Code
DExecution
Attempts:
2 left
💡 Hint

The compiler is the tool that translates source code into machine code.

🧠 Conceptual
expert
3:00remaining
Why might a language use both compilation and interpretation?

Some programming languages use both compilation and interpretation. Why would this be done?

ABecause the compiler cannot translate all code, so the interpreter finishes the job.
BTo combine fast execution from compilation with flexibility from interpretation.
CTo confuse programmers and make debugging harder.
DBecause interpretation is always faster but less accurate than compilation.
Attempts:
2 left
💡 Hint

Think about the benefits of each method and how they might complement each other.