Intermediate Code Generation # MCQs Practice set

Q.1 What is the primary purpose of intermediate code generation in a compiler?

To generate machine code directly
To provide a machine-independent representation
To simplify lexical analysis
To optimize hardware instructions
Explanation - Intermediate code acts as a bridge between the source language and target machine, making optimization and portability easier.
Correct answer is: To provide a machine-independent representation

Q.2 Which of the following is a common intermediate representation?

Parse trees
Three-address code
Symbol tables
Machine instructions
Explanation - Three-address code is a widely used intermediate representation because it is closer to machine instructions while still being abstract.
Correct answer is: Three-address code

Q.3 Why do compilers use intermediate code before machine code generation?

To reduce semantic errors
To improve portability and optimization
To avoid lexical analysis
To reduce runtime errors
Explanation - Intermediate code helps in creating a machine-independent phase and allows optimizations before mapping to specific machine code.
Correct answer is: To improve portability and optimization

Q.4 Which intermediate form uses postfix notation for expressions?

Syntax tree
Three-address code
Polish notation
Control flow graph
Explanation - Postfix (Reverse Polish) notation is often used as an intermediate form for evaluating arithmetic expressions efficiently.
Correct answer is: Polish notation

Q.5 In three-address code, each instruction typically has how many operands?

1
2
3
4
Explanation - Three-address code instructions involve at most three operands: two for the operation and one for the result.
Correct answer is: 3

Q.6 Which of the following is NOT an intermediate representation?

Syntax tree
Quadruple
Triples
Assembly code
Explanation - Assembly code is target-specific, while intermediate code is machine-independent.
Correct answer is: Assembly code

Q.7 In compiler design, DAGs (Directed Acyclic Graphs) are mainly used for?

Error detection
Optimization of expressions
Lexical analysis
Code linking
Explanation - DAGs help identify common sub-expressions and eliminate redundant computations.
Correct answer is: Optimization of expressions

Q.8 Which structure represents intermediate code in quadruple format?

Operator, Argument1, Argument2, Result
Operator, Result, Operand
Operand1, Operand2, Result
Operator, Operand
Explanation - Quadruples explicitly represent each operation using operator, arguments, and result fields.
Correct answer is: Operator, Argument1, Argument2, Result

Q.9 Triples differ from quadruples in that:

They omit the operator
They omit the result field
They use fewer operands
They cannot represent expressions
Explanation - Triples eliminate the explicit result field, referring instead to the result by the position of the instruction.
Correct answer is: They omit the result field

Q.10 Indirect triples solve which problem of triples?

Complexity of operators
Difficulty in code modification
Operand redundancy
Loss of syntax tree
Explanation - Indirect triples introduce a table of pointers, making it easier to rearrange and modify code.
Correct answer is: Difficulty in code modification

Q.11 Which intermediate code format is easiest for optimization?

Triples
Quadruples
Indirect triples
Syntax tree
Explanation - Quadruples explicitly store results and operands, making them easier to manipulate during optimization.
Correct answer is: Quadruples

Q.12 The statement a = b + c * d would require how many instructions in three-address code?

1
2
3
4
Explanation - First compute c*d into a temporary variable, then add b to the result.
Correct answer is: 2

Q.13 Which of the following describes machine-independent intermediate code?

It runs only on one architecture
It avoids hardware-specific features
It is executable directly
It uses assembly syntax
Explanation - Machine-independent code avoids hardware constraints, making optimization and portability possible.
Correct answer is: It avoids hardware-specific features

Q.14 What does the compiler use to detect common subexpressions?

Symbol table
DAG representation
Parse tree
Code generator
Explanation - DAGs capture repeated subexpressions and eliminate redundancy in intermediate code.
Correct answer is: DAG representation

Q.15 Intermediate code lies between which two phases of the compiler?

Lexical analysis and syntax analysis
Semantic analysis and code generation
Parsing and optimization
Code generation and linking
Explanation - Intermediate code is generated after semantic analysis and before final machine code generation.
Correct answer is: Semantic analysis and code generation

Q.16 Why is intermediate code important for retargetable compilers?

It reduces parsing complexity
It allows the same front end to work with multiple back ends
It improves lexical analysis
It reduces runtime memory
Explanation - By using intermediate code, compilers can support different target architectures with the same front end.
Correct answer is: It allows the same front end to work with multiple back ends

Q.17 Which of the following is a three-address code format?

x = y
x = y + z
x = y + z * w
x = (y)
Explanation - Three-address code generally represents an operation with at most three operands, like assignment with an operator.
Correct answer is: x = y + z

Q.18 Which intermediate representation is closest to machine code?

Syntax tree
Three-address code
Assembly
High-level code
Explanation - Three-address code resembles machine instructions in structure while still being abstract and machine-independent.
Correct answer is: Three-address code

Q.19 In a DAG, what do leaf nodes represent?

Operators
Intermediate results
Operands or identifiers
Control statements
Explanation - Leaf nodes in DAGs denote operands like constants or variable identifiers.
Correct answer is: Operands or identifiers

Q.20 Which of the following is an advantage of triples over quadruples?

Simpler optimization
Reduced memory usage
Easier modification
Supports control flow
Explanation - Since triples omit the result field, they use less memory compared to quadruples.
Correct answer is: Reduced memory usage

Q.21 Intermediate code should ideally be?

Platform dependent
Easily optimizable
Executable directly
Hardware specific
Explanation - Intermediate code is designed to facilitate machine-independent optimization.
Correct answer is: Easily optimizable

Q.22 Which of the following is used to represent boolean expressions in intermediate code?

Triples
Backpatching
DAGs
Symbol tables
Explanation - Backpatching is used for generating intermediate code for boolean expressions and control flow.
Correct answer is: Backpatching

Q.23 In three-address code, temporary variables are used to?

Reduce parsing time
Hold intermediate results
Reduce optimization steps
Simplify symbol tables
Explanation - Temporary variables store partial results of expressions for later use.
Correct answer is: Hold intermediate results

Q.24 The primary role of syntax-directed translation in intermediate code generation is?

Lexical analysis
Error reporting
Mapping parse trees to code
Register allocation
Explanation - Syntax-directed translation uses grammar rules to generate intermediate code from parse trees.
Correct answer is: Mapping parse trees to code

Q.25 Which of the following is a disadvantage of triples?

Code optimization complexity
Memory overhead
Machine dependence
Lack of temporary variables
Explanation - Since triples reference results by position, rearranging code for optimization is more complex.
Correct answer is: Code optimization complexity