Phases of Compilation and Compiler Architecture # MCQs Practice set

Q.1 Which phase of the compiler converts the source code into tokens?

Syntax Analysis
Lexical Analysis
Semantic Analysis
Code Generation
Explanation - Lexical Analysis scans the source code character by character and groups them into meaningful sequences called tokens.
Correct answer is: Lexical Analysis

Q.2 Which compiler phase checks the grammar of the program?

Lexical Analysis
Syntax Analysis
Code Optimization
Symbol Table Management
Explanation - Syntax Analysis, also called parsing, checks whether the tokens form a valid grammatical structure as per the programming language grammar.
Correct answer is: Syntax Analysis

Q.3 The compiler phase that verifies type consistency is:

Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Generation
Explanation - Semantic Analysis ensures that operations are semantically valid, such as type checking, scope resolution, and consistency checks.
Correct answer is: Semantic Analysis

Q.4 Intermediate code is generated in which phase?

Lexical Analysis
Intermediate Code Generation
Code Optimization
Parsing
Explanation - The Intermediate Code Generation phase produces a representation of the source code that is independent of the machine architecture.
Correct answer is: Intermediate Code Generation

Q.5 Which phase is responsible for improving the efficiency of intermediate code?

Code Optimization
Lexical Analysis
Semantic Analysis
Code Generation
Explanation - Code Optimization improves the intermediate code to generate faster or smaller machine code without altering program functionality.
Correct answer is: Code Optimization

Q.6 Which phase of the compiler generates assembly or machine code?

Lexical Analysis
Syntax Analysis
Code Generation
Code Optimization
Explanation - Code Generation converts the intermediate code into the final target machine code or assembly instructions.
Correct answer is: Code Generation

Q.7 The symbol table is primarily used to store:

Tokens
Error messages
Identifiers and their attributes
Machine code
Explanation - The symbol table stores identifiers (variables, functions) along with information like type, scope, and memory location.
Correct answer is: Identifiers and their attributes

Q.8 Which component of a compiler reports errors to the user?

Symbol Table
Error Handler
Parser
Optimizer
Explanation - Error Handler manages lexical, syntactic, and semantic errors during compilation and provides meaningful error messages.
Correct answer is: Error Handler

Q.9 Lexical Analysis uses which kind of automaton?

Pushdown Automaton
Finite Automaton
Turing Machine
Stack Automaton
Explanation - Lexical Analysis typically uses finite automata to recognize tokens from regular expressions.
Correct answer is: Finite Automaton

Q.10 Parsing is usually performed using:

Finite Automata
Pushdown Automata
Turing Machines
Assemblers
Explanation - Parsing requires a context-free grammar, which can be recognized by pushdown automata.
Correct answer is: Pushdown Automata

Q.11 Which phase is closely associated with grammar rules of programming languages?

Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generation
Explanation - Syntax Analysis applies grammar rules to check if the token sequence forms a syntactically valid program.
Correct answer is: Syntax Analysis

Q.12 Which compiler phase is responsible for constant folding?

Lexical Analysis
Semantic Analysis
Code Optimization
Code Generation
Explanation - Constant folding is an optimization technique performed during the Code Optimization phase.
Correct answer is: Code Optimization

Q.13 Which of the following is NOT a phase of the compiler?

Lexical Analysis
Syntax Analysis
Linking
Code Optimization
Explanation - Linking is part of the software build process but not a compiler phase. It happens after code generation.
Correct answer is: Linking

Q.14 What is the output of Lexical Analysis?

Abstract Syntax Tree
Parse Tree
Tokens
Intermediate Code
Explanation - Lexical Analysis outputs a sequence of tokens, which serve as input for Syntax Analysis.
Correct answer is: Tokens

Q.15 Which compiler phase is responsible for register allocation?

Lexical Analysis
Semantic Analysis
Code Generation
Code Optimization
Explanation - Register allocation is done during Code Generation to map variables to CPU registers.
Correct answer is: Code Generation

Q.16 Error detection and recovery is most difficult in which phase?

Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Generation
Explanation - Semantic Analysis deals with contextual meaning, making error recovery more complex than syntax errors.
Correct answer is: Semantic Analysis

Q.17 Which data structure is essential for syntax analysis?

Stack
Queue
Array
Hash Table
Explanation - Syntax analysis (parsing) uses a stack to handle recursive grammar rules and nested structures.
Correct answer is: Stack

Q.18 Which phase performs scope resolution?

Lexical Analysis
Semantic Analysis
Syntax Analysis
Code Generation
Explanation - Scope resolution, type checking, and ensuring identifiers are declared before use are tasks of Semantic Analysis.
Correct answer is: Semantic Analysis

Q.19 The first phase of a compiler is always:

Code Generation
Lexical Analysis
Syntax Analysis
Semantic Analysis
Explanation - Compilation begins with Lexical Analysis, which converts source code into tokens.
Correct answer is: Lexical Analysis

Q.20 Which compiler phase may use Directed Acyclic Graphs (DAGs)?

Syntax Analysis
Code Optimization
Lexical Analysis
Code Generation
Explanation - Code Optimization uses DAGs to detect common subexpressions and eliminate redundancies.
Correct answer is: Code Optimization

Q.21 Which phase helps in machine independence of the compiler?

Code Generation
Lexical Analysis
Intermediate Code Generation
Error Handling
Explanation - Intermediate Code Generation creates a machine-independent representation of code, aiding portability.
Correct answer is: Intermediate Code Generation

Q.22 During which phase is the Parse Tree constructed?

Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Generation
Explanation - Syntax Analysis constructs a Parse Tree representing the grammatical structure of the code.
Correct answer is: Syntax Analysis

Q.23 Which compiler phase is mainly concerned with detecting undeclared variables?

Lexical Analysis
Semantic Analysis
Code Generation
Code Optimization
Explanation - Semantic Analysis checks whether identifiers are declared before use and validates their scope.
Correct answer is: Semantic Analysis

Q.24 The main task of Code Generation is:

Creating tokens
Building a symbol table
Producing machine code
Checking grammar
Explanation - Code Generation translates intermediate representation into target machine code or assembly.
Correct answer is: Producing machine code

Q.25 Which phase of the compiler can use peephole optimization?

Lexical Analysis
Syntax Analysis
Code Optimization
Semantic Analysis
Explanation - Peephole optimization scans small windows of code to remove inefficiencies and redundancies.
Correct answer is: Code Optimization