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 1–5 of 54
Consider the following ANSI-C program.
Note: Assume that the program compiles and runs successfully.
#include <stdio.h>
int main(){
int *ptr, a, b, c;
a=5; b=11; c=20;
ptr=&a; *ptr=c; ptr=&c;
a=*(&b); c=*ptr-a;
printf("%d",c);
return(0);
} The output of this program is ________ . (answer in integer) Note: Assume that the program compiles and runs successfully.
Answer: 9
Go through the program one line at a time:
Start with the values: .
Next, (ptr now points to ).
Then → this sets , since ptr points to and we assign it the current value of , which is .
After that, (ptr now points to instead).
Then → this sets , so it overwrites the earlier .
Then → here is itself, since ptr points to . Its value is still , as it has not changed since it was first set, so .
The call prints the final value of , which is .
Answer: 9
Start with the values: .
Next, (ptr now points to ).
Then → this sets , since ptr points to and we assign it the current value of , which is .
After that, (ptr now points to instead).
Then → this sets , so it overwrites the earlier .
Then → here is itself, since ptr points to . Its value is still , as it has not changed since it was first set, so .
The call prints the final value of , which is .
Answer: 9
The pseudocode of a function fun() is given below:
fun(int A[0,...,n-1])
{
for i=0 to n-2
for j=0 to n-i-2
if (A[j] > A[j+1])
then swap A[j] and A[j+1]
}
Let be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun() is called with as argument, is __________. (Answer in integer)Answer: 435
This is the standard bubble-sort pseudocode. The array is in descending order, so every comparison of adjacent elements finds them out of order. A swap therefore happens on every comparison. No comparison skips the swap.
The total number of comparisons, and so the number of swaps, for an array of elements. Putting in : .
Answer: 435
The total number of comparisons, and so the number of swaps, for an array of elements. Putting in : .
Answer: 435
#include <stdio.h>
void foo(int *p, int x) {
*p = x;
}
int main() {
int *z;
int a = 20, b = 25;
z = &a;
foo(z, b);
printf("%d", a);
return 0;
}
The output of the given C program is __________. (Answer in integer) Answer: 25
The statement sets to hold the address of , whose value is now .
The call sends (the address of ) and the value of () to . So inside , points to and .
The statement stores at the place points to. That place is itself, because a pointer gives direct access to the variable of the caller. So is now .
Then prints the updated value of .
Answer: 25
The call sends (the address of ) and the value of () to . So inside , points to and .
The statement stores at the place points to. That place is itself, because a pointer gives direct access to the variable of the caller. So is now .
Then prints the updated value of .
Answer: 25
Consider the following C program:
#include <stdio.h>
void stringcopy(char *, char *);
int main(){
char a[30] = "@#Hello World!";
stringcopy(a, a + 2);
printf("%s\n", a);
return 0;
}
void stringcopy(char *s, char *t) {
while(*t)
*s++ = *t++;
}
Which ONE of the following will be the output of the program?Answer: (D)
The string @#Hello World! has 14 characters at indices to , and the null character '\0' is at index .
In the call, points to index and points to index . The loop copies to and moves both pointers, and it stops when is null. So it copies indices to (12 characters, Hello World!) into indices to .
The loop ends before the null at index is copied, so no new null is written. Indices , and keep their old values d, ! and '\0'.
printf with %s prints from index up to the first null at index : Hello World! followed by the leftover d!.
Answer: (D)
In the call, points to index and points to index . The loop copies to and moves both pointers, and it stops when is null. So it copies indices to (12 characters, Hello World!) into indices to .
The loop ends before the null at index is copied, so no new null is written. Indices , and keep their old values d, ! and '\0'.
printf with %s prints from index up to the first null at index : Hello World! followed by the leftover d!.
Answer: (D)
Consider the following C program:
#include <stdio.h>
int main(){
int a;
int arr[5] = {30,50,10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}
The output of the above program is ___________. (Answer in integer)Answer: 111
(remaining elements zero-initialized, since at least one initializer was given).
→ points to .
→ .
→ increments from to .
→ now points to (unaffected).
computes (using 's updated value , since the increment already happened).
Answer: 111
→ points to .
→ .
→ increments from to .
→ now points to (unaffected).
computes (using 's updated value , since the increment already happened).
Answer: 111
Keep practising