Q.1 Which of the following best defines a compiler?
A program that executes machine code directly
A program that translates high-level language into machine code
A program that controls computer hardware
A program that manages files and processes
Explanation - A compiler is responsible for converting source code written in a high-level language into executable machine code.
Correct answer is: A program that translates high-level language into machine code
Q.2 The process of analyzing the structure of source code is called:
Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Optimization
Explanation - Syntax analysis (parsing) checks the structure of code against the grammar of the language.
Correct answer is: Syntax Analysis
Q.3 Which phase of the compiler checks for variable type mismatches?
Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Generation
Explanation - Semantic analysis ensures type compatibility and checks the meaning of the statements.
Correct answer is: Semantic Analysis
Q.4 Intermediate code is generated in compiler design to:
Improve readability of the source code
Provide machine independence
Reduce execution time
Avoid syntax errors
Explanation - Intermediate code allows the compiler to be retargeted for multiple architectures by separating machine-independent optimization from machine-dependent code generation.
Correct answer is: Provide machine independence
Q.5 Which of the following is NOT a phase of compilation?
Lexical Analysis
Syntax Analysis
Linking
Code Generation
Explanation - Linking is performed after compilation, not within the compiler phases.
Correct answer is: Linking
Q.6 Lexical analysis uses which data structure?
Parse Tree
Symbol Table
Finite Automata
Stack
Explanation - Lexical analyzers use finite automata to recognize tokens in the input stream.
Correct answer is: Finite Automata
Q.7 What is the role of the symbol table in a compiler?
It stores error messages
It maintains identifiers and their attributes
It generates machine code
It optimizes intermediate code
Explanation - The symbol table stores variables, constants, functions, and other identifiers along with their types and scopes.
Correct answer is: It maintains identifiers and their attributes
Q.8 Which phase is responsible for translating IR to target code?
Lexical Analysis
Semantic Analysis
Intermediate Code Generation
Code Generation
Explanation - The code generation phase translates intermediate representation into the machine code of the target architecture.
Correct answer is: Code Generation
Q.9 Which of these errors can be detected during lexical analysis?
Type Mismatch
Unrecognized Token
Undeclared Variable
Division by Zero
Explanation - Lexical analysis identifies invalid sequences of characters that don’t match any token definition.
Correct answer is: Unrecognized Token
Q.10 Which compiler phase uses context-free grammar?
Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Optimization
Explanation - Syntax analysis uses context-free grammar to build parse trees for source code.
Correct answer is: Syntax Analysis
Q.11 The front end of a compiler generally consists of:
Lexical, Syntax, and Semantic Analysis
Optimization and Code Generation
Linker and Loader
Assembler and Preprocessor
Explanation - The front end performs analysis of the code, while the back end focuses on optimization and code generation.
Correct answer is: Lexical, Syntax, and Semantic Analysis
Q.12 A compiler translates:
Source code to byte code
Source code to object code
Object code to source code
Assembly to source code
Explanation - Compilers take high-level source code and convert it into low-level object code or machine code.
Correct answer is: Source code to object code
Q.13 Which optimization technique eliminates common sub-expressions?
Constant Folding
Dead Code Elimination
Common Subexpression Elimination
Loop Unrolling
Explanation - This optimization identifies and removes repeated computations to improve efficiency.
Correct answer is: Common Subexpression Elimination
Q.14 Which is a compiler construction tool?
YACC
MS Word
Notepad
Excel
Explanation - YACC (Yet Another Compiler Compiler) is a tool for generating parsers used in compiler construction.
Correct answer is: YACC
Q.15 Errors that can be detected at compile-time are:
Logical Errors
Syntax Errors
Runtime Errors
Hardware Failures
Explanation - Syntax errors, such as missing semicolons, are detected at compile-time.
Correct answer is: Syntax Errors
Q.16 Which phase is responsible for type checking?
Lexical Analysis
Syntax Analysis
Semantic Analysis
Code Generation
Explanation - Semantic analysis checks for type correctness and ensures valid usage of identifiers.
Correct answer is: Semantic Analysis
Q.17 What is the output of lexical analysis?
Intermediate Code
Tokens
Parse Tree
Assembly Code
Explanation - Lexical analysis outputs tokens which are then passed to the parser for syntax analysis.
Correct answer is: Tokens
Q.18 A Just-In-Time (JIT) compiler is used in:
C
Python
Java
Assembly
Explanation - Java uses JIT compilation to convert bytecode into native machine code at runtime.
Correct answer is: Java
Q.19 Which part of compiler deals with register allocation?
Lexical Analysis
Code Optimization
Syntax Analysis
Symbol Table
Explanation - Register allocation is performed during code optimization to efficiently use CPU registers.
Correct answer is: Code Optimization
Q.20 Which is NOT an advantage of using intermediate code?
Machine independence
Simplifies optimization
Eliminates syntax errors
Supports portability
Explanation - Syntax errors are already handled in earlier phases; intermediate code focuses on portability and optimization.
Correct answer is: Eliminates syntax errors
Q.21 Which of the following tools is used for lexical analysis?
Lex
Yacc
GCC
Assembler
Explanation - Lex is a tool specifically designed to generate lexical analyzers.
Correct answer is: Lex
Q.22 The compiler phase responsible for loop optimization is:
Lexical Analysis
Semantic Analysis
Code Optimization
Code Generation
Explanation - Loop optimizations such as unrolling and invariant code motion are part of code optimization.
Correct answer is: Code Optimization
Q.23 Which representation is easier for machine-dependent optimization?
High-level source code
Intermediate code
Machine code
Tokens
Explanation - Intermediate representation is designed to support both machine-independent and dependent optimizations.
Correct answer is: Intermediate code
Q.24 Which is an example of a compiler error?
Segmentation Fault
Type Mismatch
Infinite Loop
Stack Overflow
Explanation - Type mismatches are detected during compilation, not execution.
Correct answer is: Type Mismatch
Q.25 Which one is a goal of compiler design?
Fast execution of compiled code
Complex syntax rules
Slower parsing process
Increased hardware dependency
Explanation - A key goal of compiler design is to produce optimized code that executes efficiently.
Correct answer is: Fast execution of compiled code
