0% found this document useful (0 votes)
69 views14 pages

Compiler Design MCQ Based On Learning Outcome

The document outlines key concepts in lexical, syntactic, and semantic analysis within compiler design, including the roles of lexical analysis, parsing techniques, and the purpose of symbol tables. It also provides a series of questions and answers to test understanding of these concepts, highlighting important distinctions such as between lexical tokens and high-level constructs, as well as the differences between syntactic and semantic errors. Additionally, it discusses various parsing techniques and their capabilities, including handling left-recursive grammars.

Uploaded by

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

Compiler Design MCQ Based On Learning Outcome

The document outlines key concepts in lexical, syntactic, and semantic analysis within compiler design, including the roles of lexical analysis, parsing techniques, and the purpose of symbol tables. It also provides a series of questions and answers to test understanding of these concepts, highlighting important distinctions such as between lexical tokens and high-level constructs, as well as the differences between syntactic and semantic errors. Additionally, it discusses various parsing techniques and their capabilities, including handling left-recursive grammars.

Uploaded by

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

Learning outcome 1 : Specify and analyze the lexical, syntactic and semantic structures of

advanced language features


1. Lexical analysis is responsible for:
a. Analyzing the syntactic structure of a program
b. Analyzing the semantics of a program
c. Breaking the input program into meaningful tokens
d. Generating intermediate code

2. Which of the following is not a lexical token?


a. Identifier
b. Operator
c. Keyword
d. Loop

3. The process of grouping tokens into higher-level language constructs is called:


a. Parsing
b. Lexing
c. Tokenizing
d. Compiling

4. Which of the following is not a common approach to specify lexical structures?


a. Regular expressions
b. Context-free grammars
c. Deterministic finite automata
d. Backus-Naur Form (BNF)

5. The purpose of a lexer generator is to:


a. Generate a lexical analyzer based on regular expressions
b. Generate an optimized parser for a given grammar
c. Generate code for intermediate representation
d. Generate machine code for the target architecture

6. The output of the lexical analysis phase is:


a. Abstract Syntax Tree (AST)
b. Symbol Table
c. Token stream
d. Intermediate code

7. Which of the following is an example of a syntactic error?


a. Using an undefined variable
b. Dividing a number by zero
c. Forgetting to close a parenthesis
d. Misplacing a semicolon

8. The purpose of a parser in compiler design is to:


a. Check for syntax errors and build a parse tree
b. Generate machine code from the source program
c. Optimize the intermediate representation
d. Execute the program directly
9. Which of the following is not a commonly used parsing technique?
a. Recursive descent parsing
b. Shift-reduce parsing
c. LR parsing
d. Lexical analysis

10. The output of the parsing phase is typically represented as a:


a. Symbol table
b. Parse tree
c. Abstract Syntax Tree (AST)
d. Token stream

11. Semantic analysis in a compiler is responsible for:


a. Checking for syntax errors in the program
b. Translating the source program into machine code
c. Analyzing the meaning of the program
d. Generating an optimized intermediate representation

12. Which of the following is not a semantic error?


a. Type mismatch in an assignment statement
b. Accessing an array out of bounds
c. Using a reserved keyword as an identifier
d. Missing a semicolon at the end of a statement

13. The purpose of symbol table in semantic analysis is to:


a. Store the intermediate representation of the program
b. Store the lexical tokens of the program
c. Store information about variables and their types
d. Store the parse tree of the program

14. Type checking in semantic analysis involves:


a. Ensuring variables are declared before use
b. Checking if the operands of an operator have compatible types
c. Transforming the source program into an equivalent intermediate representation
d. Reordering instructions for better performance

15. The final output of the compiler is usually:


a. The optimized source program
b. The machine code for the target architecture
c. The intermediate representation of the program
d. The parse tree of the program
Answers
1. Lexical analysis is responsible for:
Answer: c. Breaking the input program into meaningful tokens
Explanation: Lexical analysis is the first phase of the compiler that scans the input program and breaks it into
meaningful tokens (also known as lexemes) such as identifiers, keywords, operators, literals, etc.

