0% found this document useful (0 votes)
81 views

Compiler Design_Sample MCQ

compiler design

Uploaded by

Salih Akadar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Compiler Design_Sample MCQ

compiler design

Uploaded by

Salih Akadar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Jimma Institute of Technology

Faculty of Computing and Informatics


Computer Science Program

Compiler Design (CoSc4103)

Sample MCQ

Compiled By: Alemisa Endebu (MSc)

June 14, 2023


Jimma, Ethiopia
General Objective of the course
▪ Understand the fundamental principles of compiler design, its various constituent parts,
algorithms, and data structures that must be used in the compiler, and provide the skills
required for building compilers for various situations encountered in a computer science
career.

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.

Answer: C The process of breaking the source code into tokens.


Explanation: Lexical analysis in compiler design is the process of breaking the source
code into tokens, which are the basic units of meaning in the programming language.
4. Which of the following is a task performed by a lexer in compiler design?
A. Parsing the source code.
B. Checking the syntax of the source code.
C. Breaking the source code into a sequence of tokens.
D. Optimizing the generated code.
Answer: C Breaking the source code into a sequence of tokens.
Explanation: A lexer or lexical analyzer is responsible for reading the source code and
generating a stream of tokens that represents the basic syntactic units of the programming
language. The lexer scans the input character-by-character and applies a set of rules to
identify the tokens in the input stream.
5. Which of the following is not a primary function of a lexer?
A. Identifying tokens C. Reporting errors
B. Parsing source code D. Ignoring comments E. A and C
Answer E: Identifying tokens and Reporting errors
Explanation: The primary function of a lexer is usually to identify tokens and report errors.
Parsing source code and ignoring comments are secondary functions.
6. Which of the following is not a type of token that can be generated by a lexer?
A. Operator B. Identifier C. Line number D. Keyword
Answer C: Line number.
Explanation: The lexer can generate a variety of tokens such as keywords, identifiers,
punctuations, symbols, literals, and operators, but not line number.
7. What is a lexer in a compiler design?
A. It is a component that reads character streams from a source file and converts them into
tokens.
B. It is a component that optimizes the generated machine code to improve execution speed.
C. It is a component that applies syntactic rules to validate the correctness of the source code.
D. It is a component that generates intermediate language code from the source code.
Answer: A. It is a component that reads character streams from a source file and converts
them into tokens. Explanation: A lexer, also known as a lexical analyzer, is an essential
component of a compiler design that reads the characters from the source code file and
converts them into tokens or lexemes which are the smallest meaningful units of a
programming language. These tokens are then used as input by the parser to build a parse
tree.
8. Which of the following is not an advantage of using a lexer in compiler design?
A. Faster compilation time B. Better accuracy in tokenization
B. Easy error detection D. Reduced volume of generated code
Answer: D Reduced volume of generated code.
Explanation: Lexers help to improve the accuracy of parsing the source code, detecting errors
and perform rapid translations from text to tokens for use by other parts of the compiler.
However, it does not reduce the volume of generated code.
9. Which of the following software tools are typically used for compiler design?
A. Lex and Yacc C. Visual Studio and Eclipse
B. Adobe Photoshop and Sketch D. WordPress
Answer: A Lex and Yacc
Explanation: Lex and Yacc are two common Unix-based tools used for compiler design. Lex is a lexical
analyzer used for generating lexical analyzers or tokenizers, while Yacc is a parser generator used for
creating LR parsers. Together, they make up the core components of the lexical and syntactic analysis
phases of a compiler.
10. Which of the following is a common tool used for implementing a lexer?
A. Compiler B. Interpreter C. Scanner generator D. Debugger
Answer C: Scanner generator.
Explanation: Scanner generators such as Flex and JFlex are widely used in compiler
design to automate the process of generating a lexer or lexical analyzer from a set of
regular expressions.
11. What is a token in lexical analysis?
A. A sequence of characters C. A programming construct
B. A data type D. The smallest meaningful unit of a programming language
Answer D: The smallest meaningful unit of a programming language.

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.

12. What is a regular expression in lexical analysis?


