10 compiler design mcq

 Here are 10 multiple-choice questions on Compiler Design for UGC NET, SET, GATE, ISRO, and other competitive exams.

Question 1: Symbol Table Management

Q1: In block-structured languages, how does the compiler manage multiple symbol tables?

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

Answer: B) Using a stack of symbol tables

Explanation: Block-structured languages use nested scopes, so the compiler needs to manage different symbol tables dynamically. A stack is the best data structure for this because when entering a new block, a new symbol table is pushed onto the stack, and when exiting, it is popped.

Question 2: Lexical Analysis

Q2: Which of the following is the main function of a Lexical Analyzer in a compiler?

A) To generate intermediate code
B) To check syntax and semantics
C) To convert source code into tokens
D) To optimize the final machine code

Answer: C) To convert source code into tokens

Explanation: The lexical analyzer scans the source code and converts it into tokens, which are then used by the syntax analyzer. It removes whitespace, comments, and identifies keywords, identifiers, operators, and literals.

Question 3: Parsing Techniques

Q3: Which parsing technique is used in top-down parsing without backtracking?

A) LL(1) Parsing
B) LR(1) Parsing
C) Recursive Descent Parsing
D) Operator Precedence Parsing

Answer: A) LL(1) Parsing

Explanation: LL(1) parsers read input from Left to right and construct a Leftmost derivation. They work without backtracking by using a lookahead token to decide the next step.

Question 4: Intermediate Code Generation

Q4: Which of the following is commonly used as an Intermediate Representation (IR) in a compiler?

A) Parse Tree
B) Abstract Syntax Tree (AST)
C) Three-Address Code (TAC)
D) Lexical Tokens

Answer: C) Three-Address Code (TAC)

Explanation: Three-Address Code (TAC) is a widely used Intermediate Representation (IR) where each instruction has at most three operands. It simplifies the process of code optimization and translation into machine code.

Question 5: Syntax Analysis

Q5: What is the role of a Syntax Analyzer in a compiler?

A) To convert source code into machine code
B) To check the syntax of the source program using grammar rules
C) To optimize the generated code
D) To execute the program

Answer: B) To check the syntax of the source program using grammar rules

Explanation: The Syntax Analyzer (Parser) ensures that the program follows correct syntax according to a context-free grammar (CFG). It constructs a parse tree and reports syntax errors.

Question 6: Compiler Phases

Q6: In which phase of the compiler are syntax errors detected?

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

Answer: B) Syntax Analysis

Explanation: The Syntax Analysis phase checks whether the input code follows the grammar rules of the programming language. Errors like missing parentheses, incorrect nesting, or misplaced operators are detected here.

Question 7: Code Optimization

Q7: Which of the following techniques is NOT used for code optimization?

A) Constant Folding
B) Loop Unrolling
C) Symbol Table Management
D) Dead Code Elimination

Answer: C) Symbol Table Management

Explanation: Code optimization improves performance by reducing execution time and space. Techniques like constant folding, loop unrolling, and dead code elimination are used to optimize code. Symbol Table Management is part of semantic analysis, not optimization.

Question 8: Error Handling

Q8: Which type of error can be detected only during runtime?

A) Syntax Error
B) Semantic Error
C) Logical Error
D) Lexical Error

Answer: C) Logical Error

Explanation: Logical errors (e.g., incorrect calculations, infinite loops) do not violate syntax or semantics but cause incorrect results. These errors cannot be detected by the compiler and only appear when the program is executed.

Question 9: Compiler vs Interpreter

Q9: Which of the following statements is true about compilers and interpreters?

A) A compiler executes code line by line, while an interpreter translates the whole program at once
B) A compiler translates the entire program before execution, while an interpreter executes line by line
C) Both compilers and interpreters translate the entire program before execution
D) Interpreters generate machine code, while compilers do not

Answer: B) A compiler translates the entire program before execution, while an interpreter executes line by line

Explanation: A compiler translates the whole source code into machine code before execution, while an interpreter translates and executes code line by line.

Question 10: Just-In-Time (JIT) Compilation

Q10: What is the main advantage of Just-In-Time (JIT) compilation?

A) Faster execution by compiling code at runtime
B) Reducing memory usage
C) Eliminating the need for an operating system
D) Improving syntax error detection

Answer: A) Faster execution by compiling code at runtime

Explanation: JIT compilation is used in Java (JVM), .NET (CLR), and Python (PyPy). It compiles bytecode into native machine code at runtime, improving execution speed compared to traditional interpretation.

Comments