2. Which of the following is not a lexical token?


Answer: d. Loop
Explanation: "Loop" is not a lexical token; it is a high-level language construct or a statement. Lexical tokens are
basic building blocks recognized by the lexer, such as identifiers, keywords, operators, etc.

3. The process of grouping tokens into higher-level language constructs is called:


Answer: a. Parsing
Explanation: Parsing is the process of analyzing the syntax of a program by grouping tokens into higher-level
language constructs, such as expressions, statements, and functions.

4. Which of the following is not a common approach to specify lexical structures?


Answer: d. Backus-Naur Form (BNF)
Explanation: Backus-Naur Form (BNF) is used to define the syntax or grammatical structure of a language, not
lexical structures. Lexical structures are typically defined using regular expressions or deterministic finite automata.

5. The purpose of a lexer generator is to:


Answer: a. Generate a lexical analyzer based on regular expressions
Explanation: A lexer generator takes a set of regular expressions and generates a lexical analyzer (lexer)
automatically, which can recognize and tokenize the input program based on those regular expressions.

6. The output of the lexical analysis phase is:


Answer: c. Token stream
Explanation: The output of the lexical analysis phase is a stream of tokens (token stream) that represents the
meaningful units of the input program.

7. Which of the following is an example of a syntactic error?


Answer: d. Misplacing a semicolon
Explanation: A syntactic error occurs due to violations of the language's grammar rules, such as missing or
misplaced symbols, incorrect order of statements, etc. Misplacing a semicolon is an example of a syntactic error.

8. The purpose of a parser in compiler design is to:


Answer: a. Check for syntax errors and build a parse tree
Explanation: A parser is responsible for analyzing the syntactic structure of the input program, checking for syntax
errors, and constructing a parse tree (or abstract syntax tree) that represents the program's hierarchical structure.

9. Which of the following is not a commonly used parsing technique?


Answer: d. Lexical analysis
Explanation: Lexical analysis is not a parsing technique. It is the first phase of the compiler that breaks the input
program into tokens. Common parsing techniques include recursive descent parsing, shift-reduce parsing, and LR
parsing.

10. The output of the parsing phase is typically represented as a:


Answer: c. Abstract Syntax Tree (AST)
Explanation: The output of the parsing phase is usually represented as an Abstract Syntax Tree (AST) that reflects
the hierarchical structure of the input program.

11. Semantic analysis in a compiler is responsible for:


Answer: c. Analyzing the meaning of the program
Explanation: Semantic analysis checks the meaning of the program, enforces language-specific rules, and performs
type checking to ensure the program is semantically valid.

12. Which of the following is not a semantic error?


Answer: d. Missing a semicolon at the end of a statement
Explanation: Missing a semicolon is a syntactic error, not a semantic error. Semantic errors involve incorrect
program behavior due to inappropriate use of variables, types, or language rules.

13. The purpose of the symbol table in semantic analysis is to:


Answer: c. Store information about variables and their types
Explanation: The symbol table is a data structure used by the compiler to store information about variables, their
types, scopes, and other relevant information needed during semantic analysis.

14. Type checking in semantic analysis involves:


Answer: b. Checking if the operands of an operator have compatible types
Explanation: Type checking involves ensuring that the operands of an operator have compatible types, verifying
type consistency in assignments, expressions, and function calls, among other type-related checks.

15. The final output of the compiler is usually:


Answer: b. The machine code for the target architecture
Explanation: The final output of a compiler is typically the machine code (executable code) for the target
architecture, which can be directly executed by the computer or deployed to a specific platform.

Learning outcome 2 - Describe techniques for lexical and syntax analysis

1. Lexical analysis is the process of:


a. Checking the syntax of a program
b. Breaking the program into meaningful tokens
c. Analyzing the meaning of the program
d. Generating machine code

2. Which of the following is not a role of the lexer in a compiler?


a. Identifying keywords
b. Removing comments from the source code
c. Grouping characters into tokens
d. Constructing the parse tree

