quiz Code Generation and Optimization

Code Generation and Optimization in Compiler Design

1. What is the focus of code generation in a compiler?

  • A) Analyzing the source code for syntax errors
  • B) Generating intermediate code from high-level language
  • C) Translating intermediate code into machine code
  • D) Optimizing the source code for better performance
C) Translating intermediate code into machine code Explanation

2. What kind of optimization moves computation out of a frequently executed loop?

  • A) Dead-code elimination
  • B) Constant propagation
  • C) Loop unrolling
  • D) Code motion
D) Code motion Explanation

3. What is the primary goal of register allocation in code generation?

  • A) To optimize the size of the intermediate representation
  • B) To minimize the number of memory accesses
  • C) To reorder instructions for better performance
  • D) To ensure all instructions are machine-independent
B) To minimize the number of memory accesses Explanation

4. Which of the following is a machine-dependent optimization technique?

  • A) Dead code elimination
  • B) Loop unrolling
  • C) Constant folding
  • D)  Instruction scheduling
 D) Instruction scheduling Explanation

5. What does “strength reduction” refer to in code optimization?

  • A) Replacing expensive operations with cheaper ones
  • B) Reducing the strength of the compiler optimization pass
  • C) Minimizing the number of instructions in a loop
  • D) Enhancing the strength of register allocation
A) Replacing expensive operations with cheaper ones Explanation