A. A pattern used to define a set of valid tokens
B. A type of token
C. A programming language construct
D. A type of error in the source code
Answer A: A pattern used to define a set of valid tokens.
Explanation: A regular expression is a pattern or a string of symbols that specify a set of valid
input strings for the lexer. Regular expressions are used to define the rules for identifying
tokens in the source code.
13. Which of the following is a common notation used for specifying regular expressions?
A. BNF B. EBNF C. Regex D. Grammar
Answer C: Regex.
Explanation: Regex or regular expressions are patterns used to match character combinations
in strings. It is a common notation used for specifying the rules for identifying tokens in the
source code in the lexer.
14. Which of the following is not a component of a regular expression?
A. Symbols B. Quantifiers C. Keywords D. Alternation
Answer C: Keywords.
Explanation: The components of a regular expression are simple characters,
metacharacters, quantifiers, character classes or sets, anchors, alternations, and grouping
operators, but not keywords.
15. Which of the following is not a feature of a lexical analyzer?
A. Tokenizing the source code
B. Removing comments and preprocessor directives
C. Checking the semantics of the source code
D. Ignoring white space
Answer C: Checking the semantics of the source code.
Explanation: A lexical analyzer or lexer is responsible for generating a token stream from
the input source code. It performs tasks such as ignoring white space, removing comments,
and identifying lexical errors in the source code. It does not check the semantics of the
source code, which is the role of the semantic analyzer.
16. What is a reserved word in lexical analysis?
A. A keyword that has a special meaning in a programming language
B. A word that cannot be used as an identifier
C. A word that describes the syntax of a programming language
D. A word that must be used in a specific order in a program
Answer A: keyword that has a special meaning in a programming language.
17. Which of the following is a common tool used for generating lexical analyzers?
A. Lex B. Yacc C. Bison D. Clang
Answer A: Lex (stands for lexical analyzer)
18. Which phase of a compiler comes after lexical analysis?
A. Code generation C. Semantic analysis
B. Syntax analysis D. Optimization
Answer B: Syntax analysis
19. What is the purpose of a token stream in lexer design?
A. It is the sequence of tokens that represents the source code.
B. It is a type of error in the source code.
C. It is a programming language construct.
D. It is the process of generating intermediate code from the source code.
20. Which of the following is an example of a lexical error?
A. Using a variable before it’s defined.
B. Using an undefined keyword.
C. Assigning a value to a variable of the wrong type.
D. An infinite loop.
Answer B:
Explanation: A lexical error occurs when the compiler finds an illegal character or token
in the code, such as an undefined keyword.
21. Which of the following is not an example of a token?
A. Keywords B. Identifiers C. Operators D. Syntax errors
Answer D: Explanation: Syntax errors are not tokens
22. Which of the following is true about tokenization?
A. It is a process of breaking the input source code into a stream of characters
B. It is a process of breaking the input source code into a stream of tokens
C. It is a process of breaking the input source code into a stream of errors
D. It is a process of breaking the input source code into a stream of instructions
Answer B: Explanation: Tokenization is a process of breaking the input source code into a
stream of tokens or lexemes.
23. Which of the following is true about whitespace?
A. It should be ignored by the lexer
B. It should be considered as a token
C. It can be used to separate tokens
D. None of the above
Answer A: Explanation: Whitespace should be ignored by the lexer, but it can help to
separate tokens.
24. Which of the following is true about comments?
A. They should be ignored by the lexer
B. They should be considered as a token
C. They can be used to separate tokens
D. None of the above
Answer A: Explanation: Comments should be ignored by the lexer.
25. Which of the following is true about identifiers?
A. They are reserved words in a programming language
B. They are variables or function names
C. They can start with a number
D. None of the above
Answer B: Explanation: Identifiers are variables or function names in a programming
language, and they cannot start with a number.
Chapter Three: Questions
Multiple choices questions (Related to Syntax analysis phases)
1. What is syntax analysis in compiler design?
A. The process of checking the syntax of the source code.
B. The process of generating machine code from the source code.
C. The process of debugging the program.
D. The process of analyzing the meaning of the program.
Answer A: Explanation: Syntax analysis in compiler design is the process of analyzing the
syntax of the source code to ensure that it conforms to the rules of the programming language's
grammar.
2. Which stage of the compiler design process checks if the program is syntactically correct?
A. Code Generation
B. Syntax Analysis
C. Semantic Analysis
D. Lexical Analysis
Answer: B. Syntax Analysis
3. What is parsing in compiler design?
A. The process of analyzing the syntax of the source code to determine its grammatical
structure.
B. The process of breaking the source code into tokens.
C. The process of analyzing the meaning of the program.
D. The process of generating machine code from the source code.
Answer A: Explanation: Parsing in compiler design is the process of analyzing the syntax of
the source code to determine its grammatical structure, using a set of production rules and a
grammar.
4. Which of the following is an example of a syntax error?
A. A division by zero error C. An undefined variable
B. A misspelled keyword D. A type mismatch error
Answer B: Explanation: A syntax error is a type of error in which the source code violates the
rules of the programming language's syntax, such as a missing semicolon, a misplaced
parenthesis, or a misspelled keyword.
5. Which phase of the compiler comes after syntax analysis?
A. Code generation B. Semantic analysis C. Optimization D. Lexical analysis

