1. What is the main purpose of peephole optimization?
A) To replace high-level code with machine code
B) To improve the readability of code
C) To reduce the number of instructions in the generated code by replacing small instruction sequences
D) To convert machine code into assembly code
Answer: C) To reduce the number of instructions in the generated code by replacing small instruction sequences
2. Which of the following is an example of a peephole optimization?
A) Loop unrolling
B) Removing redundant instructions like consecutive moves or loads
C) Register allocation
D) Strength reduction
Answer: B) Removing redundant instructions like consecutive moves or loads
3. Peephole optimization generally operates on which of the following?
A) The entire program at once
B) A small window of instructions at a time
C) The program’s logic and flow
D) Only the most critical code sections
Answer: B) A small window of instructions at a time
4. Which of the following is NOT typically targeted by peephole optimization?
A) Replacing a sequence of instructions with more efficient ones
B) Removing redundant load and store instructions
C) Reducing the complexity of loops
D) Simplifying consecutive arithmetic operations
Answer: C) Reducing the complexity of loops
5. In peephole optimization, which of the following changes might be made?
A) Replacing A = B + 0
with A = B
B) Removing loops with no body
C) Converting an if
statement into a switch
statement
D) Eliminating unreachable code
Answer: A) Replacing A = B + 0
with A = B
6. Which of the following is a key characteristic of peephole optimization?
A) It is applied after the entire program is compiled.
B) It focuses on optimizing large chunks of code.
C) It is a low-level optimization technique.
D) It does not affect the correctness of the program.
Answer: C) It is a low-level optimization technique.
7. What is a possible effect of peephole optimization on the program’s performance?
A) It can significantly increase the program’s runtime.
B) It can reduce the size of the code and improve execution speed.
C) It has no effect on the performance.
D) It may make the program less readable but improve performance.
Answer: B) It can reduce the size of the code and improve execution speed.
8. Which of the following operations is NOT typically considered in peephole optimization?
A) Removing unnecessary instructions
B) Replacing a sequence of instructions with a faster alternative
C) Reducing function calls
D) Simplifying constant expressions
Answer: C) Reducing function calls
9. Which of the following is an example of a simple peephole optimization?
A) Replacing A = A + 0
with A = A
B) Converting a while
loop to a for
loop
C) Rewriting a function for better performance
D) Changing an array to a linked list
Answer: A) Replacing A = A + 0
with A = A
10. In which stage of the compiler process is peephole optimization usually applied?
A) Lexical analysis
B) Syntax analysis
C) Intermediate code generation
D) Code generation or after code generation
Answer: D) Code generation or after code generation
11. Which of the following peephole optimizations would reduce the number of instructions in the sequence MOV A, B; MOV C, A;
?
A) Replace MOV A, B; MOV C, A
with MOV C, B
B) Remove MOV A, B
and leave MOV C, A
C) Replace MOV A, B
with ADD C, B
D) None of the above
Answer: A) Replace MOV A, B; MOV C, A
with MOV C, B
12. Which of the following is a common peephole optimization?
A) Transforming loops into recursive functions
B) Removing redundant NOP
(no operation) instructions
C) Changing if
conditions into switch
statements
D) Converting high-level code to assembly
Answer: B) Removing redundant NOP
(no operation) instructions
13. Peephole optimization is primarily focused on:
A) Improving the high-level logic of the program
B) Optimizing individual instructions and instruction sequences
C) Optimizing memory usage
D) Reducing the overall complexity of the program
Answer: B) Optimizing individual instructions and instruction sequences
14. What type of instruction sequence does peephole optimization generally target?
A) Large loops with complex conditions
B) Sequences of instructions that can be replaced with more efficient alternatives
C) High-level control flow statements
D) Entire program logic
Answer: B) Sequences of instructions that can be replaced with more efficient alternatives
15. Which of the following is a common transformation applied during peephole optimization?
A) A = B * 1
→ A = B
B) if (x == 1) { } else { }
→ if (x == 1) {}
C) for (i = 0; i < 10; i++) {}
→ for (i = 0; i < 5; i++) {}
D) A = B + C + D
→ A = (B + C) + D
Answer: A) A = B * 1
→ A = B
16. Peephole optimization is considered a type of:
A) Machine-dependent optimization
B) Compiler frontend optimization
C) Low-level optimization
D) High-level optimization
Answer: C) Low-level optimization
17. In which situation would peephole optimization be most effective?
A) When there are large amounts of redundant code
B) When improving loop structures
C) When dealing with simple instruction sequences that can be improved
D) When refactoring the entire program’s logic
Answer: C) When dealing with simple instruction sequences that can be improved
18. Which of the following is an example of a sequence that peephole optimization could simplify?
A) MOV A, B; ADD A, C; MOV D, A
→ ADD D, B, C
B) MOV A, B; ADD A, C
→ MOV A, B; ADD B, C
C) MOV A, B; MOV B, C
→ MOV A, C
D) MOV A, B; ADD A, C
→ MOV A, B; ADD C, D
Answer: C) MOV A, B; MOV B, C
→ MOV A, C
19. What is one limitation of peephole optimization?
A) It can only be applied to certain types of programs
B) It works best for large code blocks and complex structures
C) It is not effective for high-level programming languages
D) It does not always significantly reduce the size of the program
Answer: D) It does not always significantly reduce the size of the program
20. Which of the following is NOT a benefit of peephole optimization?
A) Reduction of the code size
B) Improvement in the execution speed of the program
C) Improved readability of the source code
D) Removal of unnecessary instructions
Answer: C) Improved readability of the source code