Semantic Errors and Type Checking MCQs

1.What is a semantic error?

A) An error in the syntax of a program
B) An error in the logic or meaning of a program
C) An error in memory allocation
D) An error in input/output operations

Answer: B) An error in the logic or meaning of a program


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

A) Missing semicolon
B) Using an undeclared variable
C) Forgetting to close a bracket
D) Using incorrect syntax for a loop

Answer: B) Using an undeclared variable


3. What is the primary purpose of type checking during semantic analysis?

A) To ensure the program follows the syntax rules
B) To verify that variables are properly declared
C) To ensure the compatibility of operands and operators
D) To generate machine code for the program

Answer: C) To ensure the compatibility of operands and operators


4. Which of the following errors is typically caught by type checking?

A) Attempting to assign a string value to an integer variable
B) Incorrect use of parentheses in expressions
C) An undefined variable
D) Incorrect placement of operators

Answer: A) Attempting to assign a string value to an integer variable


5. Which of the following is NOT a type checking error?

A) Using a string as an integer
B) Assigning a function return type to a variable
C) Calling a function with the wrong number of arguments
D) Declaring a variable without initializing it

Answer: D) Declaring a variable without initializing it


6. Which of the following scenarios is likely to result in a semantic error?

A) Forgetting to declare a variable
B) Using a variable before it is assigned a value
C) Using a function with incorrect syntax
D) Forgetting to add a closing parenthesis

Answer: B) Using a variable before it is assigned a value


7. Which of the following can be considered a type checking issue?

A) Assigning a float value to an integer variable
B) Using a variable without declaring it
C) Forgetting to close a string literal
D) Mismatched parentheses in an expression

Answer: A) Assigning a float value to an integer variable


8. What happens during type checking in semantic analysis?

A) The program is converted into machine code
B) The syntax of the program is checked
C) The types of variables and expressions are verified
D) The variables are allocated memory

Answer: C) The types of variables and expressions are verified


9. Which of the following is a common semantic error involving type checking?

A) Trying to print a string variable
B) Trying to divide an integer by zero
C) Attempting to assign a boolean value to an integer variable
D) Writing a for loop without initializing the counter variable

Answer: C) Attempting to assign a boolean value to an integer variable


10. What is the result of attempting to perform a mathematical operation on incompatible types in a strongly-typed language?

A) Compilation continues without issues
B) A runtime exception is thrown
C) The compiler issues a semantic error
D) The compiler automatically converts types

Answer: C) The compiler issues a semantic error


11. What type of error occurs when an expression involves operands of incompatible types?

A) Syntax error
B) Type error
C) Logic error
D) Runtime error

Answer: B) Type error


12. Which of the following would likely cause a semantic error due to type mismatch?

A) Declaring a variable without a type
B) Using a variable before assigning it a value
C) Adding a number to a string without explicit casting
D) Writing a function with an incorrect signature

Answer: C) Adding a number to a string without explicit casting


13. What is the result of performing type checking on an assignment like int a = "hello"; in a statically-typed language?

A) The program will run without issues
B) The program will throw a runtime exception
C) A semantic error will be reported
D) The variable a will be automatically converted to a string

Answer: C) A semantic error will be reported


14. Which of the following errors is caught during semantic analysis?

A) Incorrect syntax in a loop
B) Missing semicolon at the end of a statement
C) Incorrect function argument types
D) Undefined variables

Answer: C) Incorrect function argument types


15. What is the role of a type checker in the semantic analysis phase?

A) To ensure the program follows the rules of the grammar
B) To verify that operations are performed on compatible types
C) To optimize the code for performance
D) To check for syntax errors in the program

Answer: B) To verify that operations are performed on compatible types

Leave a Comment

All copyrights Reserved by MCQsAnswers.com - Powered By T4Tutorials