Semantic Analysis MCQs

What is the primary goal of semantic analysis in compiler design?

A) To convert the source code into intermediate code
B) To check the correctness of syntax in the code
C) To ensure that the program has a meaningful structure
D) To optimize the code for performance

Answer: C) To ensure that the program has a meaningful structure


2. Which of the following checks is performed during semantic analysis?

A) Syntax errors
B) Type mismatches
C) Missing semicolons
D) Undefined identifiers

Answer: B) Type mismatches


3. What is a semantic error?

A) An error in the order of tokens
B) An error that causes the program to crash
C) An error that occurs when the program does not follow logical rules or meaning
D) An error detected in the parsing stage

Answer: C) An error that occurs when the program does not follow logical rules or meaning


4. Which of the following is typically handled during semantic analysis?

A) Ensuring correct loop control
B) Type checking
C) Tokenization
D) Syntax tree construction

Answer: B) Type checking


5. Which phase of compilation is responsible for checking if variables are properly declared and used?

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

Answer: C) Semantic analysis


6. Which of the following is an example of a semantic error?

A) Using an undeclared variable
B) Using a keyword as a variable name
C) Assigning a string to an integer variable
D) Missing a closing parenthesis

Answer: C) Assigning a string to an integer variable


7. Which of the following is an important component of semantic analysis?

A) Syntax tree construction
B) Symbol table creation
C) Token generation
D) Code generation

Answer: B) Symbol table creation


8. What is the role of the symbol table in semantic analysis?

A) To store all the tokens generated during lexical analysis
B) To record variable types and their scopes
C) To store the abstract syntax tree
D) To handle memory allocation during code generation

Answer: B) To record variable types and their scopes


9. Which of the following is a result of semantic analysis?

A) A list of syntax errors
B) An intermediate representation of the program
C) A list of all identifiers used with their types
D) An optimized machine code

Answer: C) A list of all identifiers used with their types


10. In semantic analysis, what is type checking used for?

A) To check the number of statements in a program
B) To ensure that operations between variables are logically consistent
C) To check for syntax errors in the program
D) To generate intermediate code

Answer: B) To ensure that operations between variables are logically consistent


11. What happens if semantic analysis detects an error?

A) The error is reported and compilation stops
B) The error is corrected automatically
C) The program is re-parsed with a different strategy
D) The program is optimized

Answer: A) The error is reported and compilation stops


12. Which of the following can be checked in semantic analysis but not in syntax analysis?

A) Whether every opening parenthesis has a corresponding closing parenthesis
B) Whether the operator is being used correctly with operands
C) Whether all keywords are used correctly
D) Whether the program follows the rules of the grammar

Answer: B) Whether the operator is being used correctly with operands


13. Which of the following is not a task performed by semantic analysis?

A) Type checking
B) Checking the validity of function calls
C) Checking for undefined variables
D) Constructing an abstract syntax tree

Answer: D) Constructing an abstract syntax tree


14. Which of the following tools or techniques is used to handle semantic analysis?

A) Abstract syntax tree (AST)
B) Regular expressions
C) Context-free grammar (CFG)
D) Finite automata

Answer: A) Abstract syntax tree (AST)


15. During semantic analysis, what happens when a variable is used without being declared?

A) It is added to the symbol table with a default type
B) The program execution is paused
C) A semantic error is reported
D) The variable is automatically declared

Answer: C) A semantic error is reported

Leave a Comment

All copyrights Reserved by MCQsAnswers.com - Powered By T4Tutorials