Intermediate Code Generation MCQ Quiz - Objective Question with Answer for Intermediate Code Generation - Download Free PDF
Last updated on Apr 8, 2025
Latest Intermediate Code Generation MCQ Objective Questions
Intermediate Code Generation Question 1:
One of the purposes of using intermediate code in compilers is to :
Answer (Detailed Solution Below)
Intermediate Code Generation Question 1 Detailed Solution
The correct answer is make parsing and semantic analysis simpler.
Key Points
- Intermediate code is used in compilers as a bridge between the source code and machine code.
- It simplifies parsing and semantic analysis by providing an abstract representation of the source code.
- This abstraction helps in isolating the machine-dependent and machine-independent parts of the compilation process.
- By using intermediate code, the compiler can perform optimizations more effectively and can target multiple machine architectures.
- Examples of intermediate code forms include Three-Address Code (TAC), Static Single Assignment (SSA), and others.
Additional Information
- Intermediate code can be more easily manipulated by different phases of the compiler than the original source code.
- It provides a uniform structure that can be used to implement various optimizations and transformations.
- Using intermediate code helps in separating the concerns of front-end and back-end compiler development.
- It can be reused across different compilers, which improves development efficiency and maintainability.
Intermediate Code Generation Question 2:
Regarding the code optimization, choose the correct sequence
A. Algebraic Simplification
B. Use of machine idioms
C. Redundant - instruction elimination
D. Flow of control optimization
E. Improved target code
Choose the correct answer from the options given below:
Answer (Detailed Solution Below)
Intermediate Code Generation Question 2 Detailed Solution
The correct answer is Option 3: C, D, A, B, E.
Key Points
- Code optimization is an important aspect of improving the efficiency and performance of code. The sequence provided in the correct answer represents a logical flow for optimizing code.
- Redundant-instruction elimination (C) should be performed first. This step removes any unnecessary instructions, reducing the code size and improving its efficiency.
- Flow of control optimization (D) follows. This step focuses on improving the control flow within the code, such as optimizing loops and conditional branches to make the code run faster.
- Algebraic simplification (A) is the next step. This involves simplifying algebraic expressions within the code to make them more efficient and easier to compute.
- Use of machine idioms (B) comes after. This step involves using specific machine-level instructions that are more efficient than their higher-level counterparts, which can improve the performance of the code on a given architecture.
- Finally, improved target code (E) is generated. This involves producing the final optimized machine code that is more efficient and runs faster on the target machine.
Additional Information
- Code optimization is typically performed by compilers, which can automate many of these steps to produce efficient machine code from higher-level programming languages.
- The goals of code optimization include reducing the execution time, minimizing memory usage, and improving the overall performance of the software application.
- Advanced optimization techniques may also consider factors such as power consumption and code maintainability.
- Different optimization techniques may be more or less effective depending on the specific context and the nature of the code being optimized.
Intermediate Code Generation Question 3:
Consider the following translation scheme :
S→ER
R→*E{print(‘‘*’’);}R|\(\varepsilon\)
E→F+E{print(“+”);}|F
F→(S)|id{print(id.value);}
Here id is a token that represents an integer and id.value represents the corresponding integer value. For an input ‘2*3+4’, this translation scheme printsAnswer (Detailed Solution Below)
Intermediate Code Generation Question 3 Detailed Solution
The correct answer is 2 3 4 + *
Explanation:
To solve the problem, you need to understand L-Attributed Syntax Directed Translation and how to construct a parse tree. In this context, semantic actions (such as printf statements) are embedded in the right-hand side (RHS) of productions (for example, R → *E{print(“*”);}R). After building the parse tree according to the provided grammar, the final output is generated by performing a depth-first traversal, evaluating each node from left to right.
Parse Tree:
Follow the arrows in the picture (which represent a Depth-First, Left-to-Right evaluation). Whenever you exit from any child that has a printf statement in this question, print the corresponding symbol, which can be an integer value, '*', or '+'.
Print Sequence
Following the steps above, the sequence of prints is: 2 3 4 + *
Intermediate Code Generation Question 4:
Which of the following statements are TRUE about intermediate code generation in a compiler?
(A) Intermediate code is platform-independent.
(B) Intermediate code can be optimized before being translated into machine code.
(C) Intermediate code is generated only for high-level programming languages.
(D) The purpose of intermediate code is to simplify the process of code generation for different target architectures.
Choose the correct answer from the options given below:
Answer (Detailed Solution Below)
Intermediate Code Generation Question 4 Detailed Solution
The correct answer is (A), (B), (D) Only
Key Points
To evaluate the statements about intermediate code generation in a compiler, let's analyze each one:
- (A) Intermediate code is platform-independent.
- True. Intermediate code is designed to be platform-independent, allowing for portability across different hardware architectures.
- (B) Intermediate code can be optimized before being translated into machine code.
- True. Optimizations can be applied to intermediate code to improve performance before the final machine code generation.
- (C) Intermediate code is generated only for high-level programming languages.
- False. Intermediate code can also be generated for lower-level languages, depending on the compiler design. It is not limited to high-level languages.
- (D) The purpose of intermediate code is to simplify the process of code generation for different target architectures.
- True. Intermediate code serves as an abstraction that simplifies the final code generation process, making it easier to target multiple architectures.
Based on this analysis, the true statements are (A), (B), and (D).
Therefore, the correct answer is: 1) (A), (B), (D) Only.
Intermediate Code Generation Question 5:
The postfix form of A*B + C/D is
Answer (Detailed Solution Below)
Intermediate Code Generation Question 5 Detailed Solution
The correct answer is AB*CD/+
Key PointsThe postfix (also known as Reverse Polish Notation - RPN) is a way of writing expressions without the need for parenthesis. It places operators after their operands. To convert the infix expression (A*B + C/D) to postfix, we follow the operands and operators in their order of operation, considering the precedence of operators.
Given infix expression: (A*B + C/D)
- Multiply A and B ((A*B))
- Divide C by D ((C/D))
- Add the results of steps 1 and 2
Following these steps in postfix notation:
- Multiplication of A and B is represented as (AB*)
- Division of C by D is represented as (CD/)
- Addition of the results of the above operations is represented by appending a plus (+) symbol after them.
Therefore, putting it all together, the postfix expression becomes: (AB*CD/+)
So, the correct option is: AB*CD/+
Top Intermediate Code Generation MCQ Objective Questions
Match the following according to input (from the left column) to the compiler phase (in the right column) that processes it:
(P) |
Syntax tree |
(i) |
Code generator |
(Q) |
Character steam |
(ii) |
Syntax analyzer |
(R) |
Intermediate representation |
(iii) |
Semantic analyser |
(S) |
Token stream |
(iv) |
Lexical analyzer |
Answer (Detailed Solution Below)
Intermediate Code Generation Question 6 Detailed Solution
Download Solution PDFPhases of the compiler are:
Diagram
Phases of Compiler |
Input |
Output |
Lexical Analyzer |
Character stream |
Token stream |
Syntax Analyzer |
Token stream |
Syntax tree |
Semantic Analyzer |
Syntax tree |
Syntax tree |
Intermediate Code Generator |
Syntax tree |
Intermediate representation |
Machine-Independent Code Optimizer |
Intermediate representation |
Intermediate representation |
Code Generator |
Intermediate representation |
Target-machine code |
Machine-Dependent Code Optimizer |
Target-machine code |
Target-machine code |
Resolution of externally defined symbols is performed by____
Answer (Detailed Solution Below)
Intermediate Code Generation Question 7 Detailed Solution
Download Solution PDFLinker:
- A Linker is a computer program that takes object files which is generated by compiler and combines all these object files into a single executable file or another object file, so resolution of externally defined symbols is performed by linker.
Loader:
- A Loader is a major component of an OS that make sure all necessary programs and files should be loaded when staring phase of a running program.
Therefore, Linker is the correct answer.
Which of the following is not an intermediate code form ?
Answer (Detailed Solution Below)
Intermediate Code Generation Question 8 Detailed Solution
Download Solution PDFThe correct answer is option 3.
Key Points
- Intermediate code can be represented in three forms, which are postfix notation, Syntax trees, Three address code.
- In a compiler, three address code can be implemented as records with fields for operator and operands. There are three such representations, Quadruples, Triples, Indirect triples.
∴ Hence the correct answer is Quadruples.
The least number of temporary variables required to create a three-address code in static single assignment form for the expression q + r / 3 + s – t * 5 + u * v / w is _______________.
Answer (Detailed Solution Below) 8
Intermediate Code Generation Question 9 Detailed Solution
Download Solution PDFConcept:
Three address code is an intermediate code generated by compilers while optimizing the code.
Rules in static single assignment form:
- Each variable must be assigned exactly once.
- Every variable must be defined before it is used.
- All of the uses reached by the assignment must be renamed.
Explanation:
Given expression: q + r / 3 + s – t * 5 + u * v / w
So, t1 = r/3
t2 = t * 5
t3 = u * v
t4 = t3 / w
t5 = q + t1
t6 = t5 + s
t7 = t6 – t2
t8 = t7 – t4
Total 8 temporary variables are required to create a three-address code in static single assignment form.Consider the following intermediate program in three address code:
p = a – b
q = p * c
p = u * v
q = p + q
Which one of the following corresponds to a static single assignment form of the above code?Answer (Detailed Solution Below)
p3 = a – b
q4 = p3 * c
p4 = u * v
q5 = p4 + q4
Intermediate Code Generation Question 10 Detailed Solution
Download Solution PDFConcept:
Rules in static single assignment form:
- Each variable must be assigned exactly once.
- Every variable must be defined before it is used.
- All of the uses reached by the assignment must be renamed.
Explanation
Now consider, all the option one by one
Option 1:
It violated the rule 1 of static single assignment form. Because in this, p1 is assigned (initialized) two times: one in p1 = a – b and another in p1 = u * v
Option 3:
It violates the rule 2. Because in this case p2, p4 and q3 are not assigned any values. They are being used here without initializing.
Option 4:
It is invalid, because in last line p + q is directly added without moving into the register.
Option 2:
Follows all the rules of static single assignment form. So, this is the answer.
Translation from symbolic program into Binary is done in _______.
Answer (Detailed Solution Below)
Intermediate Code Generation Question 11 Detailed Solution
Download Solution PDFCorrect answer: Two passes
Explanation:
Symbolic program is a program where the developers can use their own expressions, rules and other components of the program. The symbolic program is written in a high-level language which cannot be understood by the machine. As a result, the program needs to be converted into a low-level language program and this is done with the help of a compiler.
A compiler cannot directly translate a symbolic program to binary because it needs to perform a syntactical analysis.
A two pass processor flows through the symbolic program two times. During the first pass, the program is checked for syntactical correctness. A table is made of all the symbols used in the program. For the second pass, the high-level language program is converted to machine language program, which is in binary.
In a three pass processor, the first pass identifies the various symbols that are used. In the second pass, the syntax is analysed to pick out any errors in the program. During the third pass, the compiler checks if the program is in accordance with the rules of the language. The four pass compiler is similar to the three pass compiler.
Consider the following C code segment:
a = b + c;
e = a + 1;
d = b + c;
f = d + 1;
g = e + f;
In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes of nodes in the DAG is ______
Answer (Detailed Solution Below) 6
Intermediate Code Generation Question 12 Detailed Solution
Download Solution PDFAnswer:6
Explanation:
we can write g = (b+c) +1 + (b+c)+1
its syntax tree will be
and its DAG will be created by combining all redundant terms.
Hence It contains 6 nodes.
What is the cyclomatic complexity of flowgraph F?
Answer (Detailed Solution Below)
Intermediate Code Generation Question 13 Detailed Solution
Download Solution PDFConcept
Cyclomatic complexity of a flow graph is the quantitative measure of the number of linearly independent paths in it.
For calculation of cyclomatic complexity, it is necessary to understand some parameters.
- Region - In our case, there are 4 regions.
- Edges - In our case, there are 11 edges.
- Nodes - In our case, there are 9 nodes.
- Connected Components - In our case, there is 1 connected component.
- Predicates – These are nodes that contain condition. In our case, there are 3 predicates namely node 1, 2.3, 6.
Formula
There are three ways of calculating the cyclomatic complexity M,
E - Number of Edges
N - Number of Nodes
P - number of predicate nodes (1, (2,3), 6)
- M = E – N + 2
M = 9 - 11 + 2
M = 9 - 13
M = 4
Or
M = p + 1
M = 3 + 1 = 4
A variable x is said to be live at a statement Si in a program if the following three conditions hold simultaneously:
i. There exists a statement Sj that uses x
ii. There is a path from Si to Sj in the flow graph corresponding to the program
iii. The path has no intervening assignment to x including at Si and Sj
The variables which are live both at the statement in basic block 2 and at the statement in basic block 3 of the above control flow graph areAnswer (Detailed Solution Below)
Intermediate Code Generation Question 14 Detailed Solution
Download Solution PDFConcept:
A variable is considered live if it is used in the block and not overwritten by any other variable.
Rule of liveliness: If a block does not use a variable which is live after its exit point, then this variable is live also before the entry point of that block.
Explanation:
Here, variable {q and r} are live in line 1 of block 1. But p is dead because p is overwritten in the first line and then used by s. Variable s is dead at the entry point of block 1. U is not read in block 1 so it is dead before block 1.
Before Block 2, variable r and u are being read so they are live at entry point of Block 2. Similarly, in Block 3, variable s and u are live at entry point of block 3.
In Block 4, variable v and r is live and by rule of liveliness variable r and v are live before Block 3 also.
Variables which live in Block 2: r and u
Variables which are live in Block 3: r, s, u and v
But we have to find which are live in both blocks. So, answer is r and u.Consider the intermediate code given below.
1) i = 1
2) j = 1
3) t1 = 5 ∗ i
4) t2 = t1 + j
5) t3 = 4 ∗ t2
6) t4 = t3
7) a[t4] = - 1
8) j = j + 1
9) if j < = 5 goto (3)
10) i = i + 1
11) if i < 5 goto (2)
The number of nodes and edges in the control - flow - graph constructed for the above code, respectively, are
Answer (Detailed Solution Below)
Intermediate Code Generation Question 15 Detailed Solution
Download Solution PDFConcept:
Control flow graph (CFG) is the graphical representation of computation during the execution of programs or applications.
Control flow graph for the above intermediate code is given here as:
Control flow graph contains 6 vertices and 7 edges.