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 1–5 of 17
Consider the following C program:
#include <stdio.h>
int gate(int n) {
int d, t, newnum, turn;
newnum = turn = 0; t=1;
while(n >= t) t *= 10;
t /= 10;
while(t > 0) {
d = n / t;
n = n % t;
t /= 10;
if(turn) newnum = 10 * newnum + d;
turn = (turn + 1) % 2;
}
return newnum;
}
int main () {
printf("%d", gate(14362));
return 0;
}
The value printed by the given C program is _________. (Answer in integer)Answer: 46
The function first finds how many digits has. It gets digits, using the loop that scales by 10. Then it takes the digits from the most significant to the least significant. It keeps every other digit, beginning with the 2nd digit, because begins at (skip, keep, skip, keep, ...).
Digits of in order: .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
Final value: .
Answer: 46
Digits of in order: .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
Final value: .
Answer: 46
int x=126,y=105;
do {
if(x>y) x=x-y;
else y=y-x;
} while(x!=y);
printf("%d",x); The output of the given C code segment is ________. (Answer in integer)Answer: 21
The loop works like the subtraction-based Euclidean algorithm. It finds . At each step, the smaller number is taken away from the larger one. This goes on until both numbers are the same, and that happens at their GCD.
Start with : first ; next ; next ; next ; then — now , so the loop stops.
Answer: 21
Start with : first ; next ; next ; next ; then — now , so the loop stops.
Answer: 21
Consider the following C function definition.
int f(int x, int y) {
for (int i=0; i < y; i++) {
x=x+x+y;
}
return x;
} Which of the following statements is/are TRUE about the above function?One or more options are correct.
Answer: (B), (D)
Each pass does , and the loop runs y times. After y passes:
- (A) : . This is less than , so (A) is false. It is more than , so (C) is false.
- (B) : , so (B) is true.
- (D) : , so (D) is true.
(The two large values are about and . Both are below , so int does not overflow.)
Answer: (B), (D)
- (A) : . This is less than , so (A) is false. It is more than , so (C) is false.
- (B) : , so (B) is true.
- (D) : , so (D) is true.
(The two large values are about and . Both are below , so int does not overflow.)
Answer: (B), (D)
Consider the following C program:
#include <stdio.h>
int main() {
float sum = 0.0, j = 1.0, i = 2.0;
while (i / j > 0.0625) {
j = j + j;
sum = sum + i/j;
printf("%f \n", sum);
}
return 0;
} The number of times variable sum will be printed When the above program is executed is _________ .Answer: 5
i stays 2. j doubles each pass.
- i/j = 2 j = 2, print (1)
- i/j = 1 j = 4, print (2)
- i/j = 0.5 j = 8, print (3)
- i/j = 0.25 j = 16, print (4)
- i/j = 0.125 j = 32, print (5)
- i/j = 0.0625 is not 0.0625 stop
Answer: 5
- i/j = 2 j = 2, print (1)
- i/j = 1 j = 4, print (2)
- i/j = 0.5 j = 8, print (3)
- i/j = 0.25 j = 16, print (4)
- i/j = 0.125 j = 32, print (5)
- i/j = 0.0625 is not 0.0625 stop
Answer: 5
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 isAnswer: (B)
The first loop keeps halving until it reaches 1. This gives j = 40.
Now the second loop runs. Start with j = 40 20 10 5 2 1. The loop adds 1 to sum each time j is 1, so it runs 5 times.
Answer: 5
Now the second loop runs. Start with j = 40 20 10 5 2 1. The loop adds 1 to sum each time j is 1, so it runs 5 times.
Answer: 5
Keep practising