Q.1 Which of the following is the main goal of code optimization in a compiler?
Reduce memory usage
Increase execution speed
Improve readability of code
Simplify compiler design
Explanation - Code optimization primarily focuses on producing efficient target code that executes faster, though sometimes it also reduces memory usage.
Correct answer is: Increase execution speed
Q.2 Peephole optimization is a type of:
Global optimization
Local optimization
Loop optimization
Machine-independent optimization
Explanation - Peephole optimization examines a small set of consecutive instructions (a 'peephole') and replaces them with more efficient ones, making it a local optimization.
Correct answer is: Local optimization
Q.3 Dead code elimination removes:
Frequently executed code
Code that does not affect program output
Unreachable functions
Initialization code
Explanation - Dead code is code that executes but its results are never used; eliminating it improves efficiency without changing semantics.
Correct answer is: Code that does not affect program output
Q.4 Which optimization technique removes unnecessary recomputation of values inside loops?
Loop fusion
Loop invariant code motion
Loop unrolling
Constant folding
Explanation - Loop invariant code motion identifies computations that produce the same result in every iteration and moves them outside the loop.
Correct answer is: Loop invariant code motion
Q.5 Constant folding refers to:
Combining two loops into one
Replacing expressions with precomputed constant values
Expanding loops for speed
Folding machine instructions together
Explanation - In constant folding, expressions with constant operands are evaluated at compile time to simplify runtime computation.
Correct answer is: Replacing expressions with precomputed constant values
Q.6 Which optimization helps reduce function call overhead by substituting code directly at the call site?
Inlining
Constant propagation
Register allocation
Strength reduction
Explanation - Inlining replaces a function call with the function’s body, saving the overhead of parameter passing and return handling.
Correct answer is: Inlining
Q.7 Which is a machine-independent optimization?
Register allocation
Instruction scheduling
Common subexpression elimination
Pipeline optimization
Explanation - Common subexpression elimination is independent of hardware as it focuses on reducing redundant computations in the source code.
Correct answer is: Common subexpression elimination
Q.8 Strength reduction is used to:
Increase loop iterations
Replace expensive operations with cheaper ones
Remove redundant computations
Simplify control flow
Explanation - Strength reduction replaces costly operations (like multiplication) with cheaper ones (like addition) without changing program results.
Correct answer is: Replace expensive operations with cheaper ones
Q.9 Which optimization technique tries to improve cache performance by restructuring loop nests?
Loop unrolling
Loop interchange
Loop invariant motion
Dead code elimination
Explanation - Loop interchange reorders nested loops to improve memory locality and cache efficiency.
Correct answer is: Loop interchange
Q.10 The term 'basic block' in code optimization refers to:
A group of independent functions
A straight-line code sequence with no branches except entry and exit
A memory block allocated for arrays
A set of variables used together
Explanation - Basic blocks are sequences of instructions with a single entry and single exit point, making them useful units for optimization.
Correct answer is: A straight-line code sequence with no branches except entry and exit
Q.11 Which optimization is generally performed before register allocation?
Dead code elimination
Instruction scheduling
Pipeline optimization
Register coloring
Explanation - Eliminating dead code before register allocation ensures fewer variables compete for registers, improving allocation efficiency.
Correct answer is: Dead code elimination
Q.12 What is the main advantage of loop unrolling?
Reduces code size
Reduces loop overhead and increases instruction-level parallelism
Simplifies code readability
Reduces compile time
Explanation - Loop unrolling replicates the loop body multiple times, reducing iteration overhead and exposing more instructions for parallel execution.
Correct answer is: Reduces loop overhead and increases instruction-level parallelism
Q.13 Common subexpression elimination eliminates:
Duplicate variables
Repeated expressions with the same operands
Unreachable code
Excessive function calls
Explanation - If an expression is computed multiple times with unchanged operands, common subexpression elimination reuses the stored result.
Correct answer is: Repeated expressions with the same operands
Q.14 Copy propagation replaces:
Constant values with variables
Variables with their assigned values
Redundant loops with conditions
Functions with inlined code
Explanation - Copy propagation substitutes variables that only copy another variable with the actual value, simplifying expressions.
Correct answer is: Variables with their assigned values
Q.15 Which optimization reduces the number of times array bounds are checked?
Loop invariant motion
Bounds check elimination
Constant folding
Copy propagation
Explanation - Bounds check elimination ensures that array indices remain safe, allowing removal of repeated runtime checks inside loops.
Correct answer is: Bounds check elimination
Q.16 Instruction scheduling is primarily used to:
Reduce memory footprint
Hide latencies and improve parallelism
Remove dead code
Simplify syntax trees
Explanation - Instruction scheduling reorders instructions without altering program semantics, reducing stalls and increasing CPU pipeline utilization.
Correct answer is: Hide latencies and improve parallelism
Q.17 Which type of optimization can potentially increase code size while improving speed?
Inlining
Dead code elimination
Copy propagation
Constant folding
Explanation - Inlining replaces function calls with the function body, which speeds up execution but may increase code size.
Correct answer is: Inlining
Q.18 What does loop fusion do?
Splits loops into smaller loops
Merges adjacent loops with the same range
Moves invariants outside loops
Eliminates redundant loops
Explanation - Loop fusion combines loops iterating over the same range, reducing loop overhead and improving locality.
Correct answer is: Merges adjacent loops with the same range
Q.19 Which optimization is done at the intermediate code level?
Dead code elimination
Cache optimization
Pipeline scheduling
Register spilling
Explanation - Intermediate code optimizations include common subexpression elimination, copy propagation, and dead code elimination.
Correct answer is: Dead code elimination
Q.20 Strength reduction commonly replaces multiplication by:
Division
Addition
Exponentiation
Bitwise XOR
Explanation - For example, replacing 'i * 2' with 'i + i' reduces computation cost.
Correct answer is: Addition
Q.21 Which optimization works by identifying computations whose results can be reused?
Common subexpression elimination
Loop unrolling
Inlining
Dead code elimination
Explanation - This optimization detects repeated computations and replaces them with previously computed results, saving time.
Correct answer is: Common subexpression elimination
Q.22 Global optimizations differ from local optimizations in that they:
Work on a basic block only
Work across multiple basic blocks or functions
Only eliminate constants
Focus solely on machine instructions
Explanation - Global optimizations consider control flow across multiple basic blocks or procedures, unlike local optimizations that restrict themselves to one block.
Correct answer is: Work across multiple basic blocks or functions
Q.23 Which of the following is NOT a loop optimization?
Loop unrolling
Loop invariant code motion
Loop interchange
Constant folding
Explanation - Constant folding applies to constant expressions, not specifically to loops, making it unrelated to loop optimization.
Correct answer is: Constant folding
Q.24 Which optimization technique attempts to minimize the number of variables stored in memory?
Loop fusion
Register allocation
Dead code elimination
Constant folding
Explanation - Register allocation maps frequently used variables to CPU registers, reducing memory accesses and improving speed.
Correct answer is: Register allocation
Q.25 Which is a disadvantage of aggressive code optimization?
Reduced execution speed
Increased code size
Improved cache usage
Simpler debugging
Explanation - Optimizations like inlining and unrolling may improve execution speed but can increase the size of the generated code.
Correct answer is: Increased code size
