Loop


Question 1
Consider the following C code. Assume that unsigned long int type length is 64 bits.

unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}  
The value returned when we call fun with the input 2^{40} is
A
4
B
5
C
6
D
40
GATE CSE 2018   C Programming
Question 2
Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x, y, q and r are all unsigned int.
while (r >= y) {
r = r - y;
q = q +1;
}
Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x==(y*q+r)?
A
(q==r) && (r==0)
B
(x \gt 0) && (r==x) && (y \gt 0)
C
(q==0) && (r==x) && (y \gt 0)
D
(q==0) && (y \gt 0)
GATE CSE 2017 SET-2   C Programming


Question 3
The following function computes X^{Y} for positive integers X and Y.

int exp(int X,int Y){
    int res=1, a=X, b=Y;
    while (b!=0){
    if(b%2==0){a=a*a;  b=b/2;}
    else {res=res*a;  b=b-1;}
   }
   return res;
}
Which one of the following conditions is TRUE before every iteration of the loop?
A
X^{Y}=a^{b}
B
(res*a)^{Y}=(res*X)^{b}
C
X^{Y}=res*a^{b}
D
X^{Y}=(res*a)^{b}
GATE CSE 2016 SET-2   C Programming
Question 4
The following function computes the maximum value contained in an integer array p[] of size n (n>=1).
 
int max(int*p, int n){
   int a=0,b=n-1;
   while (__________){
   if (p[a]<=p[b]){a=a+1;} 
   else {b=b-1;}
   }
   return p[a];
}
The missing loop condition is
A
a !=n
B
b !=0
C
b \gt (a+1)
D
b !=a
GATE CSE 2016 SET-1   C Programming
Question 5
Consider the following pseudo code, where x and y are positive integers.
 begin
   q := 0
    r := x 
    while r \geq  y  do
         begin
               r := r - y
              q := q + 1
        end
end 
The post condition that needs to be satisfied after the program terminates is
A
{r=qx+y \; \wedge \; r \lt y}
B
{x=qy+r \; \wedge \; r \lt y}
C
{y=qx+r \; \wedge \; 0 \lt r \lt y}
D
{ q+1 \lt r-y \; \wedge \; y \gt 0}
GATE CSE 2015 SET-1   C Programming


There are 5 questions to complete.

6 thoughts on “Loop”

    • Answer is option A

      C language allows only certain words in it- these are called tokens. If we input any invalid tokens it causes lexical error.

      eg:
      44a44
      causes lexical error as in C as an alphabet cannot come in between digits.

      Syntactic error is caused by bad combination of tokens. For example, we cannot have a constant on the left hand side of an assignment statement, a for loop must have two expressions inside () separated by semi colon etc.

      In the given question, line 3 won’t cause a lexical error or syntactic error. The statement will be treated as a function call with three arguments. Function definition being absent will cause link time error, but the question asks only for compile-time errors. So, (a) must be the answer.

      Reply
  1. question number 5, options are:-
    A. {r=qx+y∧r<y}
    B. {x=qy+r∧r<y}
    C. {y=qx+r∧0<r<y}
    D. {q+10}

    please update it. 😉

    Reply

Leave a Comment