3. Regular expressions are commonly used in compiler design for:


a. Defining the grammar of the language
b. Tokenizing the input program
c. Optimizing the generated code
d. Checking semantic errors

4. Which of the following is not a common token category?


a. Identifier
b. Operator
c. Literal
d. Statement

5. The purpose of a symbol table in lexical analysis is to:


a. Store the values of constants
b. Track the position of each token
c. Store information about identifiers
d. Keep track of the program's execution flow

6. A lexical analyzer generator, such as Flex, is used to:


a. Generate optimized machine code
b. Perform semantic analysis
c. Generate a lexer based on regular expressions
d. Implement the syntax of a programming language

7. Which of the following is not a typical tokenization error?


a. Missing a semicolon at the end of a statement
b. Using a reserved keyword as an identifier
c. Accessing an array out of bounds
d. Forgetting to close a parenthesis

8. Which of the following statements about syntax analysis is true?


a. It ensures the program is free of semantic errors
b. It analyzes the meaning of the program
c. It checks the program's adherence to the language grammar
d. It optimizes the generated code

9. The most common technique used for syntax analysis is:


a. Recursive descent parsing
b. Bottom-up parsing
c. Top-down parsing
d. LR parsing

10. Which of the following is not an advantage of top-down parsing?


a. Simplicity of implementation
b. Immediate error detection
c. Handles left-recursive grammars efficiently
d. Supports left factoring

11. Which of the following is not a top-down parsing algorithm?


a. LL(1)
b. LL(k)
c. SLR(1)
d. Predictive parsing
12. Bottom-up parsing is also known as:
a. Shift-reduce parsing
b. Recursive descent parsing
c. Top-down parsing
d. LL(1) parsing

13. LR parsing is based on:


a. Deterministic finite automata (DFA)
b. Non-deterministic finite automata (NFA)
c. Pushdown automata (PDA)
d. Turing machines

14. Which of the following parsing techniques can handle left-recursive grammars?
a. Recursive descent parsing
b. LL(1) parsing
c. LR(0) parsing
d. SLR(1) parsing

15. A parse tree represents:


a. The order of statements in the program
b. The hierarchical structure of the program
c. The optimized version of the program
d. The intermediate code of the program

16. Which of the following is not a valid action in shift-reduce parsing?


a. Shift
b. Reduce
c. Accept
d. Merge

17. The operator precedence parsing technique uses:


a. Regular expressions
b. Operator precedence rules
c. Pushdown automata
d. Deterministic finite automata

18. An ambiguous grammar:


a. Allows multiple parse trees for the same input
b. Is not suitable for bottom-up parsing
c. Leads to infinite loops during parsing
d. Results in runtime errors

19. Which of the following is not a technique for resolving shift-reduce conflicts in LR parsing?
a. Precedence declarations
b. Associativity declarations
c. Operator precedence rules
d. Lookahead symbols
20. Which of the following is not a strategy for error recovery in parsing?
a. Panic mode
b. Error propagation
c. Error correction
d. Backtracking

Answers with explanations

1. Lexical analysis is the process of:


Answer: b. Breaking the program into meaningful tokens
Explanation: Lexical analysis involves scanning the input program and dividing it into individual tokens or
lexemes that hold meaning in the language.

2. Which of the following is not a role of the lexer in a compiler?


Answer: d. Constructing the parse tree
Explanation: The lexer's primary role is to identify tokens and group characters into lexemes. Constructing
the parse tree is a responsibility of the parser.

3. Regular expressions are commonly used in compiler design for:


Answer: b. Tokenizing the input program
Explanation: Regular expressions are used to define patterns for recognizing and tokenizing different parts
of the input program during lexical analysis.

4. Which of the following is not a common token category?


Answer: d. Statement
Explanation: Tokens typically include identifiers, operators, and literals, but "statement" is a higher-level
language construct that is composed of multiple tokens.

5. The purpose of a symbol table in lexical analysis is to:


