Code Generation # MCQs Practice set

Q.1 Which phase of the compiler is responsible for converting intermediate code to target machine code?

Lexical Analysis
Syntax Analysis
Code Generation
Semantic Analysis
Explanation - Code Generation is the phase where the intermediate code is translated into target machine code or assembly code.
Correct answer is: Code Generation

Q.2 In code generation, the term 'target code' usually refers to:

Source code
Intermediate code
Machine or assembly code
High-level language code
Explanation - Target code refers to machine-level or assembly instructions that can be executed on the hardware.
Correct answer is: Machine or assembly code

Q.3 A good code generator should produce code that is:

Correct and efficient
Incorrect but optimized
Readable and verbose
Only minimal in size
Explanation - Correctness and efficiency are the two main goals of a good code generator.
Correct answer is: Correct and efficient

Q.4 Which data structure is most commonly used during register allocation in code generation?

Graph
Stack
Queue
Tree
Explanation - Graph coloring is widely used for register allocation in code generation.
Correct answer is: Graph

Q.5 What is the main purpose of instruction selection in code generation?

Selects source code functions
Maps intermediate code to target instructions
Chooses registers for variables
Optimizes syntax trees
Explanation - Instruction selection maps the intermediate representation to actual target machine instructions.
Correct answer is: Maps intermediate code to target instructions

Q.6 Which of the following is NOT a concern of the code generation phase?

Instruction selection
Register allocation
Parsing grammar
Instruction ordering
Explanation - Parsing grammar is handled during syntax analysis, not code generation.
Correct answer is: Parsing grammar

Q.7 Register allocation helps in:

Reducing memory access
Speeding up parsing
Generating tokens
Improving lexical analysis
Explanation - By efficiently allocating registers, memory accesses are reduced and execution is faster.
Correct answer is: Reducing memory access

Q.8 The technique of using temporary registers for intermediate results is called:

Instruction selection
Register spilling
Register allocation
Three-address code
Explanation - Register allocation assigns variables and intermediate values to CPU registers.
Correct answer is: Register allocation

Q.9 Which type of code is closest to machine code but still abstracted for portability?

Intermediate code
Source code
Assembly code
High-level code
Explanation - Intermediate code provides abstraction for portability while being closer to machine instructions.
Correct answer is: Intermediate code

Q.10 The DAG (Directed Acyclic Graph) representation helps in:

Register allocation
Instruction scheduling
Eliminating common sub-expressions
Parsing expressions
Explanation - DAG helps in optimizing code by identifying and eliminating repeated computations.
Correct answer is: Eliminating common sub-expressions

Q.11 In code generation, 'peephole optimization' is applied to:

Large code blocks
Symbol tables
Small windows of instructions
Syntax trees
Explanation - Peephole optimization looks at small sets of instructions to optimize them.
Correct answer is: Small windows of instructions

Q.12 Which type of instructions are preferred in code generation for efficiency?

High-level
Complex multi-step
Simple machine instructions
Ambiguous instructions
Explanation - Simple instructions are efficient and less error-prone for execution.
Correct answer is: Simple machine instructions

Q.13 Which machine model is often assumed for code generation?

Load-store architecture
Multi-threaded model
Quantum model
Finite automata
Explanation - Load-store architecture simplifies code generation and is commonly used as a model.
Correct answer is: Load-store architecture

Q.14 The output of code generation is usually:

Tokens
Parse tree
Target machine code
Intermediate code
Explanation - The final output of code generation is the target machine code that can be executed.
Correct answer is: Target machine code

Q.15 Instruction scheduling in code generation primarily improves:

Compilation speed
Runtime performance
Lexical analysis
Syntax analysis
Explanation - Instruction scheduling reduces pipeline stalls and improves execution performance.
Correct answer is: Runtime performance

Q.16 Which optimization reduces the need for memory access during code generation?

Constant folding
Register allocation
Loop unrolling
Dead code elimination
Explanation - Register allocation minimizes memory access by storing values in registers.
Correct answer is: Register allocation

Q.17 Spilling in register allocation means:

Using registers more efficiently
Storing variables temporarily in memory
Eliminating redundant variables
Freeing all registers
Explanation - When registers are insufficient, some variables are stored in memory, known as spilling.
Correct answer is: Storing variables temporarily in memory

Q.18 A code generator should avoid:

Redundant instructions
Efficient registers
Instruction scheduling
Target code generation
Explanation - Redundant instructions increase code size and execution time.
Correct answer is: Redundant instructions

Q.19 Which of the following is an important criterion in code generation?

Minimizing execution time
Increasing source code readability
Adding semantic checks
Improving syntax parsing
Explanation - Generated code should run efficiently, hence minimizing execution time is a key goal.
Correct answer is: Minimizing execution time

Q.20 Intermediate code simplifies code generation because it is:

Platform dependent
High-level language
Machine independent
Error free
Explanation - Intermediate code is machine independent, which makes it easier to generate target code for different platforms.
Correct answer is: Machine independent

Q.21 What is the main challenge in code generation?

Lexical analysis
Instruction selection and register allocation
Syntax parsing
Symbol table management
Explanation - Choosing efficient instructions and managing registers are the biggest challenges in code generation.
Correct answer is: Instruction selection and register allocation

Q.22 Which of these is a technique used in instruction selection?

Parsing
Pattern matching
Symbol resolution
Lexing
Explanation - Instruction selection often uses pattern matching between intermediate code and machine instructions.
Correct answer is: Pattern matching

Q.23 Code generation should ensure that the target program is:

Readable
Optimized and correct
Verbose
Machine-independent
Explanation - The target code should be both correct and optimized for efficiency.
Correct answer is: Optimized and correct

Q.24 Which of these optimizations is most closely related to code generation?

Dead code elimination
Constant propagation
Peephole optimization
Common subexpression elimination
Explanation - Peephole optimization is directly applied during code generation to improve efficiency of small instruction sequences.
Correct answer is: Peephole optimization

Q.25 Which register is typically used for storing return addresses during code generation?

Accumulator
Stack pointer
Link register
Base pointer
Explanation - Many architectures use a link register to store the return address of function calls.
Correct answer is: Link register