Q.1 What is the main goal of semantic analysis in a compiler?
To parse the source code
To ensure the program is syntactically correct
To check for logical consistency and type correctness
To generate machine code
Explanation - Semantic analysis verifies that the syntactically correct source program is also semantically meaningful, ensuring type correctness and consistency.
Correct answer is: To check for logical consistency and type correctness
Q.2 Which compiler phase immediately follows parsing?
Lexical Analysis
Semantic Analysis
Intermediate Code Generation
Code Optimization
Explanation - After parsing, the semantic analysis phase checks for type rules and semantic correctness before moving to code generation.
Correct answer is: Semantic Analysis
Q.3 Which of the following is checked during semantic analysis?
Operator precedence
Variable declarations
Token recognition
Instruction scheduling
Explanation - Semantic analysis ensures that identifiers are declared before use and their types are consistent.
Correct answer is: Variable declarations
Q.4 Type checking ensures:
Variables are declared before use
Correct syntax is used
Operands are compatible with operators
Registers are allocated properly
Explanation - Type checking ensures that operators are applied to operands of valid and compatible types.
Correct answer is: Operands are compatible with operators
Q.5 Which data structure is essential for semantic analysis?
Abstract Syntax Tree
Symbol Table
Parse Tree
Stack
Explanation - The symbol table keeps track of identifiers, their declarations, and types, which are vital for semantic checks.
Correct answer is: Symbol Table
Q.6 What kind of error is detected by semantic analysis?
Syntax error
Lexical error
Type mismatch error
Machine-dependent error
Explanation - Semantic analysis detects type-related errors such as assigning a string to an integer variable.
Correct answer is: Type mismatch error
Q.7 Strong typing in a language means:
Types can be easily converted
All variables must be declared
Operations between incompatible types are disallowed
Variables can change type during execution
Explanation - Strong typing ensures type safety by preventing operations on incompatible types.
Correct answer is: Operations between incompatible types are disallowed
Q.8 Static type checking occurs:
During program execution
At compile-time
At link-time
After code optimization
Explanation - Static type checking verifies type rules during compilation, before execution.
Correct answer is: At compile-time
Q.9 Which one is a dynamic type checking example?
C++ compiler detecting int and float mismatch
Java detecting array bounds violation at runtime
C detecting undeclared variables
Assembler reporting invalid opcode
Explanation - Dynamic type checking occurs during execution, such as checking array bounds in Java.
Correct answer is: Java detecting array bounds violation at runtime
Q.10 Overloading resolution is handled during:
Lexical Analysis
Parsing
Semantic Analysis
Code Generation
Explanation - Semantic analysis determines which version of an overloaded function/operator should be invoked.
Correct answer is: Semantic Analysis
Q.11 Which rule ensures that an identifier is declared before it is used?
Syntax rule
Lexical rule
Scope rule
Type rule
Explanation - Scope rules ensure that identifiers are declared within a valid scope before usage.
Correct answer is: Scope rule
Q.12 Which type system allows automatic type conversions?
Strongly typed
Weakly typed
Statically typed
Dynamically typed
Explanation - Weakly typed languages permit implicit conversions that may reduce type safety.
Correct answer is: Weakly typed
Q.13 What is coercion in type checking?
Implicit type conversion
Explicit type conversion
Type declaration
Type enforcement
Explanation - Coercion is the implicit conversion of one type to another, often performed automatically by the compiler.
Correct answer is: Implicit type conversion
Q.14 Which of these is an example of a semantic error?
Missing semicolon
Use of undeclared variable
Invalid keyword
Unclosed string literal
Explanation - Using undeclared variables violates semantic rules, not syntax rules.
Correct answer is: Use of undeclared variable
Q.15 Name equivalence in type systems means:
Two types are equivalent if they have the same name
Two types are equivalent if they have the same structure
Two types are equivalent if coercion is allowed
Two types are equivalent if they are declared globally
Explanation - Name equivalence requires that two types are considered equal only if their names match.
Correct answer is: Two types are equivalent if they have the same name
Q.16 Structural equivalence means:
Types are equal if declared in the same scope
Types are equal if they look the same structurally
Types are equal if explicitly casted
Types are equal only if names match
Explanation - Structural equivalence compares the internal structure of types, not their names.
Correct answer is: Types are equal if they look the same structurally
Q.17 Which semantic analysis technique is used to check variable scopes?
Type checking
Symbol table management
Error recovery
Lexical scoping
Explanation - Symbol tables are maintained during semantic analysis to manage scopes and declarations.
Correct answer is: Symbol table management
Q.18 A language that enforces type checking at compile-time is called:
Statically typed
Dynamically typed
Weakly typed
Loosely typed
Explanation - Statically typed languages enforce type rules during compilation.
Correct answer is: Statically typed
Q.19 Which is true about dynamically typed languages?
Variables must declare types explicitly
Type errors are caught at compile-time
Type errors may occur at runtime
Type safety is guaranteed
Explanation - Dynamically typed languages defer type checking until runtime.
Correct answer is: Type errors may occur at runtime
Q.20 What is the purpose of attribute grammars in semantic analysis?
Define syntax rules
Specify semantic rules and type information
Tokenize the input
Generate object code
Explanation - Attribute grammars extend CFGs with attributes to define semantic rules.
Correct answer is: Specify semantic rules and type information
Q.21 Inherited attributes are:
Passed from parent to child nodes in syntax tree
Passed from child to parent nodes
Defined globally in the program
Always calculated at runtime
Explanation - Inherited attributes flow down the parse tree from parent nodes to children.
Correct answer is: Passed from parent to child nodes in syntax tree
Q.22 Synthesized attributes are:
Passed from parent to child nodes
Passed from child to parent nodes
Stored in the symbol table
Always used for type casting
Explanation - Synthesized attributes are computed from children and passed upwards in the parse tree.
Correct answer is: Passed from child to parent nodes
Q.23 Which of the following is NOT handled by semantic analysis?
Type compatibility
Variable scope
Grammar rule enforcement
Function overloading
Explanation - Grammar rules are enforced during syntax analysis, not semantic analysis.
Correct answer is: Grammar rule enforcement
Q.24 In polymorphic functions, type checking must handle:
Multiple return statements
Multiple types for parameters
Dynamic memory allocation
Register spilling
Explanation - Polymorphism requires type checking to validate function calls with varying argument types.
Correct answer is: Multiple types for parameters
Q.25 What is the result of assigning an integer value to a floating-point variable in C?
Type error
Coercion from int to float
Runtime exception
Ignored
Explanation - C allows implicit coercion of integers to floats during assignment.
Correct answer is: Coercion from int to float