Answer B: Explanation: Semantic analysis comes after syntax analysis and is followed by
code generation.

6. What is a parse tree in compiler design?


A. A tree-like structure that represents the structure of the source code according to the
language's grammar rules.
B. A tree-like structure that represents the symbols used in the program.
C. A tree-like structure that represents the performance of the program.
D. A tree-like structure used to optimize the program.

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.

7. What is a context-free grammar?


A. A set of rules for specifying the semantics of a programming language.
B. A set of rules for optimizing the performance of a program.
C. A set of rules for debugging a program.
D. A set of rules for specifying the syntax of a programming language.
Answer D: Explanation: A context-free grammar is a set of rules for specifying the syntax of
a programming language, including rules for constructing statements, expressions, and other
syntactic elements.
8. What is the difference between a terminal symbol and a non-terminal symbol in a grammar?
A. Terminal symbols represent tokens that cannot be further divided, while non-terminal
symbols represent rules for constructing valid programming constructs
B. Terminal symbols represent rules for constructing valid programming constructs, while
non-terminal symbols represent tokens that cannot be further divided
C. Terminal symbols are used in semantic analysis, while non-terminal symbols are used in
syntax analysis
D. Terminal symbols are optional in a grammar, while non-terminal symbols are required
Answer: A. Terminal symbols represent tokens that cannot be further divided, while non-
terminal symbols represent rules for constructing valid programming constructs
9. What is leftmost derivation in syntax analysis?
A. A process of constructing a parse tree from the leftmost non-terminal symbol in the input.
B. A process of constructing a parse tree from the rightmost non-terminal symbol in the
input.
C. A process of optimizing the performance of the program.
D. A process of debugging the program.
Answer A: Explanation: Leftmost derivation in syntax analysis is a process of constructing a
parse tree from the leftmost non-terminal symbol in the input, using the production rules of
the grammar to generate a sentence that matches the input.
10. What is a shift-reduce conflict in syntax analysis?
A. A conflict that arises when both shift and reduce actions are possible in a given state
of the parser.
B. A conflict that arises when a grammar rule is ambiguous.
C. A conflict that arises when a grammar rule is left-recursive.
D. A conflict that arises when a grammar rule is right-recursive.
Answer A: Explanation: A shift-reduce conflict in syntax analysis is a conflict that arises
when both shift and reduce actions are possible in a given state of the parser, causing the
parser to choose between two possible actions.
11. What is a reduce-reduce conflict in syntax analysis?
A. A conflict that arises when both shift and reduce actions are possible in a given state
of the parser.
B. A conflict that arises when two or more production rules can be used to reduce the
same input.
C. A conflict that arises when a grammar rule is ambiguous.
D. A conflict that arises when a grammar rule is left-recursive.
Answer B: Explanation: A reduce-reduce conflict in syntax analysis is a conflict that arises
when two or more production rules can be used to reduce the same input, causing the parser to
choose between two possible reductions.
12. What is an LR parser in syntax analysis?
A. A parser that uses a top-down parsing technique to construct a parse tree.
B. A parser that checks the syntax of the program based on a regular expression.
C. A parser that uses a bottom-up parsing technique to construct a parse tree.
D. A parser that optimizes the performance of the program.
Answer C: Explanation: An LR parser in syntax analysis is a parser that uses a
bottom-up parsing technique to construct a parse tree, based on a LR grammar or LR
parsing table.
13. What is top-down parsing?
A. A parsing technique that starts with the root of the parse tree and works down to the
leaves.
B. A parsing technique that starts with the leaves of the parse tree and works up to the root.
C. A parsing technique that works by replacing a string of input with a production from a
grammar.
D. A parsing technique that uses a stack to keep track of the symbols in the parse tree.
Answer A: Explanation: Top-down parsing is a parsing technique that starts with the root
of the parse tree and works down to the leaves, using a grammar and a set of production
rules.
14. What is bottom-up parsing?
A. A parsing technique that starts with the root of the parse tree and works down to the
leaves.
B. A parsing technique that works by replacing a string of input with a production from
a grammar.
C. A parsing technique that uses a stack to keep track of the symbols in the parse tree.
D. A parsing technique that starts with the leaves of the parse tree and works up to the
root.
Answer D: Explanation: Bottom-up parsing is a parsing technique that starts with the leaves
of the parse tree and works up to the root, using a set of production rules and a stack to build
the parse tree.
15. What is a predictive parser in top-down parsing?
A. A parser that uses a LR(1) grammar to parse the input.
B. A parser that uses a stack to keep track of the symbols in the parse tree.
C. A parser that uses a grammar and a set of production rules.
D. A parser that uses a LL(1) grammar to parse the input.
Answer D: Explanation: A predictive parser in top-down parsing is a parser that uses a LL(1)
grammar to parse the input, predicting which production rule to use based on the next input
token.
16. What is a recursive descent parser in top-down parsing?
A. A parser that uses a stack to keep track of the symbols in the parse tree.
B. A parser that uses a grammar and a set of production rules.
C. A parser that uses a LL(1) grammar to parse the input.
D. A parser that uses a set of recursive functions to parse the input.
Answer D: Explanation: A recursive descent parser in top-down parsing is a parser that uses
a set of recursive functions to parse the input, each function corresponding to a non-terminal
symbol in the grammar.
17. What is a grammar in syntax analysis?
A. A set of rules for specifying the semantics of a programming language.
B. A set of rules for specifying the syntax of a programming language.
C. A set of rules for converting intermediate code into machine code.
D. A set of rules for debugging a program.
Answer B: Explanation: A grammar in syntax analysis is a set of rules for specifying the
syntax of a programming language, including rules for constructing statements, expressions,
and other syntactic elements.
18. What is a production rule in syntax analysis?
A. A rule that specifies how to derive a sentence from a non-terminal symbol in a
grammar.
B. A rule that specifies how to optimize the performance of a program.
C. A rule that specifies how to check the syntax of a program.
D. A rule that specifies how to break the source code into tokens.
Answer A: Explanation: A production rule in syntax analysis is a rule that specifies how to
derive a sentence from a non-terminal symbol in a grammar, using other symbols and
production rules from the grammar.
19. What is left recursion in syntax analysis?
A. A grammar rule that causes a non-terminal symbol to generate itself as the first symbol in
its production rule.
B. A conflict that arises when both shift and reduce actions are possible in a given parsing
state.
C. A conflict that arises when two or more production rules can be used to reduce the same
input.
D. A rule that specifies how to derive a sentence from a non-terminal symbol in a grammar.
Answer A: Explanation: Left recursion in syntax analysis is a grammar rule that causes a
non-terminal symbol to generate itself as the first symbol in its production rule, which can
cause the parser to loop indefinitely.

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 C: Explanation: The language of regular expressions is a regular language, not a


