Consider the code segment int i, j, x, y, m, n; n=20; for (i = 0, i < n; i++)…
2017
Consider the code segment
int i, j, x, y, m, n;
n=20;
for (i = 0, i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i % 2)
{
x + = ((4*j) + 5*i);
y += (7 + 4*j);
}
}
}
m = x + y;Which one of the following is false
- A.
The code contains loop invariant computation
- B.
There is scope of common sub-expression elimination in this code
- C.
There is scope of strength reduction in this code
- D.
There is scope of dead code elimination in this code
Attempted by 62 students.
Show answer & explanation
Correct answer: D
Analysis of the code reveals several optimization opportunities. The expression 5*i is computed in the inner loop but depends only on the outer loop variable i, making it a loop invariant. The term 4*j is calculated twice within the inner loop body, allowing for common sub-expression elimination. Additionally, multiplying by 4 can be optimized via strength reduction (bit shifting). However, since x and y are both used to compute m at the end, no code is dead. Thus, any claim regarding dead code elimination in this context is incorrect.
A video solution is available for this question — log in and enroll to watch it.