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 26–30 of 54
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.
int main () {
unsigned int x[4][3] ={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}Answer: (A)
One row has 3 ints, so it is 12 bytes.
- x + 3 is the address of row 3: 2000 + 3 12 = 2036
- *(x + 3) is the address of x[3][0]: 2036
- *(x + 2) + 3 is the address of x[2][0] plus 3 ints: 2024 + 12 = 2036
Answer: 2036, 2036, 2036
- x + 3 is the address of row 3: 2000 + 3 12 = 2036
- *(x + 3) is the address of x[3][0]: 2036
- *(x + 2) + 3 is the address of x[2][0] plus 3 ints: 2024 + 12 = 2036
Answer: 2036, 2036, 2036
Consider the following C program segment.
#include <stdio.h>
int main()
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0';
printf("%s", s1);
} What will be printed by the program?Answer: (C)
The pointer p = s1 + 2 holds the address of s1[2], which is the character '3'. The statement *p = '0' replaces that character with '0'.
So s1 now reads "1204". The string still stops at the original '\0'.
Answer: 1204
So s1 now reads "1204". The string still stops at the original '\0'.
Answer: 1204
Consider the following C program.
#include <stdio.h>
int main( )
{
static int a[ ] = {10, 20, 30, 40, 50};
static int *p[ ] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;
ptr++;
printf("%d%d", ptr-p,**ptr);
}The output of the program is ______________.Answer: 140
The statement ptr++ moves ptr forward, so it now points to p[1].
- The difference ptr p = 1
- **ptr = *p[1] = *(a + 3) = 40
The format "%d%d" prints the two values one after the other: 140
Answer: 140
- The difference ptr p = 1
- **ptr = *p[1] = *(a + 3) = 40
The format "%d%d" prints the two values one after the other: 140
Answer: 140
Consider the following program in C language:
#include <stdio.h>
main()
{
int i;
int * pi = &i;
scanf( "%d", pi) ;
printf ("%d \n", i+5) ;
} Which one of the following statements is TRUE?Answer: (D)
The pointer pi holds the address of i. So scanf("%d", pi) saves the number you type into i. Then printf prints the value of i + 5.
Answer: On execution, the value printed is 5 more than the integer value entered
Answer: On execution, the value printed is 5 more than the integer value entered
Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.
int ProcessArray(int *listA, int x, int n)
{
int i, j, k;
i = 0;
j = n-1;
do {
k = (i+j)/2;
if (x <= listA[k])
j = k-1;
if (listA[k] <= x)
i = k+1;
}while (i <= j);
if (listA[k] == x)
return(k);
else
return -1;
} Which one of the following statements about the function ProcessArray is CORRECT?Answer: (B)
Here k is the middle index of the range [i, j]. If x A[k], the search moves to the left half. If x A[k], the search moves to the right half. If x equals A[k], both updates run, so i becomes j, the loop stops, and A[k] = x.
This is binary search. It returns k when x is found and 1 when it is not found, and the loop always ends.
Answer: It is an implementation of binary search
This is binary search. It returns k when x is found and 1 when it is not found, and the loop always ends.
Answer: It is an implementation of binary search
Keep practising