Types of errors (lexical, syntactic, semantic)(MCQs)

Lexical Errors

What is a lexical error?

A) An error in the meaning of a program
B) An error in the structure of a program
C) An error in the formation of tokens
D) An error in the logic of a program
Answer: C) An error in the formation of tokens
Which phase of the compiler is responsible for detecting lexical errors?

A) Lexical analysis
B) Syntax analysis
C) Semantic analysis
D) Code generation
Answer: A) Lexical analysis
A lexical error typically occurs when:

A) The compiler cannot recognize a sequence of characters as a valid token
B) The parser encounters an undefined variable
C) The program has a logical inconsistency
D) The code does not compile due to missing semicolons
Answer: A) The compiler cannot recognize a sequence of characters as a valid token
Which of the following can cause a lexical error?

A) Misspelled keywords
B) Incorrect indentation
C) Mismatched parentheses
D) Division by zero
Answer: A) Misspelled keywords
Which of the following tools detects lexical errors?

A) Lex (lexical analyzer generator)
B) Yacc (parser generator)
C) Debugger
D) Compiler linker
Answer: A) Lex (lexical analyzer generator)
What happens when a lexical analyzer encounters an unrecognized character?

A) A lexical error is reported
B) The program continues execution
C) The character is ignored
D) The program enters an infinite loop
Answer: A) A lexical error is reported
Which type of error is caused by a misused operator in source code?

A) Lexical error
B) Syntax error
C) Semantic error
D) Runtime error
Answer: A) Lexical error
When a numeric constant contains invalid characters, which type of error occurs?

A) Lexical error
B) Semantic error
C) Syntactic error
D) Logical error
Answer: A) Lexical error
Which of the following is NOT a possible lexical error?

A) Incorrect keyword spelling
B) Incorrect token formation
C) Integer overflow
D) Illegal identifier name
Answer: C) Integer overflow
A lexical error in a program is detected during which of the following?

A) Compilation time
B) Runtime
C) Program execution
D) Memory allocation
Answer: A) Compilation time

Syntactic Errors

What is a syntactic error?

A) An error in the structure of a source code statement
B) An error in variable declaration
C) An error in the logic of the program
D) An error in the sequence of tokens
Answer: A) An error in the structure of a source code statement
Which compiler phase is responsible for detecting syntactic errors?

A) Syntax analysis
B) Semantic analysis
C) Lexical analysis
D) Intermediate code generation
Answer: A) Syntax analysis
Which of the following is a typical example of a syntactic error?

A) Mismatched parentheses
B) Use of uninitialized variables
C) Division by zero
D) Invalid memory access
Answer: A) Mismatched parentheses
A missing semicolon in a C++ program results in which type of error?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Logical error
Answer: A) Syntactic error
Syntactic errors occur due to violations in:

A) The grammar rules of the programming language
B) The logical flow of the program
C) The use of illegal tokens
D) Incorrect variable types
Answer: A) The grammar rules of the programming language
Which of the following tools can help detect syntactic errors?

A) Yacc (Yet Another Compiler Compiler)
B) Debugger
C) Profiler
D) Memory checker
Answer: A) Yacc (Yet Another Compiler Compiler)
An unmatched curly brace in a C++ program is an example of which type of error?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Runtime error
Answer: A) Syntactic error
A missing keyword in an if statement causes which type of error?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Runtime error
Answer: A) Syntactic error
If an else clause is used without a preceding if, which type of error is generated?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Logical error
Answer: A) Syntactic error
An incorrect arrangement of tokens in a statement results in:

A) Syntactic error
B) Lexical error
C) Semantic error
D) Runtime error
Answer: A) Syntactic error

Semantic Errors

What is a semantic error?

A) An error related to the meaning of a program statement
B) An error in the order of tokens
C) An error due to a missing operator
D) An error due to invalid syntax
Answer: A) An error related to the meaning of a program statement
Which phase of the compiler detects semantic errors?

A) Semantic analysis phase
B) Lexical analysis phase
C) Syntax analysis phase
D) Code generation phase
Answer: A) Semantic analysis phase
Using a variable before it is initialized is an example of a:

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
If an operation is performed on incompatible data types, what type of error occurs?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Runtime error
Answer: A) Semantic error
A semantic error typically indicates a problem with:

A) The logic or meaning of a program
B) The structure of code
C) The arrangement of tokens
D) The memory allocation
Answer: A) The logic or meaning of a program
Which of the following is an example of a semantic error?

A) Adding a number to a string variable
B) Mismatched parentheses
C) Misspelled keyword
D) Missing semicolon
Answer: A) Adding a number to a string variable
Declaring a variable but not using it in a program results in a:

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
When a function is called with an incorrect number of arguments, which error is triggered?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
Assigning the wrong data type to a variable results in a:

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
A division of an integer by zero results in which error type at runtime?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error

Comparing Lexical, Syntactic, and Semantic Errors

Which error is typically detected first during the compilation process?

A) Lexical error
B) Syntactic error
C) Semantic error
D) Logical error
Answer: A) Lexical error
Which of the following errors is related to the structure of the code but not its meaning?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Logical error
Answer: A) Syntactic error
Which type of error can result from an incorrect function call?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Runtime error
Answer: A) Semantic error
When a variable is declared but not defined, what type of error might occur?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
Which type of error involves incorrect usage of grammar in the source code?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Runtime error
Answer: A) Syntactic error
Which error type occurs when the program’s meaning does not align with its operations?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
Misspelled keywords usually result in which error?

A) Lexical error
B) Syntactic error
C) Semantic error
D) Logical error
Answer: A) Lexical error
Which error type is most commonly encountered during program execution rather than compilation?

A) Runtime error
B) Lexical error
C) Syntactic error
D) Semantic error
Answer: A) Runtime error
An undeclared variable used in an expression will cause a:

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
What error is most likely to be caught by a parser?

A) Syntactic error
B) Lexical error
C) Semantic error
D) Logical error
Answer: A) Syntactic error

Advanced Concepts

When a variable is declared multiple times in the same scope, what error is caused?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
Which type of error will occur if you try to use a reserved keyword as an identifier?

A) Lexical error
B) Semantic error
C) Syntactic error
D) Runtime error
Answer: A) Lexical error
A function call with missing arguments leads to:

A) Semantic error
B) Syntactic error
C) Lexical error
D) Logical error
Answer: A) Semantic error
Which of the following is NOT a semantic error?

A) Unused variable
B) Division by zero
C) Incorrect argument type
D) Missing semicolon
Answer: D) Missing semicolon
Which error is caused by a wrong operator precedence in an expression?

A) Semantic error
B) Syntactic error
C) Lexical error
D) Runtime error
Answer: A) Semantic error
Which type of error is detected at the execution time rather than at compile time?

A) Runtime error
B) Syntactic error
C) Lexical error
D) Semantic error
Answer: A) Runtime error
When a loop condition is incorrect, causing an infinite loop, it is an example of:

A) Logical error
B) Lexical error
C) Syntactic error
D) Semantic error
Answer: A) Logical error
What type of error results from using an invalid array index?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Runtime error
Answer: A) Semantic error
Inserting the wrong operator in an expression causes which error?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Logical error
Answer: A) Semantic error
Which error involves declaring a function with incorrect return types?

A) Semantic error
B) Lexical error
C) Syntactic error
D) Runtime error
Answer: A) Semantic error