Compiler Design_Sample MCQ
Compiler Design_Sample MCQ
Sample MCQ
Learning Outcome
▪ Specify and analyze the lexical, syntactic, and semantic structures of advanced language
features.
▪ Describe techniques for lexical and syntax analysis
▪ Understand the basic principles of compiler design.
▪ Describe syntax directed translation and type checking techniques
Chapter One: Questions
Multiple-choice questions (understand the basic principles of compiler design)
1. What is the purpose of a compiler in computer science?
A. To translate source code written in a programming language into machine code.
B. To optimize the performance of a computer system.
C. To convert machine code into source code.
D. To analyze and debug other programs.
Answer: A
Explanation: The purpose of a compiler in computer science is to translate source code written
in a programming language into machine code that can be executed by a computer.
2. Compiler is a program that
A. Appears to execute a source program as if it were machine language
B. Accepts a program written in a high level language and produces an object program
C. Automates the translation of assembly language into machine language
D. Places programs into memory and prepares them for execution
Answer: B
Accepts a program written in a high level language and produces an object program
3. What is a compiler?
A. A program that translates the source code of a program into executable code.
B. A program that executes other programs.
C. A program that optimizes the performance of a program.
D. A program that converts machine code into source code.
E. A program that performs debugging on a program.
Answer: A Explanation: A compiler is a program that translates the source code of a
program into executable code that can be run on a computer.
4. What is the difference between a compiler and an interpreter?
A. A compiler executes the source code directly, whereas an interpreter translates the
source code into machine code.
B. A compiler and an interpreter are the same thing.
C. A compiler translates source code into machine code, whereas an interpreter executes
the source code directly.
D. A compiler and an interpreter do not exist.
Answer: C Explanation: A compiler translates source code into machine code before
execution, whereas an interpreter executes the source code directly without translating it
into machine code first.
5. Which of the following is a stage of compiler design?
A. Semantic analysis C. Intermediate code generator
B. Code generator D. All
Answer: D Explanation: The phases of a compiler are:
1. Lexical analysis 2. Syntax analysis 3. Semantic analysis 4. Intermediate code
generator. 5. Code optimizer 6. Code generator
6. Which of the following is not a stage of the compiler design process?
A. Syntax Analysis
B. Semantic Analysis
C. Code Generation
D. Program Execution
Answer: D. Program Execution
7. Which of the following is a system program that integrates a program’s individually compiled
modules into a form that can be executed?
A. Interpreter B. Assembler C. Compiler D. Linking Loader
Answer: D
Explanation: A loader that combines the functionality of a relocation loader with the ability
to combine a number of independently compiled program segments.
8. Which of the following phase of the compiler is Syntax Analysis?
A. Second B. Third C. First D. All of the mentioned
Answer: A
Explanation: It is the Second Phase of the Compiler after Lexical Analyzer. It is also
called Hierarchical Analysis or Parsing.
9. Users write the programs in which language?
A. Decimal-Format C. Low-level Language
B. High-Level Language D. Middle-Level Language
Answer: B
Explanation: Users write the program in a high-level language because it is easier to learn, use,
understand, and remember.
10. A compiler is preferable to an interpreter because
A. If one changes a statement, only that statement needs re-compilation
B. Debugging can be fa ster and easier
C. It is much helpful in the initial stages of program development
D. It can generate stand-alone programs that often take less time for execution
Answer: D
Explanation: A compiler is preferable to an interpreter because it can generate stand-alone
programs that often take less time for execution.
11. Compiler should report the presence of ________ in the source program, in the translation
process.
A. Objects B. Classes C. Errors D. Text
Answer: C
Explanation: The compiler should report the presence of errors in the source program, in
the translation process.
12. Two parts of compilation are.
A. analysis and compilation. C. analysis and conversion.
C. static and dynamic. D. analysis and synthesis.
Answer: D Analysis and synthesis
Chapter Two: Questions
Multiple choices questions (Related to lexical analysis phases)
1. What is lexical analysis in compiler design?
A. It is the process of translating source code into machine code.
B. It is the process of converting a sequence of characters into a stream of tokens.
C. It is the process of performing semantic analysis.
D. It is the process of generating intermediate code.
Answer: B It is the process of converting a sequence of characters into a sequence of
tokens.
2. Which phase of the compilation process takes input as the source code and produces a
stream of tokens?
A. Parsing B. Lexical Analysis C. Semantic Analysis D. Code Generation
Answer: B
Explanation: The lexical analysis or scanning phase aims to break down the source code
into a sequence of tokens or lexemes, which are the smallest meaningful units of a
program.
3. What is lexical analysis in compiler design?
A. The process of analyzing the syntax of the source code.
B. The process of analyzing the meaning of the program.
C. The process of breaking the source code into tokens.
D. The process of generating machine code from the source code.
Explanation: A token is the smallest unit of a programming language that has a meaning,
typically consisting of an identifier, keyword, operator, constant, or symbol.
Answer B: Explanation: Semantic analysis comes after syntax analysis and is followed by
code generation.
Answer A: Explanation: A parse tree in compiler design is a tree-like structure that represents the
structure of the source code according to the language's grammar rules.
20. An error that occurs in the source code but is not captured by the scanner or the parser is
called:
A. A lexical error. B. A syntax error. C. A semantic error. D. A parsing error.
Answer C: Explanation: An error that occurs in the source code but is not caught by the
scanner or parser is typically a semantic error caused by invalid assignments, wrong return
types, or incompatible data types.
21. Which of the following is not a primary function of a parser?
A. Reporting errors B. Identifying tokens
B. Building the parse tree D. Checking syntax rules
Answer B: Explanation: Identifying tokens is the primary function of a lexer, not a parser.
22. Which of the following is an example of a context-free grammar?
A. Regular expression B. BNF C. DFA D. NFA
Answer B: Explanation: BNF (Backus-Naur Form) is an example of a context-free grammar.
23. What is the role of a parser in the compilation process?
A. Translate code into machine language
B. Translate code into assembly language
C. Translate code into tokens
D. Build the parse tree
Answer D: Explanation: The parser is responsible for building the parse tree from the input
source code.
24. Which of the following is true about LL parsing?
A. It is top-down parsing C. It is bottom-up parsing
B. It uses a parse table D. It uses a stack E. A and D
Answer E: Explanation: LL parsing is top-down parsing that uses a parse table and a stack to
build the parse tree.
25. Which of the following is not a context-free language?
A. The language of arithmetic expressions
B. The language of HTML tags
C. The language of regular expressions
D. The language of C programs
Answer E: Explanation: LR parsing is bottom-up parsing that uses a parse table and a stack to
build the parse tree.
27. Which of the following is an example of a shift-reduce conflict in parsing?
A. Reduce-reduce conflict
B. Shift-shift conflict
C. Reduce-shift conflict
D. None of the above
Answer D: Explanation: A shift-reduce conflict occurs when the parser has the option to
either shift or reduce the current token.
28. Which of the following is true about LR parsing?
A. It is more powerful than LL parsing
B. It is less powerful than LL parsing
C. It is equally powerful as LL parsing
D. None of the above
Answer A: Explanation: LR parsing is more powerful than LL parsing, as it can handle more
complex grammars.
Answer A: Explanation: Syntax trees represent the structure of the input source code.
Answer D: Explanation: A shift-reduce conflict happens when the parser has the option to
either shift or reduce the current token.
33. Which of the following is an example of a left-recursive grammar rule?
A. E -> E + T
B. E -> T + E
C. E -> E * F
D. A and C
Answer D: Explanation: E -> E + T and E -> E * F is a left-recursive grammar rule.
34. Which of the following is true about grammar ambiguity?
A. It can cause shift-reduce conflicts
B. It can cause reduce-reduce conflicts
C. It can cause parsing errors
D. All of the above
Answer D: Explanation: Grammar ambiguity can cause shift-reduce conflicts and reduce-
reduce conflicts, as well as parsing errors.
Answer C: Explanation: An LL(k) parser is a top-down parser that uses a look-ahead buffer of k
tokens to predict the next parse step.