10 MCQs related to Code Optimization

10 Multiple Choice Questions (MCQs) related to Code Optimization, useful for UGC NET, SET, and GATE exams:


1. What is the main purpose of code optimization in a compiler?

A) Convert source code to machine code
B) Improve the execution speed and reduce resource usage
C) Detect syntax errors
D) Allocate memory for variables

Explanation:
Code optimization aims to enhance the performance of the compiled code by reducing execution time, memory usage, and power consumption.

Answer: B) Improve the execution speed and reduce resource usage


2. Which of the following is NOT a type of code optimization?

A) Peephole Optimization
B) Loop Optimization
C) Syntax Checking
D) Constant Folding

Explanation:
Syntax Checking is part of syntax analysis, not code optimization.

Answer: C) Syntax Checking


3. Which of the following is an example of local optimization?

A) Loop Unrolling
B) Constant Folding
C) Register Allocation
D) Dead Code Elimination

Explanation:
Local optimization focuses on small portions of code, such as constant folding, which replaces constant expressions with their computed values at compile time.

Answer: B) Constant Folding


4. What does Dead Code Elimination (DCE) do?

A) Removes redundant instructions
B) Eliminates unreachable code that has no effect on program output
C) Removes comments from the code
D) Converts source code into an intermediate representation

Explanation:
Dead Code Elimination removes instructions that are never executed or whose results are never used, improving efficiency.

Answer: B) Eliminates unreachable code that has no effect on program output


5. Which optimization technique reduces the overhead of loop control statements?

A) Loop Unrolling
B) Constant Propagation
C) Common Subexpression Elimination
D) Strength Reduction

Explanation:
Loop Unrolling reduces the number of iterations by expanding the loop body, decreasing branching overhead.

Answer: A) Loop Unrolling


6. What is the purpose of Register Allocation in code optimization?

A) Convert high-level code to low-level code
B) Allocate variables to CPU registers to reduce memory access
C) Remove redundant computations
D) Detect semantic errors

Explanation:
Register allocation assigns frequently used variables to CPU registers, reducing memory access time and improving efficiency.

Answer: B) Allocate variables to CPU registers to reduce memory access


7. Which of the following optimization techniques replaces expensive operations with equivalent but cheaper operations?

A) Peephole Optimization
B) Strength Reduction
C) Inline Expansion
D) Code Hoisting

Explanation:
Strength Reduction replaces expensive operations (e.g., multiplication) with simpler ones (e.g., addition or bit-shifting).

Answer: B) Strength Reduction


8. What does Common Subexpression Elimination (CSE) do?

A) Removes unused variables
B) Identifies and eliminates repeated computations
C) Reduces the number of loops
D) Converts code into an abstract syntax tree

Explanation:
CSE finds repeated expressions in a program and computes them only once, storing the result for reuse.

Answer: B) Identifies and eliminates repeated computations


9. What is Global Optimization in compilers?

A) Optimization applied to a small block of code
B) Optimization applied across the entire program
C) Optimization applied only at the assembly level
D) Syntax analysis improvement

Explanation:
Global optimization analyzes the entire program to apply transformations like loop optimizations and dead code elimination across function boundaries.

Answer: B) Optimization applied across the entire program


10. What is the main goal of Peephole Optimization?

A) Optimize a small set of adjacent instructions
B) Reduce recursion in function calls
C) Improve parsing speed
D) Increase compilation time

Explanation:
Peephole optimization scans small sequences of instructions and replaces them with faster or more efficient alternatives.

Answer: A) Optimize a small set of adjacent instructions

Comments