What is a compiler?
a) A software that interprets code line-by-line
b) A tool that translates high-level language code into machine code
c) A hardware component for processing instructions
d) A program that executes code directly
Answer: b) A tool that translates high-level language code into machine code
Which phase of a compiler checks for syntax errors?
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Optimization
Answer: b) Syntax Analysis
What does the lexical analysis phase of a compiler do?
a) Translates code into machine code
b) Checks for syntactic correctness
c) Breaks down code into tokens
d) Optimizes code for performance
Answer: c) Breaks down code into tokens
In which phase of compilation is intermediate code generated?
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Code Generation
Answer: d) Code Generation
Which data structure is typically used to represent syntax trees?
a) Stack
b) Queue
c) Tree
d) Graph
Answer: c) Tree
What is the purpose of semantic analysis in a compiler?
a) To check if the syntax of the code is correct
b) To convert high-level code into machine code
c) To ensure the code adheres to the language’s semantics
d) To optimize the code for performance
Answer: c) To ensure the code adheres to the language’s semantics
Which of the following is NOT a phase of compilation?
a) Preprocessing
b) Parsing
c) Debugging
d) Code Optimization
Answer: c) Debugging
What is a parse tree?
a) A tree that represents the token stream of the code
b) A tree that represents the syntactic structure of the source code
c) A tree used for code optimization
d) A tree that represents the execution flow of the program
Answer: b) A tree that represents the syntactic structure of the source code
What is the role of the symbol table in a compiler?
a) To store intermediate code
b) To keep track of variable names and their attributes
c) To optimize the compiled code
d) To generate machine code
Answer: b) To keep track of variable names and their attributes
Which type of grammar is commonly used to define the syntax of programming languages?
a) Regular Grammar
b) Context-Free Grammar
c) Context-Sensitive Grammar
d) Unrestricted Grammar
Answer: b) Context-Free Grammar
What is a lexical analyzer (scanner)?
a) A program that generates machine code
b) A program that checks for syntax errors
c) A program that converts a sequence of characters into tokens
d) A program that optimizes code
Answer: c) A program that converts a sequence of characters into tokens
What is the purpose of the Code Optimization phase in a compiler?
a) To generate intermediate code
b) To improve the performance of the generated code
c) To check for syntax errors
d) To convert source code into assembly code
Answer: b) To improve the performance of the generated code
Which of the following is a type of intermediate code representation?
a) Abstract Syntax Tree (AST)
b) Bytecode
c) Assembly Language
d) Source Code
Answer: b) Bytecode
What is the purpose of the semantic analysis phase?
a) To generate the final executable code
b) To ensure that the code complies with the language’s rules and semantics
c) To parse the source code into tokens
d) To optimize the code for performance
Answer: b) To ensure that the code complies with the language’s rules and semantics
Which compiler phase involves translating code into assembly language?
a) Lexical Analysis
b) Syntax Analysis
c) Code Generation
d) Code Optimization
Answer: c) Code Generation
What is a token in the context of lexical analysis?
a) A unit of syntax
b) A unit of meaning in the code
c) A sequence of characters that represent a keyword or identifier
d) A structure in the parse tree
Answer: c) A sequence of characters that represent a keyword or identifier
Which data structure is commonly used to implement a parser?
a) Stack
b) Queue
c) Tree
d) List
Answer: a) Stack
What does “code generation” refer to in the compilation process?
a) Converting source code into an intermediate representation
b) Generating assembly or machine code from intermediate code
c) Checking the syntax of the source code
d) Optimizing the intermediate code
Answer: b) Generating assembly or machine code from intermediate code
Which phase of a compiler is responsible for translating a program’s control flow into a form that can be executed by a CPU?
a) Lexical Analysis
b) Syntax Analysis
c) Code Optimization
d) Code Generation
Answer: d) Code Generation
What is the primary goal of code optimization?
a) To correct syntax errors
b) To make the code more readable
c) To improve the performance of the code
d) To convert code to a different programming language
Answer: c) To improve the performance of the code
What is a grammar in the context of programming languages?
a) A set of rules for syntax and semantics
b) A type of compiler optimization
c) A representation of the code in machine language
d) A tool for debugging code
Answer: a) A set of rules for syntax and semantics
What is the main function of syntax-directed translation?
a) To translate code into machine language
b) To translate tokens into a syntax tree
c) To use syntax rules to guide the translation of source code
d) To generate intermediate code
Answer: c) To use syntax rules to guide the translation of source code
What does “tokenization” refer to in the context of lexical analysis?
a) The process of generating tokens from intermediate code
b) The process of converting a sequence of characters into meaningful tokens
c) The process of translating tokens into machine code
d) The process of analyzing the syntax of tokens
Answer: b) The process of converting a sequence of characters into meaningful tokens
What is a “parse tree” used for in a compiler?
a) To represent the execution flow of a program
b) To represent the lexical structure of the source code
c) To represent the syntactic structure of the source code
d) To represent the memory layout of variables
Answer: c) To represent the syntactic structure of the source code
Which phase of a compiler is typically the first to process the source code?
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Code Optimization
Answer: a) Lexical Analysis
What is an “abstract syntax tree” (AST)?
a) A representation of the code’s execution flow
b) A hierarchical representation of the syntactic structure of the code
c) A representation of the code in assembly language
d) A linear representation of the source code
Answer: b) A hierarchical representation of the syntactic structure of the code
Which of the following is an example of a high-level programming language?
a) Assembly Language
b) Machine Code
c) C++
d) Bytecode
Answer: c) C++
What is “semantic checking” in the context of compilation?
a) Ensuring that the syntax of the code is correct
b) Verifying that the code adheres to the language’s semantic rules
c) Optimizing the generated machine code
d) Generating intermediate code
Answer: b) Verifying that the code adheres to the language’s semantic rules
Which of the following phases is responsible for optimizing the performance of the compiled code?
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Code Optimization
Answer: d) Code Optimization
What is a “token” in the context of lexical analysis?
a) A symbolic representation of a data type
b) A unit of syntax that is recognized by the lexer
c) A part of the machine code
d) A semantic error in the code
Answer: b) A unit of syntax that is recognized by the lexer
Which phase of compilation involves creating a symbol table?
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Code Generation
Answer: c) Semantic Analysis
What is the purpose of a “control flow graph” in a compiler?
a) To represent the logical flow of control in the source code
b) To represent the syntactic structure of the code
c) To track variable usage in the code
d) To optimize the performance of the generated machine code
Answer: a) To represent the logical flow of control in the source code
What is a “machine language” in the context of compilation?
a) A low-level programming language that is directly executed by the CPU
b) A high-level programming language that needs to be compiled
c) An intermediate code used for optimization
d) A language used for debugging
Answer: a) A low-level programming language that is directly executed by the CPU
What is a “lexer” in the context of lexical analysis?
a) A tool that generates machine code
b) A tool that parses the syntax of the code
c) A tool that performs lexical analysis and generates tokens
d) A tool that optimizes the code
Answer: c) A tool that performs lexical analysis and generates tokens
Which compiler phase involves checking the correctness of variable types and operations?
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Code Generation
Answer: c) Semantic Analysis
What is a “register allocation” in the context of code generation?
a) A process of allocating memory for variables
b) A process of assigning variables to processor registers
c) A process of optimizing loop structures
d) A process of converting source code into intermediate code
Answer: b) A process of assigning variables to processor registers
What is an “optimization pass” in the context of code optimization?
a) A phase that generates intermediate code
b) A phase that refines and improves code performance
c) A phase that converts source code into assembly language
d) A phase that checks for syntax errors
Answer: b) A phase that refines and improves code performance
What is “inline expansion” in compiler optimization?
a) Replacing a function call with the function’s body
b) Generating additional intermediate code for optimization
c) Replacing a variable with its computed value
d) Removing unused functions from the code
Answer: a) Replacing a function call with the function’s body
What is “dead code elimination” in the context of optimization?
a) Removing code that will never be executed
b) Replacing old code with new code
c) Generating additional code for optimization
d) Removing redundant variables
Answer: a) Removing code that will never be executed
Which of the following is an example of a high-level language construct?
a) Assembly Instructions
b) Machine Code Instructions
c) Loops and Conditional Statements
d) Register Allocation
Answer: c) Loops and Conditional Statements
What does the term “syntax-directed translation” refer to?
a) Using syntax rules to guide the generation of code
b) Translating machine code into source code
c) Directly translating tokens into machine code
d) Analyzing the semantics of the code
Answer: a) Using syntax rules to guide the generation of code
What is the purpose of a “back end” in a compiler?
a) To analyze the syntax and semantics of the source code
b) To generate and optimize the machine code
c) To convert tokens into a syntax tree
d) To perform lexical analysis
Answer: b) To generate and optimize the machine code
What is “syntax error reporting” in the context of compilation?
a) Generating warnings about potential issues in the code
b) Displaying errors related to incorrect syntax in the code
c) Generating machine code from intermediate code
d) Checking for semantic errors
Answer: b) Displaying errors related to incorrect syntax in the code
What is a “preprocessor” in the context of compilation?
a) A tool that generates machine code
b) A tool that performs lexical analysis
c) A tool that processes source code before actual compilation
d) A tool that optimizes code
Answer: c) A tool that processes source code before actual compilation
What is the purpose of “error recovery” in a compiler?
a) To handle and recover from errors encountered during compilation
b) To remove redundant code
c) To optimize the performance of the code
d) To generate the final machine code
Answer: a) To handle and recover from errors encountered during compilation
What is “code generation” in a compiler’s context?
a) The process of translating high-level code into an intermediate representation
b) The process of generating executable machine code from intermediate code
c) The process of parsing the source code
d) The process of analyzing code for semantic errors
Answer: b) The process of generating executable machine code from intermediate code
What does “type checking” refer to in semantic analysis?
a) Ensuring that variables are correctly assigned to memory locations
b) Ensuring that operations are performed on compatible data types
c) Verifying the correctness of the syntax of the code
d) Optimizing the performance of the generated machine code
Answer: b) Ensuring that operations are performed on compatible data types
What is a “symbol table” used for in a compiler?
a) Storing intermediate code
b) Storing information about variables, functions, and objects
c) Generating machine code
d) Optimizing the code
Answer: b) Storing information about variables, functions, and objects
Which of the following best describes “source code”?
a) The machine code generated by the compiler
b) The high-level code written by the programmer
c) The intermediate code used for optimization
d) The code generated during lexical analysis
Answer: b) The high-level code written by the programmer
Which of the following is a key goal of compiler design?
a) To ensure the source code is error-free
b) To convert high-level code into efficient machine code
c) To debug the source code
d) To maintain the source code in a readable format
Answer: b) To convert high-level code into efficient machine code