# 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 1 Explanation:
 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 2 Explanation:
 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 3 Explanation:
 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 4 Explanation:
 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
Question 5 Explanation:
 Question 6
Consider the following pseudo code. What is the total number of multiplications to be performed?
 D= 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3 
 A Half of the product of the 3 consecutive integers B One-third of the product of the 3 consecutive integers. C One-sixth of the product of the 3 consecutive integers D None of the above.
GATE CSE 2014 SET-1   C Programming
Question 6 Explanation:
 Question 7
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
 A maximum possible sum of elements in any sub-array of array E. B maximum element in any sub-array of array E. C sum of the maximum elements in all possible sub-arrays of array E. D the sum of all the elements in the array E.
GATE CSE 2014 SET-1   C Programming
Question 7 Explanation:
 Question 8
Consider the following segment of C-code:
 int j, n;
j = 1;
while (j <=n)
j=j*2; 
The number of comparisons made in the execution of the loop for any $n \gt 0$ is:
 A $\left \lceil log_{2}n \right \rceil +1$ B n C $\left \lceil log_{2}n \right \rceil$ D $\left \lfloor log_{2}n \right \rfloor +1$
GATE CSE 2007   C Programming
Question 8 Explanation:
 Question 9
Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n+m] be another integer array.

void xyz (int a[],int b[],int c[]){
int i, j, k;
i=j=k=0;
while((i < n))&&(j < m)
if (a[i] < b[j]c[k++]=a[i++];
else c[k++]=b[j++];
}
Which of the following condition(s) hold(s) after the termination of the while loop ?

I. j$\lt$m, k=n+j-1, and a [n-1]$\lt$b[j] if i=n
II. i$\lt$n, k=m+i-1, and b[m-1]$\leq$a[i] if j=m
 A only (I) B only (II) C either (I) or (II) but not both D neither (I) nor (II)
GATE CSE 2006   C Programming
Question 9 Explanation:
 Question 10
Consider line number 3 of the following C-program.

int min ( ) { /* Line 1 */
int I, N; /* Line 2 */
fro (I =0, I < N, I++); /* Line 3 */
}
 A No compilation error B Only a lexical error C Only syntactic errors D Both lexical and syntactic errors
GATE CSE 2005   C Programming
Question 10 Explanation:
There are 10 questions to complete.

### 6 thoughts on “Loop”

1. It should be b[m] in question number 9 of loops. Please correct it.

• Thank You PRAFUL SAMBHAJI RANE,
We have updated the question.

2. Confusion between option c and d in question number 10 of loops of c programming.

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.