Machine-translated by Google. Questions are authored in English.
Top

Code Optimization Questions and Answers

81.

Consider the control flow graph given below.

Control flow graph: ENTRY to B1 (a = b + c); B1 to B2 (d = a + e) and to B3 (e = a + f); B3 loops back to B1; B2 to B4 (g = d + e); B4 to EXIT

Which one of the following options is the set of live variables at the exit point of each basic block?

Exam Year(s)

GATE CS - 2026,

Answer: A

The live variables at block exits are B1:{a, b, c, e, f}, B2:{d, e}, B3:{b, c, e, f}, B4:∅.

Liveness flows backwards: OUT(B) is the union of IN over B's successors, and IN(B) = USE(B) ∪ (OUT(B) - DEF(B)). The edges are B1 → B2, B1 → B3, B3 → B1 and B2 → B4.

  • B4 (g = d + e) leads to EXIT, and g is never used: OUT(B4) = ∅, IN(B4) = {d, e}.
  • B2 (d = a + e): OUT(B2) = IN(B4) = {d, e}; IN(B2) = {a, e}.
  • B1 (a = b + c) and B3 (e = a + f) form a loop. Iterating to a fixed point: IN(B1) = {b, c, e, f} and IN(B3) = {a, b, c, f}.
  • OUT(B1) = IN(B2) ∪ IN(B3) = {a, b, c, e, f}; OUT(B3) = IN(B1) = {b, c, e, f}.

The options with B1:∅ ignore that a is used in both successors of B1. The option with B3:{c, e, f} drops b, which is live because B1, reached from B3, uses it.

Please log in or register to report this question.

82.

Consider the control flow graph shown.

Control flow graph: ENTRY to B1 (i = m - 1, j = n, a = 10), B1 to B2 (i = i + 1, j = j - 1), B2 to B3 (a = 20) and to B4 (i = a + 1), B3 to B4, B4 back to B2 and to EXIT

Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?

Exam Year(s)

GATE CS - 2023,

Answer: D

The live sets at block exits are B1: {a, i, j}, B2: {a, j}, B3: {a, j}, B4: {a, i, j}.

A variable is live at a point if some path from there uses it before redefining it. Work backwards from the uses, assuming nothing is live at EXIT:

  • B2 uses i and j (i = i + 1, j = j - 1) before defining them, so both are live on entry to B2.
  • B4 uses a, then redefines i. Its successors are B2 and EXIT, so live-out of B4 = live-in of B2.
  • B3 defines a and uses nothing; B2 flows to B3 and B4.

Solving: live-in(B4) = {a} plus (live-out(B4) minus {i}) = {a, j}; live-in(B3) = {a, j} minus {a} = {j}; live-out(B2) = {j} plus {a, j} = {a, j}; live-in(B2) = {i, j} plus ({a, j} minus {i, j}) = {a, i, j}, which is live-out of both B1 and B4. B3's exit set is live-in(B4) = {a, j}.

The first two options drop i and j at B1's exit although B2 reads them at once. The third keeps i live after B2, but i is redefined in B4 before any use on that path.

Please log in or register to report this question.

83.

................. refers to the order in which individual statements or instructions of a program are executed.

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: B

The order in which statements are executed is the Control flow.

Control flow is the sequence in which a program's statements, instructions or function calls are executed, shaped by conditionals, loops, jumps and calls. Compilers represent it as a control flow graph.

Data flow concerns how values move between definitions and uses, register allocation assigns variables to registers, and instruction scheduling reorders machine instructions for the pipeline.

Please log in or register to report this question.

Answer: D

Loop unrolling aims at expanding the body of a loop to reduce overhead and improve parallelism.

Unrolling replicates the loop body several times and adjusts the loop counter, so the test, increment and branch are executed less often. The longer straight-line body also gives the scheduler more independent instructions to overlap.

It does not reduce the number of loops in the program, it does not remove conditionals inside the body, and it makes the loop larger, not smaller.

Please log in or register to report this question.

85.

In peephole optimization, constant folding involves :

Exam Year(s)

MH SET - 2025, SET Exam - 2025,

Answer: C

Constant folding means folding constant expressions during compilation.

When an expression's operands are all known at compile time, the compiler evaluates it once and replaces it with the result: x = 2 * 3 + 4 becomes x = 10. This saves the computation at run time.

Combining consecutive loads, eliminating dead code and unrolling loops are separate optimisations. Each removes or restructures code, but none computes constant expressions in advance.

Please log in or register to report this question.

86.

The graph that shows basic blocks and their successor relationship is called

Exam Year(s)

Tripura Public Service Commission - 2019,

Answer: B

The graph is called a flow graph.

In compiler design, a flow graph has the basic blocks of a program as its nodes, with an edge from one block to another whenever control can pass directly from the first to the second, its successor. It is the basis of data-flow analysis and loop optimisation.

A DAG represents the expressions computed within a single basic block, 'control graph' is not the standard term, and a Hamiltonian graph is a graph-theory idea unrelated to compilers.

Please log in or register to report this question.

87.

Peephole optimization is form of

Exam Year(s)

Tripura Public Service Commission - 2025,

Answer: B

Peephole optimization is a form of local optimization.

It examines a small sliding window of consecutive instructions, the 'peephole', and replaces patterns with shorter or faster equivalents: removing redundant loads and stores, unreachable code or jumps to jumps, and simplifying algebra. It looks only at a small local region.

Loop optimization works on whole loops, dataflow analysis on the whole procedure, and constant folding is one particular transformation that peephole optimization may perform, not the category it belongs to.

Please log in or register to report this question.

Answer: D

Reduction in strength means replacing a costly operation by a relatively cheaper one.

A typical example is replacing a multiplication by a loop index with an addition carried through the loop, or x * 2 with a shift or x + x. The result is the same but the operation is cheaper.

Replacing run-time computation by compile-time computation is constant folding, removing loop-invariant computation is code motion, and removing common subexpressions is a separate optimization.

Please log in or register to report this question.

Answer: D

The false statement is that it can be applied to the portion of the code that is not contiguous.

Peephole optimization works on a short window of consecutive instructions, so by definition it is applied to contiguous code.

The other statements are true: it works on a small part of the code; it can be applied to intermediate code as well as target code; and one improvement often exposes another, so it gives the best results when applied repeatedly.

Please log in or register to report this question.

Answer: B

Reducing the strength in a compiler means replacing an expensive operation with a cheaper machine instruction, for example replacing a multiplication by 2 with a shift or an addition, or a multiplication inside a loop by a repeated addition. It is therefore a code optimization using cheaper machine instructions.

It has nothing to do with reducing the range of the input values, and it is the opposite of reducing the efficiency of the program, since its purpose is to make the generated code faster. So the other options are wrong.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Code Optimization Questions and Answers || Code Optimization MCQ Questions and Answers || Code Optimization GK Questions and Answers || Code Optimization GK MCQ Questions || Code Optimization Multiple Choice Questions and Answers || Code Optimization GK || GK on Code Optimization || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design