Array and Pointer · Programming in C
54 questions · asked in 22 of 27 years (2000–2026)
MCQ 39 · MSQ 1 · NAT 141-mark 34 · 2-mark 20
2000Questions per year2026
Questions 46–50 of 54
Consider the following C program which is supposed to compute the transpose of a given matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.
#include <stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
} Answer: (C)
A swap needs a temporary: save one value in , copy the other over it, then put in the other place. So the body must be . This rules out (B) and (D): they write first, before holds anything useful, so the old value of is lost and the matrix is destroyed.
Now the start of . If starts at (option A), the pair and is swapped when , and swapped back when . Every off-diagonal pair is swapped twice, so is unchanged and not transposed.
If starts at (option C), each pair above the diagonal is swapped exactly once, and the diagonal entry swaps with itself, which does nothing. This gives the transpose.
Answer: (C)
Now the start of . If starts at (option A), the pair and is swapped when , and swapped back when . Every off-diagonal pair is swapped twice, so is unchanged and not transposed.
If starts at (option C), each pair above the diagonal is swapped exactly once, and the diagonal entry swaps with itself, which does nothing. This gives the transpose.
Answer: (C)
Consider the following C program segment:
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s",p); The output of the program isAnswer: (D)
length = strlen("string") = 6. In the loop, p[i] = s[length - i] (not s[length - 1 - i]). For i = 0 this is p[0] = s[6], which is the '\0' that ends the string literal.
printf with %s stops at the first '\0'. Here that is p[0], so it prints an empty string, which is no output.
The other copies (p[1] = 'g', ..., p[5] = 't') come after the '\0' and are never printed. s[0] = 's' is never copied at all.
(A) "gnirts" would need s[length - 1 - i] and a '\0' stored at the end. (C) "gnirt" is what p[1] to p[5] hold, but printf stops at p[0] before reaching them. (B) is the original string and the loop does not copy in forward order.
Answer: (D)
printf with %s stops at the first '\0'. Here that is p[0], so it prints an empty string, which is no output.
The other copies (p[1] = 'g', ..., p[5] = 't') come after the '\0' and are never printed. s[0] = 's' is never copied at all.
(A) "gnirts" would need s[length - 1 - i] and a '\0' stored at the end. (C) "gnirt" is what p[1] to p[5] hold, but printf stops at p[0] before reaching them. (B) is the original string and the loop does not copy in forward order.
Answer: (D)
In the following C program fragment, j, k, n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer 3, and TwoLog_n is initialized to the value of
for (k = 3; k <= n; k++)
A[k] = 0;
for (k = 2; k <= TwoLog_n; k++)
for (j = k + 1; j <= n; j++)
A[j] = A[j] || (j % k);
for (j = 3; j <= n; j++)
if (!A[j]) printf("%d", j); The set of number printed by this program fragment isAnswer: (D)
A[j] becomes 1 as soon as j % k is non-zero for some k that the loops use, that is, for some k with (the inner loop starts at j = k + 1). A[j] stays 0 only if j is divisible by every such k. The program prints those j, for .
Let .
Case : k = j - 1 is used, and j % (j-1) = 1, which is non-zero (j - 1 ≥ 2). So A[j] = 1.
Case : j would have to be a multiple of lcm(2, 3, ..., T). That lcm is at least , and , so it is at least , which is more than for . So no j ≤ n qualifies, and A[j] = 1.
So A[j] = 1 for every j from 3 to n, and nothing is printed. The set is empty.
Answer: (D)
Let .
Case : k = j - 1 is used, and j % (j-1) = 1, which is non-zero (j - 1 ≥ 2). So A[j] = 1.
Case : j would have to be a multiple of lcm(2, 3, ..., T). That lcm is at least , and , so it is at least , which is more than for . So no j ≤ n qualifies, and A[j] = 1.
So A[j] = 1 for every j from 3 to n, and nothing is printed. The set is empty.
Answer: (D)
Assume the following C variable declaration
int *A [10], B[10][10];
Of the following expressions
I A[2]
II A[2][3]
III B[1]
IV B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
int *A [10], B[10][10];
Of the following expressions
I A[2]
II A[2][3]
III B[1]
IV B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
Answer: (A)
is an array of 10 pointers to . is a array of . A left side of an assignment must be a modifiable lvalue.
I. is an variable, so it can be assigned a pointer. Valid.
II. is , an variable. Valid.
III. is a whole row, with array type . An array cannot be assigned to, so this is a compile-time error.
IV. is an element. Valid.
So I, II and IV only.
Answer: (A)
I. is an variable, so it can be assigned a pointer. Valid.
II. is , an variable. Valid.
III. is a whole row, with array type . An array cannot be assigned to, so this is a compile-time error.
IV. is an element. Valid.
So I, II and IV only.
Answer: (A)
Consider the following three functions :
[P1]
[P1]
int * g (void)
{
int x= 10;
return (&x);
} [P2] int * g (void)
{
int * px;
*px= 10;
return px;
} [P3] int *g (void)
{
int *px;
px = (int *) malloc (sizeof(int));
*px= 10;
return px;
} Which of the above three functions are likely to cause problems with pointers ?Answer: (C)
may cause an error, because the function returns the address of a variable declared locally.
will cause a problem, because px is an int pointer that has no address assigned to it, and still we dereference it.
will work, because memory of the size of an int, in bytes, is reserved and its address is stored in px, so it can be used later. Once the function finishes, this memory still stays in the Heap until we free it with the function.
So, the answer is (C).
Keep practising