Error Types in Compilation (Lexical, Syntax, Semantic) MCQs

1. Which of the following error types is detected by the lexical analyzer in the compiler?

A) Syntax errors
B) Semantic errors
C) Lexical errors
D) Runtime errors

Answer: C) Lexical errors


2. Which of the following is an example of a lexical error?

A) Using an undeclared variable
B) Mismatched parentheses in an expression
C) Misspelled keywords
D) Dividing by zero

Answer: C) Misspelled keywords


3. What type of error occurs when the source code does not conform to the grammar rules of the language?

A) Semantic error
B) Syntax error
C) Lexical error
D) Linker error

Answer: B) Syntax error


4. Which of the following is an example of a syntax error?

A) Using a variable before it is declared
B) Incorrect use of the if statement (e.g., missing parentheses or braces)
C) Division by zero
D) Calling a function with the wrong number of arguments

Answer: B) Incorrect use of the if statement (e.g., missing parentheses or braces)


5. Which type of error occurs when the program’s logic is correct, but the program does not produce the expected results?

A) Lexical error
B) Syntax error
C) Semantic error
D) Runtime error

Answer: C) Semantic error


6. Which of the following best describes a semantic error?

A) The program has incorrect syntax
B) The program uses an undeclared variable
C) The program runs without crashing but produces incorrect results
D) The program has missing punctuation marks

Answer: C) The program runs without crashing but produces incorrect results


7. Which of the following errors is typically caught during the lexical analysis phase?

A) Missing semicolons
B) Incorrect function definition
C) Invalid characters in the source code
D) Type mismatch in an expression

Answer: C) Invalid characters in the source code


8. Which error type occurs when a program contains an operation that is not logically valid in terms of the language’s semantics, such as dividing by zero?

A) Syntax error
B) Lexical error
C) Semantic error
D) Linker error

Answer: C) Semantic error


9. What is the main difference between syntax and semantic errors?

A) Syntax errors occur due to incorrect grammar, while semantic errors occur due to the incorrect meaning of the code
B) Syntax errors are always fatal, while semantic errors are not
C) Syntax errors are runtime issues, whereas semantic errors are compile-time issues
D) There is no difference between them

Answer: A) Syntax errors occur due to incorrect grammar, while semantic errors occur due to the incorrect meaning of the code


10. In which phase of the compiler are semantic errors most commonly detected?

A) Lexical analysis
B) Syntax analysis
C) Semantic analysis
D) Code generation

Answer: C) Semantic analysis


11. Which of the following is typically a lexical error?

A) Using a reserved keyword as an identifier
B) Forgetting to close a quotation mark in a string
C) Writing an invalid expression
D) Calling an undefined function

Answer: A) Using a reserved keyword as an identifier


12. What type of error occurs when a program violates the rules of the programming language’s semantics, but does not result in an immediate crash?

A) Syntax error
B) Lexical error
C) Runtime error
D) Semantic error

Answer: D) Semantic error


13. Which error occurs if a variable is used before it is initialized?

A) Lexical error
B) Syntax error
C) Semantic error
D) Runtime error

Answer: C) Semantic error


14. Which of the following errors would most likely result in a syntax error?

A) Incorrect use of a variable’s type
B) Forgetting to include a semicolon at the end of a statement
C) Using the wrong data type in a function call
D) Dividing a number by zero

Answer: B) Forgetting to include a semicolon at the end of a statement


15. In the case of lexical errors, which of the following actions would the lexical analyzer take?

A) Report the error and terminate the compilation
B) Attempt to recover and continue analysis
C) Skip the erroneous token and continue
D) Automatically correct the error

Answer: B) Attempt to recover and continue analysis


16. Which error type occurs when a program violates the rules of the programming language that pertain to the meaning of statements, such as type mismatch?

A) Syntax error
B) Lexical error
C) Semantic error
D) Runtime error

Answer: C) Semantic error


17. What is an example of a semantic error in a strongly-typed language?

A) A missing semicolon
B) Assigning an integer value to a string variable
C) A missing parenthesis in a function call
D) Using an undeclared variable

Answer: B) Assigning an integer value to a string variable


18. Which of the following errors is typically detected during the parsing phase of a compiler?

A) Syntax errors
B) Lexical errors
C) Semantic errors
D) Runtime errors

Answer: A) Syntax errors


19. Which of the following scenarios would likely generate a syntax error?

A) Using an undeclared variable
B) Declaring a variable of an invalid type
C) Forgetting to close a parenthesis in a function call
D) Performing a division by zero

Answer: C) Forgetting to close a parenthesis in a function call


20. Which of the following is the correct order in which errors are typically detected during compilation?

A) Lexical errors → Syntax errors → Semantic errors
B) Semantic errors → Syntax errors → Lexical errors
C) Syntax errors → Semantic errors → Lexical errors
D) Lexical errors → Semantic errors → Syntax errors

Answer: A) Lexical errors → Syntax errors → Semantic errors

Leave a Reply

Your email address will not be published. Required fields are marked *