Intermediate Code Optimization # MCQs Practice set

Q.1 What is the main goal of intermediate code optimization?

To generate machine code directly
To improve performance and reduce resource usage
To remove syntax errors
To translate source code to assembly
Explanation - Intermediate code optimization focuses on improving the intermediate representation to make final machine code faster and smaller.
Correct answer is: To improve performance and reduce resource usage

Q.2 Which of the following is NOT a local optimization technique?

Constant folding
Common subexpression elimination
Dead code elimination
Loop unrolling
Explanation - Loop unrolling is a loop optimization (global scope), while the others are local optimizations within a basic block.
Correct answer is: Loop unrolling

Q.3 Constant folding refers to:

Eliminating dead code
Replacing expressions with constants when possible
Inlining functions
Reordering loops
Explanation - Constant folding replaces expressions involving constants with their computed result at compile time.
Correct answer is: Replacing expressions with constants when possible

Q.4 What is common subexpression elimination?

Removing duplicate variables
Reusing results of identical expressions
Eliminating unused loops
Inlining function calls
Explanation - CSE avoids recalculating the same expression by storing and reusing its result.
Correct answer is: Reusing results of identical expressions

Q.5 Which optimization eliminates code that will never be executed?

Loop unrolling
Dead code elimination
Constant propagation
Inlining
Explanation - Dead code elimination removes instructions that have no effect on the program’s outcome.
Correct answer is: Dead code elimination

Q.6 In loop optimization, loop invariant code motion involves:

Duplicating code in a loop
Moving constant calculations outside loops
Removing loops entirely
Inlining functions within loops
Explanation - Loop invariant code motion improves efficiency by moving computations that don’t depend on loop variables outside the loop.
Correct answer is: Moving constant calculations outside loops

Q.7 Strength reduction replaces:

Strong operators with weak operators
Expensive operations with cheaper ones
Large loops with smaller ones
Recursion with iteration
Explanation - Strength reduction replaces costly operations like multiplication with cheaper ones like addition.
Correct answer is: Expensive operations with cheaper ones

Q.8 Peephole optimization is applied:

Globally across functions
At the machine code level
At the high-level language stage
At the syntax analysis stage
Explanation - Peephole optimization is a local technique applied at the machine code or assembly level to replace small inefficient instruction sequences.
Correct answer is: At the machine code level

Q.9 Which of the following is an example of algebraic simplification?

Replacing i*2 with i+i
Moving code out of loops
Eliminating unused variables
Inlining small functions
Explanation - Algebraic simplification uses equivalent but faster expressions to improve efficiency.
Correct answer is: Replacing i*2 with i+i

Q.10 Which optimization technique is particularly important in nested loops?

Inlining
Loop invariant code motion
Constant folding
Dead code elimination
Explanation - In nested loops, moving invariant computations outside can greatly reduce repeated work.
Correct answer is: Loop invariant code motion

Q.11 Which optimization replaces a loop by multiple copies of its body?

Loop unrolling
Constant propagation
Code motion
Inlining
Explanation - Loop unrolling reduces loop control overhead by executing several iterations per loop cycle.
Correct answer is: Loop unrolling

Q.12 Which optimization can reduce procedure call overhead?

Inlining
Dead code elimination
Loop unrolling
Constant folding
Explanation - Inlining replaces procedure calls with the body of the procedure to avoid call overhead.
Correct answer is: Inlining

Q.13 Which optimization is useful for improving memory locality?

Loop interchange
Constant folding
Dead code elimination
Inlining
Explanation - Loop interchange changes the nesting order of loops to improve cache performance.
Correct answer is: Loop interchange

Q.14 Which optimization ensures values computed once are reused instead of recalculated?

Common subexpression elimination
Loop unrolling
Constant folding
Dead code elimination
Explanation - CSE reduces redundant calculations by reusing previously computed results.
Correct answer is: Common subexpression elimination

Q.15 Constant propagation involves:

Removing constants from loops
Replacing variables with their known constant values
Inlining constants into functions
Eliminating constant expressions
Explanation - Constant propagation substitutes variables whose values are constant at compile time with those constants.
Correct answer is: Replacing variables with their known constant values

Q.16 Which optimization can convert multiplication by a power of 2 into a shift operation?

Constant propagation
Strength reduction
Common subexpression elimination
Loop interchange
Explanation - Strength reduction transforms costly operations into equivalent but cheaper ones, such as using shifts instead of multiplications.
Correct answer is: Strength reduction

Q.17 What is the role of control flow analysis in optimization?

To simplify algebraic expressions
To detect unreachable code and optimize branches
To inline functions
To fold constants
Explanation - Control flow analysis helps optimize programs by identifying dead code and improving branch handling.
Correct answer is: To detect unreachable code and optimize branches

Q.18 What is loop fusion?

Splitting loops into smaller loops
Combining adjacent loops with the same range
Unrolling loop iterations
Inlining loop bodies
Explanation - Loop fusion combines loops with the same iteration range to reduce loop overhead and improve cache locality.
Correct answer is: Combining adjacent loops with the same range

Q.19 Dead store elimination removes:

Unused function calls
Assignments whose results are never used
Constants from loops
Unreachable loops
Explanation - Dead store elimination removes writes to variables that are not subsequently read.
Correct answer is: Assignments whose results are never used

Q.20 Which type of optimization is applied within a single basic block?

Global optimization
Loop optimization
Local optimization
Interprocedural optimization
Explanation - Local optimization is applied inside a basic block without considering control flow between blocks.
Correct answer is: Local optimization

Q.21 Which of the following is a machine-independent optimization?

Instruction scheduling
Register allocation
Constant folding
Pipeline optimization
Explanation - Constant folding is independent of the target machine and is applied to intermediate code.
Correct answer is: Constant folding

Q.22 Machine-dependent optimizations include:

Strength reduction
Loop unrolling
Instruction scheduling
Constant folding
Explanation - Instruction scheduling depends on the target machine’s architecture to improve pipeline efficiency.
Correct answer is: Instruction scheduling

Q.23 Which optimization technique reduces the number of temporaries in intermediate code?

Dead code elimination
Copy propagation
Loop unrolling
Inlining
Explanation - Copy propagation replaces occurrences of variables assigned from others to reduce temporary variables.
Correct answer is: Copy propagation

Q.24 Which optimization is helpful in vectorization?

Loop interchange
Constant folding
Inlining
Common subexpression elimination
Explanation - Loop interchange enables vectorization by arranging loops to process data in memory-friendly order.
Correct answer is: Loop interchange

Q.25 Which optimization replaces a sequence of instructions with a more efficient equivalent sequence?

Peephole optimization
Dead code elimination
Loop invariant code motion
Inlining
Explanation - Peephole optimization inspects small instruction sequences and replaces them with faster equivalents.
Correct answer is: Peephole optimization