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
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)?Answer: (C)
Each pass changes r to and q to , so does not change. If is true before the loop, it is true after every pass and when the loop ends.
In (C), and give at the start. Also makes r fall by at least 1 each pass, so r becomes less than y and the loop stops.
Why the others fail:
- (A) gives , but x is not fixed to 0. Also, if then is always true and the loop never ends.
- (B) q is not fixed. For example ends with , and .
- (D) r is not fixed. For example ends with , and .
Answer: (C)
In (C), and give at the start. Also makes r fall by at least 1 each pass, so r becomes less than y and the loop stops.
Why the others fail:
- (A) gives , but x is not fixed to 0. Also, if then is always true and the loop never ends.
- (B) q is not fixed. For example ends with , and .
- (D) r is not fixed. For example ends with , and .
Answer: (C)
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 isAnswer: (D)
Two pointers start at the two ends of the array. In each step, the smaller of p[a] and p[b] is dropped, so the maximum is never dropped. The loop must stop when only one element is left, that is, when a and b become equal.
The loop condition is: b != a.
Answer: b != a
The loop condition is: b != a.
Answer: b != a
The following function computes 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?Answer: (C)
Show that always equals :
- At the start: holds.
- When b is even: , and holds.
- When b is odd: , and holds.
At b = 0, res = .
Answer:
- At the start: holds.
- When b is even: , and holds.
- When b is odd: , and holds.
At b = 0, res = .
Answer:
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 isAnswer: (B)
Each pass of the loop takes y away from r and adds 1 to q. So the program is dividing x by y. The invariant is x = qy + r. The loop ends when r is y.
Answer: {x = qy + r r y}
Answer: {x = qy + r r y}
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 theAnswer: (A)
At the start, Y holds the sum of the whole array. The whole array is also a sub-array.
Next, the nested loops find the sum Z of each sub-array E[i..j]. Whenever Z is bigger than Y, Y is updated. So Y ends up as the largest sub-array sum.
Answer: maximum possible sum of elements in any sub-array of array E
Next, the nested loops find the sum Z of each sub-array E[i..j]. Whenever Z is bigger than Y, Y is updated. So Y ends up as the largest sub-array sum.
Answer: maximum possible sum of elements in any sub-array of array E
Keep practising