10 Symbol Table MCQ

 Here are 10 multiple-choice questions on Symbol Table with explanations, useful for UGC NET, SET, GATE, ISRO, and other competitive exams.

Question 1: Purpose of Symbol Table

Q1: What is the main purpose of a Symbol Table in a compiler?

A) To store all the variables of a program
B) To store information about identifiers like variables, functions, and classes
C) To store the machine code of a program
D) To store the syntax tree of a program

Answer: B) To store information about identifiers like variables, functions, and classes

Explanation: The Symbol Table keeps track of identifiers (variables, functions, classes, objects) and their associated information like scope, type, memory location, and access rights.


Question 2: Data Structure for Symbol Table

Q2: Which of the following data structures is commonly used for implementing a Symbol Table?

A) Stack
B) Queue
C) Hash Table
D) Linked List

Answer: C) Hash Table

Explanation: A Hash Table allows fast lookups, insertions, and deletions, making it ideal for managing a Symbol Table. Other structures like linked lists can be used in case of collisions.


Question 3: Symbol Table Operations

Q3: Which of the following is NOT an operation performed on a Symbol Table?

A) Insert
B) Delete
C) Lookup
D) Sorting

Answer: D) Sorting

Explanation: A Symbol Table inserts, deletes, and looks up identifiers, but sorting is not required because the compiler only needs to retrieve and update information efficiently.


Question 4: Scope Handling

Q4: How does a compiler manage multiple symbol tables in block-structured languages?

A) Using a single global table
B) Using a stack of symbol tables
C) Using a queue
D) Using an array

Answer: B) Using a stack of symbol tables

Explanation: When entering a new block or function, a new symbol table is pushed onto the stack. When exiting, it is popped to restore the previous scope.


Question 5: Symbol Table in Different Compiler Phases

Q5: In which compiler phase is the Symbol Table created and maintained?

A) Lexical Analysis
B) Syntax Analysis
C) Semantic Analysis
D) Code Optimization

Answer: C) Semantic Analysis

Explanation: The Symbol Table is populated in the Lexical and Syntax Analysis phases but fully utilized during Semantic Analysis to perform type checking, scope resolution, and name resolution.


Question 6: Hashing in Symbol Table

Q6: Why is hashing commonly used for implementing Symbol Tables?

A) It provides constant-time average-case lookup
B) It reduces memory usage
C) It maintains the order of identifiers
D) It allows sequential access to identifiers

Answer: A) It provides constant-time average-case lookup

Explanation: Hashing ensures fast access (O(1) on average) to identifiers, making insertions and lookups efficient in a Symbol Table.


Question 7: Collision Resolution in Hashing

Q7: Which of the following is a collision resolution technique used in Symbol Tables?

A) Depth-First Search
B) Chaining
C) Sorting
D) Binary Search

Answer: B) Chaining

Explanation: Chaining handles hash collisions by maintaining a linked list of entries that hash to the same index. Linear probing and double hashing are other methods.


Question 8: Nested Scopes

Q8: In a programming language with nested scopes, how does the compiler resolve identifier references using the Symbol Table?

A) By searching only in the local Symbol Table
B) By searching the local table first, then the global table if not found
C) By using only the global Symbol Table
D) By using only the most recently defined identifier

Answer: B) By searching the local table first, then the global table if not found

Explanation: The compiler first checks the current scope’s symbol table. If the identifier is not found, it looks in enclosing scopes up to the global level.


Question 9: Semantic Analysis and Symbol Table

Q9: During Semantic Analysis, what role does the Symbol Table play?

A) Checking whether variables have been declared before use
B) Generating machine code
C) Optimizing the program
D) Parsing the input program

Answer: A) Checking whether variables have been declared before use

Explanation: The Semantic Analyzer uses the Symbol Table to verify type consistency, function calls, and variable declarations before generating code.


Question 10: Types of Symbol Tables

Q10: What is the difference between Global Symbol Table and Local Symbol Table?

A) The Global Symbol Table stores all identifiers, while the Local Symbol Table stores only function parameters
B) The Global Symbol Table is used only for built-in functions, while the Local Symbol Table is used for user-defined functions
C) The Global Symbol Table stores identifiers visible throughout the program, while the Local Symbol Table is specific to functions or blocks
D) There is no difference between them

Answer: C) The Global Symbol Table stores identifiers visible throughout the program, while the Local Symbol Table is specific to functions or blocks

Explanation: Global Symbol Table stores variables accessible anywhere, while Local Symbol Tables manage variables within a function or block to handle scoping rules properly.

Comments