context-free language.
26. Which of the following is true about LR 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. C and D

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.

29. Which of the following is true about syntax trees?


A. They represent the structure of the input source code
B. They represent the structure of the parse table
C. They represent the structure of the stack
D. None of the above

Answer A: Explanation: Syntax trees represent the structure of the input source code.

30. Which of the following is an example of a non-terminal symbol?


A. Identifier B. Literal C. Operator D. Expression

Answer D: Explanation: Non-terminal symbols in a grammar represent groups of terminal


symbols or other non-terminal symbols, like "expression".

31. Which of the following is true about LL parsing?


A. It never requires backtracking
B. It may require backtracking
C. It always requires backtracking
D. None of the above
Answer B: Explanation: LL parsing may require backtracking in case of ambiguity.
32. Which of the following is true about LR parsing?
A. It may require backtracking
B. It never requires backtracking
C. It always requires backtracking
D. None of the above
Answer B: Explanation: LR parsing never requires backtracking.
33. Which of the following is true about a shift-reduce conflict?
A. The parser has to shift the current token
B. The parser has to reduce the current token
C. The parser is not able to resolve the conflict
D. None of the above

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.

35. Which of the following is an example of a right-recursive grammar rule?


A. E -> E * F
B. E -> F * E
C. E -> E + T
D. None of the above
Answer B: Explanation: E -> F * E is a right-recursive grammar rule.
36. What is an LL(k) parser?
A. A parser that uses a table to predict the next token
B. A parser that uses a stack to store information about the parse tree
C. A parser that uses a look-ahead buffer of k tokens to predict the next parse step
D. None of the above

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.

