recategorized by
1,572 views
2 votes
2 votes

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?

  1. The code contains loop invariant computation
  2. There is scope of common sub-expression elimination in this code
  3. There is scope of strength reduction in this code
  4. There is scope of dead code elimination in this code
recategorized by

2 Answers

Best answer
12 votes
12 votes
  • loop invariant: A fragment of code that resides in the loop and computes the same value in all iterations. In given loop if(i%2) computes same value within loop of j. This code can be moved out of the loop.
 for(i=0;i<n;i++) {
if(i%2)
   {
    for(j=0;j<n;j++)
    {
       ......

This movement is called code hoisting

A is true 

  • The sub expression  4*j is used at two places which can be eliminated.

B is true

  • Strength reduction: Operations that consume more time and space can be replaced by simple operations that produce the same result.

  4*j can be replaced by j <<2

C is true

  • There is no dead / Unreachable code.

D is false

Answer is D

selected by
Answer:

Related questions

2 votes
2 votes
1 answer
1
gatecse asked Dec 17, 2017
2,971 views
Consider the following table :$\begin{array}{|l|l|l|} \hline \textbf{A.} & \text{Activation record} & \textbf{p.} & \text{Linking loader} \\\hline \textbf{B.} & \text{Loc...
1 votes
1 votes
1 answer
2
gatecse asked Dec 17, 2017
1,260 views
Which languages necessarily need heap allocation in the runtime environment?Those that support recursionThose that use dynamic scoping Those that use global variablesThos...
9 votes
9 votes
2 answers
3
gatecse asked Dec 17, 2017
3,744 views
Consider the following $C$ function#include<stdio.h int main(void) { char c[]="ICRBCSIT17" char *p=c; printf("%s",c+2[p]-6[p]-1); return 0; }The output of the program is ...
5 votes
5 votes
2 answers
4
gatecse asked Dec 17, 2017
2,749 views
Which of the following related to snowflake schema is true?Each dimension is represented by a single dimensional tableMaintenance efforts are lessDimension tables are nor...