Answer: c. Store information about identifiers
Explanation: The symbol table is used to store information about identifiers, such as their names, types,
and memory locations.

6. A lexical analyzer generator, such as Flex, is used to:


Answer: c. Generate a lexer based on regular expressions
Explanation: Lexical analyzer generators take a set of regular expressions and automatically generate a
lexer program that can tokenize the input based on those regular expressions.

7. Which of the following is not a typical tokenization error?


Answer: c. Accessing an array out of bounds
Explanation: Accessing an array out of bounds is a semantic error that occurs during later stages of the
compilation process. Tokenization errors involve incorrect recognition or grouping of characters into
tokens.

8. Which of the following statements about syntax analysis is true?


Answer: c. It checks the program's adherence to the language grammar
Explanation: Syntax analysis, also known as parsing, verifies if the structure of the program follows the
grammar rules of the language being compiled.
9. The most common technique used for syntax analysis is:
Answer: a. Recursive descent parsing
Explanation: Recursive descent parsing involves building a parse tree from the top-down, starting from
the program's highest-level construct and recursively expanding it.

10. Which of the following is not an advantage of top-down parsing?


Answer: c. Handles left-recursive grammars efficiently
Explanation: Left-recursive grammars pose challenges for top-down parsing. Other advantages of top-
down parsing include simplicity of implementation and immediate error detection.

11. Which of the following is not a top-down parsing algorithm?


Answer: c. SLR(1)
Explanation: SLR(1) is a bottom-up parsing algorithm. Top-down parsing algorithms include LL(1), LL(k),
and predictive parsing.

12. Bottom-up parsing is also known as:


Answer: a. Shift-reduce parsing
Explanation: In bottom-up parsing, the parser starts from the input and repeatedly applies shift or reduce
operations to construct the parse tree.

13. LR parsing is based on:


Answer: c. Pushdown automata (PDA)
Explanation: LR parsing utilizes a pushdown automaton to handle a larger class of grammars, allowing for
more powerful parsing capabilities than LL parsing.

14. Which of the following parsing techniques can handle left-recursive grammars?
Answer: a. Recursive descent parsing
Explanation: Recursive descent parsing can handle left-recursive grammars by using appropriate
techniques such as left-factoring or recursion elimination.

15. A parse tree represents:


Answer: b. The hierarchical structure of the program
Explanation: A parse tree depicts the hierarchical structure of a program, illustrating how different
language constructs are nested within each other.

16. Which of the following is not a valid action in shift-reduce parsing?


Answer: d. Merge
Explanation: Shift-reduce parsing involves shifting input tokens onto the parsing stack or reducing a set of
tokens on the stack to a higher-level language construct.

17. The operator precedence parsing technique uses:


Answer: b. Operator precedence rules
Explanation: Operator precedence parsing determines the order of evaluating operators based on their
precedence and associativity rules.

18. An ambiguous grammar:


Answer: a. Allows multiple parse trees for the same input
Explanation: An ambiguous grammar is one that can produce more than one valid parse tree for the same
input, making it difficult to determine the intended structure and meaning.
19. Which of the following is not a technique for resolving shift-reduce conflicts in LR parsing?
Answer: d. Lookahead symbols
Explanation: Lookahead symbols are used in LR parsing to determine the appropriate action when shift-
reduce conflicts occur. Precedence and associativity declarations, as well as operator precedence rules,
help resolve such conflicts.

20. Which of the following is not a strategy for error recovery in parsing?
Answer: c. Error correction
Explanation: Error correction typically refers to techniques used in code editors or integrated
development environments (IDEs) to suggest fixes for detected syntax errors. In parsing, error recovery
strategies include panic mode, error propagation, and backtracking.

Learning outcome 3 : Understand the basic principles of compiler design

21. What is the main purpose of a compiler?


a. To execute a program
b. To interpret a program
c. To translate a program into machine code
d. To debug a program

22. Which of the following is not a phase of the compiler?


a. Lexical analysis
b. Semantic analysis
c. Code optimization
d. Debugging

