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 31–35 of 54
What does the following fragment of C-program print?
char c []="GATE2011";
char *p =c;
printf ("%s", p + p[3]- p[ 1 ]);
char c []="GATE2011";
char *p =c;
printf ("%s", p + p[3]- p[ 1 ]);
Answer: (C)
Here p[3] = 'E' (69) and p[1] = 'A' (65). So the pointer becomes p + 69 65 = p + 4.
The address p + 4 points to the string "2011".
Answer: 2011
The address p + 4 points to the string "2011".
Answer: 2011
What does the following program print?
#include <stdio.h>
void f (int *p, int * q) {
p=q;
*p=2;
}
int i= 0, j= 1;
int main ( ){
f(&i, & j);
printf( "%d%d \n", i,j);
return 0;
} Answer: (D)
In f, the statement p = q makes p point to j. This change to p stays inside the function. Then *p = 2 sets j to 2. The value of i does not change.
Printed output: 0 2
Answer: 0 2
Printed output: 0 2
Answer: 0 2
C program is given below:
What should be the contents of the array b at the end of the program?
#include <stdio.h>
int main ()
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
} What should be the contents of the array b at the end of the program?
Answer: (B)
p = *b points to the first char of b, and b is stored row by row, with 2 chars per row. So the char at p + 2*j + i is b[j][i].
The statement therefore does b[j][i] = a[i][j]: it copies a into b transposed.
For i = 0 it writes offsets 0, 2, 4 with a[0][0..2] = a, b, c. For i = 1 it writes offsets 1, 3, 5 with a[1][0..2] = d, e, f.
Memory in order is a d b e c f, so the rows of b are (a d), (b e), (c f).
Option (A) would be a plain row-by-row copy, which needs *(p + 3*i + j); it does not match this index.
Answer: (B)
The statement therefore does b[j][i] = a[i][j]: it copies a into b transposed.
For i = 0 it writes offsets 0, 2, 4 with a[0][0..2] = a, b, c. For i = 1 it writes offsets 1, 3, 5 with a[1][0..2] = d, e, f.
Memory in order is a d b e c f, so the rows of b are (a d), (b e), (c f).
Option (A) would be a plain row-by-row copy, which needs *(p + 3*i + j); it does not match this index.
Answer: (B)
Consider the C program given below. What does it print?
#include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for(i = 0; i < 3; i++) {
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
} Answer: (C)
First loop (the outer is changed twice per pass: once by in the body and once by the loop's own ):
: ; body makes ; loop makes .
: ; body makes ; loop makes .
: is false, so the loop ends. Then makes the outer .
Now .
Second loop: the statement declares a new inside the loop body. It hides the outer only inside that body, and the outer stays .
: inner , .
: inner , .
: inner , .
: is false, so the loop ends.
printf sees the outer and , so it prints .
Answer: (C)
: ; body makes ; loop makes .
: ; body makes ; loop makes .
: is false, so the loop ends. Then makes the outer .
Now .
Second loop: the statement declares a new inside the loop body. It hides the outer only inside that body, and the outer stays .
: inner , .
: inner , .
: inner , .
: is false, so the loop ends.
printf sees the outer and , so it prints .
Answer: (C)
What is the output printed by the following C code?
#include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%s\n", a);
} Answer: (B)
Start: holds w, o, r, l, d and the null character '\0' at index .
In the first pass, and , so , the null character. Now the first character of the array is the null character.
The loop goes on for two more passes ( and ) and stops when fails, but these do not change .
printf with %s prints characters from the start of the array until it meets a null character. The very first character is null, so it prints nothing: an empty (null) string.
Answer: (B)
In the first pass, and , so , the null character. Now the first character of the array is the null character.
The loop goes on for two more passes ( and ) and stops when fails, but these do not change .
printf with %s prints characters from the start of the array until it meets a null character. The very first character is null, so it prints nothing: an empty (null) string.
Answer: (B)
Keep practising