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 21–25 of 54
Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y.
int main ( ) {
int array[]={3,5,1,4,6,2};
int done =0 ;
int i ;
while (done == 0) {
done = 1;
for (i = 0; i <=4; i ++) {
if (array [i] < array [i +1]) {
swap (& array [i], &array [i+1]);
done = 0;
}
}
for (i = 5 ; i >=1; i --) {
if (array [i] > array [ i-1]) {
swap ( & array [i] , &array [i-1]);
done = 0;
}
} }
printf ( " %d " , array [3] );
} The output of the program is ____________.Answer: 3
Each pass swaps two neighbours whenever the smaller one comes first. So the while loop keeps running until the array is in descending order: 6, 5, 4, 3, 2, 1.
In this order, array[3] = 3.
Answer: 3
In this order, array[3] = 3.
Answer: 3
Consider the following C Program.
#include <stdio.h>
#include <string.h>
int main ( ) {
char* c = "GATECSIT2017";
char* p = c;
printf("%d", (int) strlen (c+2[p]-6[p]-1));
return 0;
}
The output of the program is _______________.Answer: 2
Here 2[p] is the same as p[2], which is 'T' (84). Also 6[p] is the same as p[6], which is 'I' (73).
So the pointer is c + 84 73 1 = c + 10. It points to the string "17".
strlen("17") = 2
Answer: 2
So the pointer is c + 84 73 1 = c + 10. It points to the string "17".
strlen("17") = 2
Answer: 2
Consider the following C program.
#include <stdio.h>
void mystery(int *ptra,int *ptrb){
int *temp;
temp =ptrb;
ptrb =ptra;
ptra =temp;
}
int main(){
int a=2016,b=0,c=4,d=42;
mystery(&a, &b);
if (a < c)
mystery(&c, &a);
mystery(&a, &d);
printf("%d\n", a);
} The output of the program is _________.Answer: 2016
The function mystery swaps only its local copies of the pointers (ptra, ptrb). It never changes the values *ptra or *ptrb. So a, b, c and d in main stay the same.
The value of a is still 2016.
Answer: 2016
The value of a is still 2016.
Answer: 2016
The value printed by the following program is .
void f(int*p, int m){
m =m+5;
*p =*p+m;
return;
}
void main(){
int i=5, j=10;
f(&i, j);
printf("%d", i+j);
}Answer: 30
m takes j's value: m = 10 + 5 = 15.
*p changes i: i = 5 + 15 = 20.
j is unchanged: 10.
Sum: i + j = 20 + 10 = 30
Answer: 30
*p changes i: i = 5 + 15 = 20.
j is unchanged: 10.
Sum: i + j = 20 + 10 = 30
Answer: 30
The output of the following C program is__________.
void f1(int a, int b) {
int c;
c=a; a=b; b=c;
}
void f2(int *a, int *b) {
int c;
c=*a; *a=*b; *b=c;
}
int main(){
int a=4, b=5, c=6;
f1(a,b);
f2(&b, &c);
printf("%d",c-a-b);
}Answer: -5
Go through each call:
- f1 gets only copies (call by value), so a = 4 and b = 5 do not change.
- f2 swaps using pointers, so b = 6 and c = 5.
c a b = 5 4 6 = 5
Answer: -5
- f1 gets only copies (call by value), so a = 4 and b = 5 do not change.
- f2 swaps using pointers, so b = 6 and c = 5.
c a b = 5 4 6 = 5
Answer: -5
Keep practising