23. The intermediate representation used by a compiler is:


a. Assembly language
b. Machine code
c. High-level language
d. Abstract syntax tree

24. The process of converting a high-level programming language into machine code is called:
a. Compilation
b. Interpretation
c. Execution
d. Debugging

25. Which of the following is not a typical optimization performed by a compiler?


a. Loop unrolling
b. Constant folding
c. Dead code elimination
d. Syntax analysis

26. What is the role of the optimizer in a compiler?


a. To generate machine code
b. To improve the efficiency of the generated code
c. To check for semantic errors
d. To interpret the program

27. Which of the following is an example of a static analysis performed by a compiler?


a. Type checking
b. Code generation
c. Register allocation
d. Dynamic memory allocation

28. The process of allocating registers to variables is known as:


a. Lexical analysis
b. Syntax analysis
c. Code optimization
d. Code generation

29. Which of the following is not a common programming language paradigm?


a. Imperative
b. Functional
c. Object-oriented
d. Compiler-oriented

30. Which phase of the compiler detects syntax errors in the input program?
a. Lexical analysis
b. Semantic analysis
c. Syntax analysis
d. Code generation

31. What is the role of the symbol table in compiler design?


a. To store variable values at runtime
b. To store intermediate code instructions
c. To store information about identifiers and their attributes
d. To store machine code instructions

32. Which of the following is not a compiler optimization technique?


a. Loop invariant code motion
b. Dead code elimination
c. Debugging
d. Common subexpression elimination

33. Which of the following is an example of a lexical error?


a. Mismatched parentheses
b. Type mismatch in an expression
c. Undefined variable reference
d. Misspelled keyword

34. The process of rearranging the order of instructions to improve cache performance is known as:
a. Loop unrolling
b. Loop fusion
c. Loop interchange
d. Loop parallelization

35. Which of the following statements about compiler frontends is true?


a. Frontends generate machine code.
b. Frontends handle optimization.
c. Frontends perform lexical and syntax analysis.
d. Frontends manage memory allocation.

36. What is the main difference between a compiler and an interpreter?


a. A compiler translates the entire program before execution, while an interpreter translates and
executes the program line by line.
b. A compiler generates machine code, while an interpreter executes the source code directly.
c. A compiler is used for high-level languages, while an interpreter is used for low-level languages.
d. A compiler is slower than an interpreter.

37. Which of the following is not a factor that affects compiler performance?
a. Size of the input program
b. Speed of the processor
c. Complexity of the programming language
d. Memory availability

38. The process of transforming the abstract syntax tree into a lower-level representation is called:
a. Lexical analysis
b. Semantic analysis
c. Code generation
d. Code optimization

39. Which of the following is not a common method for error handling in compilers?
a. Printing an error message and terminating compilation
b. Recovering from errors and continuing compilation
c. Ignoring errors and producing an output anyway
d. Providing suggestions for fixing errors

40. What is the role of a lexer in a compiler?


a. To analyze the meaning of the program
b. To generate machine code
c. To break the program into tokens
d. To handle optimization techniques
Answers with explanations
1. What is the main purpose of a compiler?
Answer: c. To translate a program into machine code
Explanation: The main purpose of a compiler is to convert a program written in a high-level language into
machine code that can be executed by the computer's processor.

2. Which of the following is not a phase of the compiler?


Answer: d. Debugging
Explanation: Debugging is not a phase of the compiler. It is a separate process used to identify and fix
errors or issues in a program.

3. The intermediate representation used by a compiler is:


Answer: d. Abstract syntax tree
Explanation: The abstract syntax tree (AST) is a hierarchical representation of the program's syntactic
structure, often used as an intermediate representation during compilation.

4. The process of converting a high-level programming language into machine code is called:
Answer: a. Compilation
Explanation: Compilation is the process of translating a program written in a high-level language into
machine code, which can then be executed by the computer.

5. Which of the following is not a typical optimization performed by a compiler?


