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
Questions 11–15 of 17
D= 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3 Answer: (C)
For a given j, the middle loop reaches it for every , so there are j such values of i. The total number of multiplications is:
This is one-sixth of the product of 3 consecutive integers, (equal to ).
Answer: (C)
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. jm, k=n+j-1, and a [n-1]b[j] if i=n
II. in, k=m+i-1, and b[m-1]a[i] if j=m
Answer: (D)
The loop runs while both and , so it stops when one array is used up: either or .
Each pass copies exactly one element into and does together with one of or . Since all three start at 0, at all times.
Take the case . Then , and statement I says , so I is false. Take the case . Then , and statement II says , so II is false.
Both statements are false, so option (C) is wrong too, because it needs one of them to hold. (If started at and the loop used , then and the statements would change.)
Answer: (D)
int main ( ) { /* Line 1 */
int I, N; /* Line 2 */
fro (I =0, I < N, I++); /* Line 3 */
} Identify the compiler's response about this line while creating the object-module:Answer: (A)
Lexical check: every piece is a valid C token: the identifier , brackets, the names , the number 0, and the operators and the commas. So there is no lexical error.
Syntax check: an identifier followed by brackets holding three comma-separated expressions (, , ), ending with a semicolon, is a valid function call statement. So there is no syntax error.
The function is not declared. In classic C, a call to an undeclared function is accepted: the compiler assumes it returns int, and at most gives a warning. The missing definition is found only when linking, not when the object module is created.
So the compiler reports no error for this line.
Answer: (A)
x = m;
y = 1;
while (x - y > e)
{
x = (x + y)/2;
y = m/x;
}
print(x); Answer: (C)
Before the loop, and . After every pass, , so .
The loop stops when , that is, when and are almost equal. At that point the update no longer changes , so putting gives .
Check with : ; then ; then ; then . The values move towards . This is Newton's method for the square root.
Answer: (C)
main()
{
int x, y, m, n;
scanf ("%d %d", &x, &y);
/* Assume x > 0 and y > 0 */
m = x;
n = y;
while (m != n)
{
if (m > n)
m = m - n;
else
n = n - m;
}
printf ("%d", n);
} The program computesAnswer: (C)
Why the gcd does not change: any common divisor of and also divides , and any common divisor of and also divides . So , and the same for . The values stay positive and keep getting smaller, so the loop must stop, and it stops at . Then , which is the gcd of the starting and , and it is printed.
Example with : . It prints 6, which is . The other options give different values for this input: sum , , lcm .
Answer: (C)
Keep practising