37. What is an LR(k) parser?


A. A parser that uses a table to predict the next token
B. A parser that uses a stack to store information about the parse tree
C. A parser that uses a look-ahead buffer of k tokens to predict the next parse step
D. None of the above
Answer C: Explanation: An LR(k) parser is a bottom-up parser that uses a look-ahead buffer of
k tokens to predict the next parse step.
38. What is the purpose of a parse table in parsing?
A. To store the mapping between tokens and their lexemes
B. To store the mapping between non-terminal symbols and their production rules
C. To store the mapping between identifiers and their scope
D. None of the above
Answer B: Explanation: A parse table is used to store the mapping between non-terminal symbols
and their production rules.
39. What is the purpose of building an abstract syntax tree during syntax analysis?
A. To check for syntax errors
B. To recognize programming constructs
C. To optimize the code
D. To generate machine code
Answer: B. To recognize programming constructs
40. Which data structure is commonly used to represent the syntactic structure of a program
during syntax analysis?
A. Linked list
B. Array
C. Binary tree
D. Graph
Answer: C. Binary tree
41. Which one of the following is not a step in LR parsing?
A. Reduce B. Shift C. Resolve D. Goto
Answer: c. Resolve (This is not a step in LR parsing)
Chapter Four & Five : Questions
Multiple choices questions (Related to Syntax Directed Translation, Type
checking and Symbol table)
1. Which of the following is a key task in semantic analysis?
A. Converting the source code into machine code.
B. Checking the syntax of the source code.
C. Optimizing the performance of the program.
D. Ensuring that the program has a valid meaning based on its context.
Answer D: Explanation: Semantic analysis is the process of ensuring that the program has
a valid meaning based on its context, including checking the types and scopes of variables
and functions.
2. Which of the following is an example of a semantic error?
A. Assigning a string to a variable declared as an integer.
B. Using an undefined keyword.
C. Missing a semicolon at the end of a line.
D. An infinite loop.
Answer A: Explanation: A semantic error occurs when there's a problem with the
meaning of a program, such as assigning the wrong type to a variable.
3. Which stage of the compiler design process checks if the program is semantically
correct?
A. Syntax Analysis
B. Code Optimization
C. Semantic Analysis
D. Lexical Analysis
Answer: C. Semantic Analysis
4. What is syntax-directed translation?
A. A technique for converting source code into machine code.
B. A technique for generating parse trees from source code.
C. A technique for adding semantic actions to a parser.
D. A technique for optimizing the performance of a program.
Answer C: Explanation: Syntax-directed translation is a technique for adding semantic
actions to a parser that perform tasks such as constructing a parse tree, computing
attributes, and generating intermediate code.
5. What are semantic actions in syntax-directed translation?
A. Actions that perform tasks such as constructing a parse tree, computing attributes,
and generating intermediate code.
B. Actions that check the syntax of the source code.
C. Actions that break down the source code into tokens.
D. Actions that optimize the performance of the program.
Answer A: Explanation: Semantic actions in syntax-directed translation perform tasks
such as constructing a parse tree, computing attributes, and generating intermediate code.
6. What does the semantic analysis phase cover?
A. Breaking down the source code into a sequence of tokens or lexemes.
B. Generation of machine code from the source code.
C. Analysis of the meaning of the program to identify and report type inconsistencies.
D. Debugging of the program.
Answer C: Explanation: The semantic analysis phase covers the analysis of the meaning
of the program to identify and report type inconsistencies.
7. What is a symbol table in compiler design?
A. A table that stores the names and attributes of all the symbols used in the program.
B. A table that lists all the syntax rules used in the program.
C. A table that stores the instructions to optimize the program.
D. A table that stores the intermediate code of the program.
Answer: A Explanation: A symbol table in compiler design is a data structure that
stores the names and attributes of all the symbols used in the program, such as
variables, functions, and data types.
8. What is the purpose of type checking in the semantic analysis phase?
B. A. To check if the syntax of the input program is correct
C. B. To find and report any syntax errors in the input program
D. C. To ensure that the types of expressions are compatible
E. D. To generate machine code from the input program
Answer: C. To ensure that the types of expressions are compatible
9. What is type checking in compiler design?
A. The process of verifying that the types of operands and operators in an expression
are compatible.
B. The process of identifying and resolving syntax errors in the source code.
C. The process of converting intermediate code into machine code.
D. The process of performing semantic analysis on the program.
Answer: A Explanation: Type checking in compiler design is the process of
verifying that the types of operands and operators in an expression are
compatible, such as ensuring that an integer is not used in place of a boolean
value.
10. What is a type system in compiler design?
A. A set of rules for identifying and resolving syntax errors in the source code.
B. A set of rules for determining the type of an expression and ensuring that types are
used correctly.
C. A set of rules for converting intermediate code into machine code.
D. A set of rules for performing semantic analysis on the program.
Answer: B Explanation: A type system in compiler design is a set of rules for
determining the type of an expression and ensuring that types are used correctly,
such as preventing the use of a string value where a numeric value is expected.
11. What is a static type system in compiler design?
A. A type system that checks the types of expressions at runtime.
B. A type system that does not check the types of expressions.
C. A type system that checks the types of expressions at compile time.
D. A type system that checks the semantics of expressions.
Answer C: Explanation: A static type system in compiler design is a type system that
checks the types of expressions at compile time, before the program is executed.
12. What is a dynamic type system in compiler design?
A. A type system that checks the types of expressions at runtime.
B. A type system that checks the types of expressions at compile time.
C. A type system that does not check the types of expressions.
D. A type system that checks the semantics of expressions.
Answer A: Explanation: A dynamic type system in compiler design is a type system
that checks the types of expressions at runtime, while the program is executing.
13. What is the difference between dynamic and static scoping?
B. A. Dynamic scoping refers to the way variables are assigned memory locations at
runtime, while static scoping refers to the way variables are assigned memory
locations at compile time
C. B. Dynamic scoping refers to the way variables are resolved at runtime, while
static scoping refers to the way variables are resolved at compile time
D. C. Dynamic scoping uses a stack to keep track of variables, while static scoping
uses a symbol table
E. D. Dynamic scoping is more efficient than static scoping
Answer: B. Dynamic scoping refers to the way variables are resolved at runtime,
while static scoping refers to the way variables are resolved at compile time
14. Which of the following is not a benefit of using a strong type system in
programming languages?
A. Improved program reliability
B. Improved type safety
C. Reduced compilation time
D. Improved program maintainability
Answer C: Explanation: Reduced compilation time is not a benefit of using a strong
type system in programming languages.
15. What is type promotion in programming languages?
A. The process of converting a value from a higher type to a lower type.
B. The process of converting a value to a different type.
C. The process of checking the type of a value.
D. The process of converting a value from a lower type to a higher type.
Answer D: Explanation: Type promotion in programming languages is the process of
converting a value from a lower type to a higher type, such as converting an integer to a
float.
Chapter Six, Seven & eight: Questions
Multiple choices questions (Related to Intermediate code generation, code
optimization and code generation)
1. What is the purpose of code optimization in compiler design?
A. To improve the efficiency of the generated code in terms of memory usage and runtime
performance.
B. To ensure the correct syntax of the program.
C. To generate machine code from the source code.
D. To detect and report errors in the code.
Answer A: Explanation: The purpose of code optimization in compiler design is to improve the
efficiency of the generated code in terms of memory usage and runtime performance.
2. What is code optimization in compiler design?
A. The process of improving the performance or size of the generated machine code.
B. The process of analyzing the syntax of the source code.
C. The process of analyzing the meaning of the program.
D. The process of breaking the source code into tokens.
Answer A: Explanation: Code optimization in compiler design is the process of improving
the performance or size of the generated machine code, usually by reordering instructions,
eliminating redundant code, or simplifying expressions.
3. What is an intermediate representation in compiler design?
A. A representation of the program between the source code and the final machine
code.
B. A representation of the program between two stages of optimization.
C. A representation of the program after execution.
D. A representation of the program for debugging purposes.
Answer A: Explanation: An intermediate representation is a representation of the program
between the source code and the final machine code, which can be used for optimization,
debugging, or other purposes.
4. What is the purpose of intermediate code generation in the compiler design process?
A. To check for syntax errors in the program
B. To generate optimized code for the program
C. To convert the program into a machine-readable format
D. To perform type-checking on the program
Answer: B. To generate optimized code for the program
5. Which of the following is an advantage of using an intermediate code?
A. Faster execution speed
B. Improved memory management
C. Improved code portability
D. Improved error detection
Answer: C. Improved code portability
6. What is the purpose of a three-address code in the intermediate code?
A. To convert the program into a machine-readable format
B. To store information about symbols in the program
C. To identify potential optimizations in the code
D. To represent expressions in a compact format
Answer: D. To represent expressions in a compact format

You might also like