Answer: d. Syntax analysis
Explanation: Syntax analysis is a phase of the compiler that checks the program's syntax for correctness.
Optimization techniques focus on improving the efficiency or performance of the generated code.

6. What is the role of the optimizer in a compiler?


Answer: b. To improve the efficiency of the generated code
Explanation: The optimizer is responsible for analyzing the program and applying various techniques to
optimize the generated code, such as eliminating redundant operations or rearranging instructions for
better performance.

7. Which of the following is an example of a static analysis performed by a compiler?


Answer: a. Type checking
Explanation: Static analysis refers to the analysis of a program's properties without executing it. Type
checking is a common static analysis performed by a compiler to ensure that variables and expressions
have consistent types.

8. The process of allocating registers to variables is known as:


Answer: c. Code optimization
Explanation: Register allocation is a code optimization technique where the compiler assigns variables to
registers instead of memory locations to optimize access and improve program efficiency.

9. Which of the following is not a common programming language paradigm?


Answer: d. Compiler-oriented
Explanation: The term "compiler-oriented" is not commonly used to describe programming language
paradigms. The common paradigms include imperative, functional, object-oriented, and declarative.
10. Which phase of the compiler detects syntax errors in the input program?
Answer: c. Syntax analysis
Explanation: Syntax analysis, also known as parsing, is responsible for detecting and reporting syntax
errors in the input program by checking if it adheres to the grammar rules of the programming language.

11. What is the role of the symbol table in compiler design?


Answer: c. To store information about identifiers and their attributes
Explanation: The symbol table is a data structure used by the compiler to store information about
identifiers (variables, functions, etc.) encountered in the program, such as their names, types, scopes, and
memory locations.

12. Which of the following is not a compiler optimization technique?


Answer: c. Debugging
Explanation: Debugging is not an optimization technique but rather a process of identifying and fixing
errors or issues in a program. Compiler optimization techniques include loop optimization, constant
folding, and common subexpression elimination.

13. Which of the following is an example of a lexical error?


Answer: d. Misspelled keyword
Explanation: Lexical errors occur when the input program contains lexical (or tokenization) mistakes, such
as misspelled keywords, undefined symbols, or invalid characters.

14. The process of rearranging the order of instructions to improve cache performance is known as:
Answer: c. Loop interchange
Explanation: Loop interchange is an optimization technique where the order of nested loops is changed to
improve cache performance by optimizing memory access patterns.

15. Which of the following statements about compiler frontends is true?


Answer: c. Frontends perform lexical and syntax analysis.
Explanation: Compiler frontends are responsible for processing the input program, including lexical
analysis (tokenizing) and syntax analysis (parsing) to build the program's abstract syntax tree.

16. What is the main difference between a compiler and an interpreter?


Answer: a. A compiler translates the entire program before execution, while an interpreter translates and
executes the program line by line.
Explanation: A compiler translates the entire program into machine code before execution, while an
interpreter translates and executes the program's instructions line by line, typically without generating
intermediate machine code.

17. Which of the following is not a factor that affects compiler performance?
Answer: c. Complexity of the programming language
Explanation: The complexity of the programming language can indeed affect compiler performance.
Factors that can affect compiler performance include the size of the input program, speed of the
processor, and memory availability.

18. The process of transforming the abstract syntax tree into a lower-level representation is called:
Answer: c. Code generation
Explanation: Code generation is the process of transforming the intermediate representation (e.g.,
abstract syntax tree) into a lower-level representation, such as assembly language or machine code.
19. Which of the following is not a common method for error handling in compilers?
Answer: c. Ignoring errors and producing an output anyway
Explanation: Ignoring errors and producing an output anyway is not a common method for error handling
in compilers. Typically, compilers either terminate compilation upon encountering an error or try to
recover from errors and continue compilation.

20. What is the role of a lexer in a compiler?


Answer: c. To break the program into tokens
Explanation: The lexer, also known as the lexical analyzer, is responsible for breaking the input program
into tokens or lexemes, which are meaningful units in the programming language, such as keywords,
identifiers, operators, and literals.

You might also like