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
Questions 41–45 of 54
#include <stdio.h>
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
printf("%d,", a[0][2]);
printf("%d,", *a[2]);
printf("%d,", *++a[0]);
printf("%d,", *(++a)[0]);
printf("%d\n", a[-1][+1]);
}
main()
{
print(x);
} Answer: (A)
1. a[0][2] = a1[2] = 8.
2. [] binds tighter than *, so *a[2] = *(a[2]) = *a3 = a3[0] = -12.
3. ++a[0] increases the pointer a[0] (that is x[0]) by one int, so it now points to a1[1]. The * gives 7. (a1 itself is an array name and cannot change; x[0] is what moves.)
4. In (++a)[0], the ++a runs first and a now points to x[1]. So (++a)[0] = x[1] = a2, and * gives a2[0] = 23.
5. a now points to x[1], so a[-1] is x[0], which points to a1[1] after step 3. Then [+1] moves one more place to a1[2] = 8.
Output: 8, -12, 7, 23, 8. (B) has 8 as the second value but *a[2] is -12; (C) and (D) start with -12 but a[0][2] is 8.
Answer: (A)
#include <stdio.h>
struct test {
int i;
char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{
struct test *p = st;
p += 1;
++p -> c;
printf("%s,", p++ -> c);
printf("%c,", *++p -> c);
printf("%d,", p[0].i);
printf("%s \n", p -> c);
} Answer: (B)
Program with the grouping of each expression marked:
#include <stdio.h>
struct test {
int i;
char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
int main () {
struct test *p = st;
p += 1;
++p->c; // ++(p->c)
printf("%s,", p++->c); // (p++)->c
printf("%c,", *++p->c); // *(++(p->c))
printf("%d,", p[0].i);
printf("%s \n", p->c);
}
Assume an int takes bytes and a pointer takes bytes. With no padding between the members, each structure takes bytes.
Precedence: -> binds tighter than prefix ++ and unary *, as the figure shows. So ++p->c is ++(p->c) and *++p->c is *(++(p->c)). Postfix ++ is at the same level as -> and groups left to right, so p++->c is (p++)->c.
Starting state:
struct test *p = st; makes p point to st[0].
p += 1; adds bytes (one structure), so p points to st[1].
++p->c; moves the string pointer of st[1] on by one character, from "better" to "etter". p itself does not move.
printf("%s,", p++->c); prints "etter", and then p moves on to st[2].
printf("%c,", *++p->c); moves the string pointer of st[2] from "jungle" to "ungle", and prints its first character, 'u'.
printf("%d,", p[0].i); prints the integer of st[2], which is . In the figure, the form *(p+0)->i is not valid C. It should read (p+0)->i, which is the same as p[0].i.
printf("%s \n", p->c); prints the string now held by st[2], which is "ungle".
The output is etter,u,6,ungle. Options (A) and (C) are wrong because the first item is "etter", not "jungle" or "cetter". Option (D) is wrong because p is still on st[2] at the end, so the last two items are and "ungle", not and "ncestor".
Answer: (B)
void swap(int *px,int *py){
*px=*px-*py;
*py=*px+*py;
*px=*py-*px;
} S1 : will generate a compilation error S2 : may generate a segmentation fault at runtime depending on the arguments passed
S3 : correctly implements the swap procedure for all input pointers referreing to integers stored in memory locations accessible tot he process
S4 : implements the swap procedure correctly for some but not all valid input pointers
S5 : may add or subtract integers and pointers
Answer: (C)
So the values are swapped when px and py point to different locations (ignoring integer overflow).
S1 is false. Dereferencing pointers and doing integer arithmetic on the values is valid C, so the code compiles.
S2 is true. If px or py is NULL or points to memory the process cannot access, the dereference can cause a segmentation fault.
S3 is false. If px and py point to the same location, , then , then . The value is lost, but it should stay the same. Overflow in is another possible problem.
S4 is true. The code works for pointers to different locations but not for the same location, so it is correct for some valid inputs but not all.
S5 is false. The arithmetic is done on the integer values and , never on the pointers themselves.
So S2 and S4 are true.
Answer: (C)
i = 0; j = 1;
while (j < n ){
if (E) j++;
else if (a[j] - a[i] == S) break;
else i++;
}
if (j < n) printf("yes") else printf ("no"); Choose the correct expression for E.
Answer: (B)
- If a[j] - a[i] < S, it is too small. Only a larger j can help, so j++. This is the test E.
- If a[j] - a[i] == S, the pair is found, so break.
- Otherwise the difference is larger than S, so a bigger j only makes it worse; move i up with i++.
Since S > 0, a[j] - a[i] is 0 when i = j, which is less than S, so i never gets ahead of j. The loop ends either with a break (j < n, prints yes) or when j reaches n (prints no).
(A) would do j++ when the difference is already too big. (C) is a[i] - a[j] < S, which is true for every j ≥ i (it is 0 or negative), so j would always increase and the pair would never be found. (D) is never true for j ≥ i.
Answer: (B)
int anagram (char *a, char *b) {
int count [128], j;
for (j = 0; j < 128; j++) count[j] = 0;
j = 0;
while (a[j] && b[j]) {
A;
B;
}
for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
} Choose the correct alternative for statements A and B.
Answer: (D)
Statement A uses j and statement B must do the j++ after it uses b[j]. This is option (D): count[a[j]]++ and count[b[j++]]--.
(A) never changes j, so the while loop does not end. (B) adds 1 for both strings, so the counts never cancel, and j also never moves. (C) does j++ inside A, so B then uses the next index of b and compares a[j] with b[j+1].
Example a = ISS, b = SIS: count[I] +1, count[S] -1; then count[S] +1, count[I] -1; then count[S] +1 and count[S] -1. All counts are 0, so it returns 1.
Answer: (D)
Keep practising