Q. Consider the following C code segment. Which one of the following is false?…

2006

Q. Consider the following C code segment.
Which one of the following is false?

for (i = 0, i < n; i++)

{

for (j = 0; j < n; j++)

{

if (i % 2)

{

x += (4 j + 5 i);

y += (7 + 4 * j);

}

}

}

  1. A.

    The code contains loop invariant computation

  2. B.

    There is scope of common sub-expression elimination in this code

  3. C.

    There is scope of strength reduction in this code

  4. D.

    There is scope of dead code elimination in this code

Attempted by 33 students.

Show answer & explanation

Correct answer: D

The code contains nested loops where i and j iterate up to n. Inside the inner loop, 5*i is constant relative to j, making it a loop invariant eligible for code motion. Common subexpression elimination applies to repeated calculations like 4*j if optimized. However, dead code elimination does not apply here because x and y are actively updated with computed values. Therefore, the statement claiming dead code elimination is applicable is false.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir