Loop: GATE CS Previous Year Questions

Loop · Programming in C

17 questions · asked in 12 of 27 years (2000–2026)

MCQ 13 · MSQ 1 · NAT 31-mark 7 · 2-mark 10

2000Questions per year2026

Questions 6–10 of 17

Q6GATE 2017 (Session 2)2 marksMCQ−0.67 if wrong
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)?

Q7GATE 2016 (Session 1)2 marksMCQ−0.67 if wrong
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

Q8GATE 2016 (Session 2)2 marksMCQ−0.67 if wrong
The following function computes XYX^{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?

Q9GATE 2015 (Session 1)2 marksMCQ−0.67 if wrong
Consider the following pseudo code, where x and y are positive integers.
 begin
   q := 0
    r := x 
    while r >= y  do
         begin
               r := r - y
              q := q + 1
        end
end 
The post condition that needs to be satisfied after the program terminates is

Q10GATE 2014 (Session 1)2 marksMCQ−0.67 if wrong
Consider the following C function in which size is the number of elements in the array E:
 int MyX(int *E, unsigned int size)
{
  int Y = 0;
  int Z;
  int i, j, k; 
    for(i = 0; i < size; i++)
    Y = Y + E[i];
  for(i = 0; i < size; i++)
    for(j = i; j < size; j++)
    {
       Z = 0;
       for(k = i; k <= j; k++)
           Z= Z + E[k];
       if (Z > Y)
            Y = Z;
   }
return Y;
}
The value returned by the function MyX is the

Keep practising

Topics in Programming in C

All CS topics ›