Consider the following C code segment. for (i = 0, i<n; i++) { for (j=0; j<n;…
2006
Consider the following C code segment.
for (i = 0, i<n; i++)
{
for (j=0; j<n; j++)
{
if (i%2)
{
x += (4*j + 5*i);
y += (7 + 4*j);
}
}
}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 39 students.
Show answer & explanation
Correct answer: D
The code contains nested loops where the condition i%2 is evaluated repeatedly. Since i%2 depends only on i, which changes every outer loop iteration, it is not a loop invariant. However, the expressions 4*j and 7+4*j are computed repeatedly without being reused across iterations. The term (4*j + 5*i) and (7+4*j) are not dead code because they contribute to x and y. Dead code elimination applies only when statements have no effect, but here all computations affect variables x and y. Therefore, option D is false because there is no dead code.
A video solution is available for this question — log in and enroll to watch it.