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 6–10 of 54
Consider the following C function definition.
int fX(char *a){
char *b = a;
while(*b)
b++;
return b - a;} Which of the following statements is/are TRUE?One or more options are correct.
Answer: (A), (B), (D)
The function fX increases pointer until it finds '\0'. Then it returns , which is the string length.
- fX("abcd"): a string literal ends with a null character so it returns 4.
- char c[] = "abcd": the array holds 5 chars, including '\0' so it returns 4.
- char *c = "abcd": the pointer points to a literal that ends with a null character so it returns 4.
- The code is valid C (the pointer difference is converted to int) so it compiles.
Answer: Options 1, 2 and 4
- fX("abcd"): a string literal ends with a null character so it returns 4.
- char c[] = "abcd": the array holds 5 chars, including '\0' so it returns 4.
- char *c = "abcd": the pointer points to a literal that ends with a null character so it returns 4.
- The code is valid C (the pointer difference is converted to int) so it compiles.
Answer: Options 1, 2 and 4
What is the output of the following C program?
#include <stdio.h>
int main() {
double a[2]={20.0, 25.0}, *p, *q;
p = a;
q = p + 1;
printf("%d,%d", (int)(q - p), (int)(*q - *p));
return 0;}Answer: (B)
Work out the two values that are printed:
- Here . Subtracting two pointers gives the number of elements between them, not bytes: .
- For the second value, . Casting this to int gives 5.
Output: 1,5
Answer: 1,5
- Here . Subtracting two pointers gives the number of elements between them, not bytes: .
- For the second value, . Casting this to int gives 5.
Output: 1,5
Answer: 1,5
Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices.
The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).
(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)
The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).
int first=0, second=0, len1=0, len2=0, maxlen=0;
for (int i=0; i < n; i++) {
if (X[i] == first) {
len2++; len1++;
} else if (X[i] == second) {
len2++;
len1 = (P) ;
second = first;
} else {
len2 = (Q) ;
len1 = 1; second = first;
}
if (len2 > maxlen) {
maxlen = len2;
}
first = X[i];
} Which one of the following options gives the CORRECT missing expressions?(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)
Answer: (B)
Here first = X[i1], which is the previous element. second is the other value in the current window.
- X[i] == second (and not first): the window still holds only two values, so len2++. The run of equal values ends and a new run starts with X[i]. So len1 = 1 (P) = 1.
- X[i] is a new value: the new window can keep only the last run of equal values (the run of first, of length len1) and X[i]. So len2 = len1 + 1 (Q) = len1+1.
Answer: (P) 1, (Q) len1+1
- X[i] == second (and not first): the window still holds only two values, so len2++. The run of equal values ends and a new run starts with X[i]. So len1 = 1 (P) = 1.
- X[i] is a new value: the new window can keep only the last run of equal values (the run of first, of length len1) and X[i]. So len2 = len1 + 1 (Q) = len1+1.
Answer: (P) 1, (Q) len1+1
What is printed by the following ANSI C program?
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p=NULL; p=&x;
*p=10;
p =&z[1];
*(&z[0]+1)+=3;
printf("%d, %d, %d \n",x,z[0],z[1]); return 0;
}
Answer: (D)
Go through the program one line at a time:
- p is set to x. Then *p = 10 gives x = 10
- Next, p is set to z[1]. p is not used after this.
- The expression means z[1]. So z[1] += 3 gives z[1] = 14
- z[0] is not changed, so 10
The program prints: 10, 10, 14
Answer: 10, 10, 14
- p is set to x. Then *p = 10 gives x = 10
- Next, p is set to z[1]. p is not used after this.
- The expression means z[1]. So z[1] += 3 gives z[1] = 14
- z[0] is not changed, so 10
The program prints: 10, 10, 14
Answer: 10, 10, 14
What is printed by the following ANSI C program?
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++ ){
for(k = 0; k < 3; k++ )
printf("%d ", a[i][j][k]);
printf(" \n");
}
return 0;
}
Answer: (A)
Each inner brace holds 9 values for , and they are stored one after another in order:
a[0][0] = {1, 2, 3}, a[1][0] = {10, 11, 12}, a[2][0] = {19, 20, 21},
The value of j remains 0, and the loops print for k = 0, 1, 2:
1 2 3
10 11 12
19 20 21
Answer: 1 2 3 / 10 11 12 / 19 20 21
a[0][0] = {1, 2, 3}, a[1][0] = {10, 11, 12}, a[2][0] = {19, 20, 21},
The value of j remains 0, and the loops print for k = 0, 1, 2:
1 2 3
10 11 12
19 20 21
Answer: 1 2 3 / 10 11 12 / 19 20 21
Keep practising