What is the main purpose of code generation in a compiler?
The main purpose of code generation is to translate the intermediate representation of a program into executable machine code that a computer can run directly.
Click to reveal answer
beginner
How does code generation ensure the output is executable?
Code generation produces output in a format that matches the target machine's instruction set, so the output can be loaded and run by the computer's processor.
Click to reveal answer
intermediate
What role does the target machine architecture play in code generation?
The target machine architecture defines the instructions and formats that code generation must produce to create executable code compatible with that machine.
Click to reveal answer
intermediate
Why can't code generation produce output in a high-level language?
Because the goal is to create executable output, code generation must produce low-level machine code or assembly, which the computer hardware can directly execute, unlike high-level languages which need further translation.
Click to reveal answer
intermediate
What is the difference between code generation and interpretation regarding executable output?
Code generation creates a standalone executable file that runs directly on hardware, while interpretation executes code line-by-line without producing a separate executable file.
Click to reveal answer
What does code generation produce in a compiler?
ASyntax trees
BHigh-level source code
CComments and documentation
DExecutable machine code
✗ Incorrect
Code generation produces executable machine code that the computer can run directly.
Why must code generation match the target machine's instruction set?
ATo ensure the output can be executed by the processor
BTo make the code easier to read
CTo improve compilation speed
DTo generate comments
✗ Incorrect
Matching the target machine's instruction set ensures the output is executable by that machine.
Which of these is NOT a reason code generation produces executable output?
ATo allow the program to run on hardware
BTo create a file the operating system can load
CTo generate human-readable code
DTo translate intermediate code into machine instructions
✗ Incorrect
Code generation focuses on machine instructions, not human-readable code.
What is the difference between code generation and interpretation?
5. A compiler generates executable machine code from source code. If the source code uses a new processor feature not supported by the target hardware, what will happen during code generation and execution?
hard
A. The compiler will produce an error during lexical analysis
B. Code generation will automatically replace the new feature with older instructions
C. Code generation will produce machine code using the new feature, but execution will fail on unsupported hardware
D. The executable will run correctly on all hardware
Solution
Step 1: Understand code generation with new hardware features
The compiler generates machine code using instructions matching the source code, including new processor features.
Step 2: Analyze execution on unsupported hardware
If the hardware does not support these new instructions, the executable will fail to run properly.
Final Answer:
Code generation will produce machine code using the new feature, but execution will fail on unsupported hardware -> Option C
Quick Check:
New feature code + unsupported hardware = execution failure [OK]
Hint: New hardware features need matching hardware to run [OK]
Common Mistakes:
Assuming compiler replaces new features automatically
Thinking lexical analysis detects hardware features
Believing executables run on all hardware regardless
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.