Here are multiple-choice questions (MCQs) on Error Handling in the context of compilers and programming:
1. Which of the following types of errors is detected during the compilation process?
A) Run-time errors
B) Logic errors
C) Syntax errors
D) Semantic errors
Answer: C) Syntax errors
2. What is the primary purpose of error handling in a compiler?
A) To prevent logical errors in the program
B) To ensure that the program produces the correct output
C) To detect and report errors in source code and help in debugging
D) To optimize the execution speed of the program
Answer: C) To detect and report errors in source code and help in debugging
3. Which type of error occurs when a program does not conform to the grammar of the programming language?
A) Semantic error
B) Runtime error
C) Syntax error
D) Linker error
Answer: C) Syntax error
4. Which of the following is an example of a semantic error?
A) Dividing by zero
B) Using an undeclared variable
C) Mismatched parentheses
D) Calling a function with incorrect arguments
Answer: D) Calling a function with incorrect arguments
5. Which phase of the compiler is responsible for detecting syntax errors?
A) Lexical analysis
B) Parsing
C) Code generation
D) Optimization
Answer: B) Parsing
6. What is the primary role of error recovery in a compiler?
A) To correct the errors in the source code automatically
B) To continue compiling the program after encountering an error
C) To optimize the source code
D) To generate intermediate code
Answer: B) To continue compiling the program after encountering an error
7. Which error recovery strategy involves skipping the next few tokens and continuing parsing?
A) Panic mode recovery
B) Phrase level recovery
C) Error production recovery
D) Backtracking recovery
Answer: A) Panic mode recovery
8. Which of the following is a common technique used for error handling in compilers?
A) Syntax-directed translation
B) Backtracking
C) Panic mode recovery
D) Code optimization
Answer: C) Panic mode recovery
9. What type of error occurs when a variable is used before it is defined?
A) Syntax error
B) Semantic error
C) Runtime error
D) Logical error
Answer: B) Semantic error
10. Which of the following best describes a “semantic error”?
A) The program is syntactically correct but produces incorrect results
B) The program does not compile due to incorrect syntax
C) The program runs but throws exceptions during execution
D) The program runs without errors but produces incorrect results
Answer: A) The program is syntactically correct but produces incorrect results
11. What is an example of a runtime error?
A) Undefined function call
B) Using an undeclared variable
C) Division by zero
D) Mismatched parentheses
Answer: C) Division by zero
12. What does error recovery in syntax analysis typically involve?
A) Generating machine code
B) Continuing parsing from a safe point after an error
C) Correcting syntax errors automatically
D) Reporting errors and terminating compilation
Answer: B) Continuing parsing from a safe point after an error
13. What is a major disadvantage of panic-mode error recovery?
A) It can miss some syntax errors
B) It requires backtracking to fix errors
C) It only works for runtime errors
D) It leads to slower compilation times
Answer: A) It can miss some syntax errors
14. In which of the following cases would you most likely encounter a semantic error?
A) Missing semicolon in the program
B) Incorrect variable type assignment
C) Incorrect indentation
D) Incorrect syntax for a loop
Answer: B) Incorrect variable type assignment
15. What is the main challenge with handling semantic errors during compilation?
A) They are easy to detect and fix
B) They occur when the program violates the rules of the programming language
C) They require deeper understanding of the program’s meaning
D) They are detected during the syntax analysis phase
Answer: C) They require deeper understanding of the program’s meaning
16. What is a common feature of error reporting in modern compilers?
A) Providing no output until all errors are fixed
B) Reporting the first error and halting execution
C) Providing detailed error messages with error location and potential fixes
D) Only reporting syntax errors
Answer: C) Providing detailed error messages with error location and potential fixes
17. What is the primary purpose of the lexical analyzer in error handling?
A) To detect semantic errors
B) To generate intermediate code
C) To identify lexemes and report lexical errors
D) To handle syntax errors during parsing
Answer: C) To identify lexemes and report lexical errors
18. Which error handling strategy involves inserting or deleting tokens to recover from an error?
A) Panic mode recovery
B) Phrase-level recovery
C) Backtracking
D) Syntax-directed translation
Answer: B) Phrase-level recovery
19. Which of the following is an advantage of using error recovery in a compiler?
A) It can automatically correct all errors in the source code
B) It allows the compiler to continue compiling despite errors, reporting multiple issues
C) It ensures the program runs correctly
D) It eliminates the need for debugging
Answer: B) It allows the compiler to continue compiling despite errors, reporting multiple issues
20. What is the main objective of the syntax error handling mechanism in a compiler?
A) To correct errors automatically
B) To detect errors and report their location to the programmer
C) To continue compilation even if errors occur
D) To optimize the code generation phase
Answer: B) To detect errors and report their location to the programmer