⏱️
15 questions · 24 marks · 50 minutes. Negative marking on MCQ only: −1/3 mark for a wrong 1-mark MCQ, −2/3 for a wrong 2-mark MCQ. No negative marking for MSQ or NAT; MSQ has no partial credit.
Q1NAT1 markTypes
What is the largest value of an unsigned 16-bit integer?
Numerical answer: ________
Q2MCQ1 markOperators
What is -9 / 4 and -9 % 4 in C?
- (A) −2 and 1
- (B) −2 and −1
- (C) −3 and 3
- (D) −3 and −1
Q3MCQ2 marksShort-circuit
What does this print?
int x = 1, y = 0, z;
z = y && (x = 7);
z = z || x++;
printf("%d %d %d\n", x, y, z);
- (A)
7 0 1 - (B)
2 0 0 - (C)
2 0 1 - (D)
1 0 1
Q4MCQ1 markConversion
printf("%d", -1 < 2u); prints:
- (A) −1
- (B) 1
- (C) 2
- (D) 0
Q5NAT2 marksSwitch
What does this print?
int k = 3, s = 0;
switch (k * 2 - 1) {
case 4: s += 4;
case 5: s += 5;
case 6: s += 6; break;
case 7: s += 7;
default: s += 1;
}
printf("%d\n", s);
Numerical answer: ________
Q6NAT2 marksLoops
How many times does count++ run?
int i, j, count = 0;
for (i = 1; i <= 32; i *= 2)
for (j = 0; j < i; j += 2)
count++;
printf("%d\n", count);
Numerical answer: ________
Q7MCQ2 marksStatic
What does this print?
int g(int n)
{
static int acc;
acc = acc + n;
return acc;
}
int main(void)
{
g(1);
g(2);
printf("%d\n", g(3) + g(4));
return 0;
}
- (A)
16 - (B)
7 - (C)
10 - (D)
13
Q8NAT1 markCall stack
main calls f; f calls g twice; g calls h twice. Counting main, what is the deepest number of frames at once?
Numerical answer: ________
Q9MCQ2 marksPointers
What does this print?
int a = 1, b = 2;
int *p = &a, *q = &b;
*p = *q + 5;
p = q;
*p = *p * 3;
printf("%d %d\n", a, b);
- (A)
21 2 - (B)
7 6 - (C)
21 6 - (D)
7 2
Q10MCQ2 marksCall by reference
What does this print?
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int main(void)
{
int i = 0, j = 1;
f(&i, &j);
printf("%d %d\n", i, j);
return 0;
}
- (A)
0 2 - (B)
0 1 - (C)
2 1 - (D)
2 2
Q11NAT1 markPointer arithmetic
A long long *p (8 bytes) holds 1000. What does p + 6 hold?
Numerical answer: ________
Q12NAT2 marksArrays
What is the second number printed?
int a[] = {10, 20, 30, 40, 50};
int *p = a;
p = p + 3;
*p = *(p - 1) + p[1];
p--;
printf("%d %d\n", *p, a[3]);
Numerical answer: ________
Q13NAT2 marks2-D arrays
int a[5][7]; starts at 100 (4-byte ints). What is the address of a[4][6]?
Numerical answer: ________
Q14MCQ1 markStrings
char s[] = "exam"; — sizeof(s) and strlen(s) are:
- (A) 4 and 4
- (B) 5 and 4
- (C) 5 and 5
- (D) 4 and 5
Q15MSQ2 marksMemory and macros
Which are true?
- (A) Returning the address of a static local variable is safe.
- (B) With
#define SQ(x) x*x, SQ(1+1) is 3. - (C) free(p) sets p to NULL.
- (D) sizeof(struct { char c; int i; }) is 8 under natural alignment.
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: 655351 mark
2¹⁶ − 1.
Re-read: Lesson 1.1.1 — Data types and their sizes; signed vs unsigned; constants; variables; declaration vs definition
Q2 Answer: (B)1 mark
Truncation towards zero; remainder takes the sign of −9.
- (A) is wrong: The remainder is negative.
- (C) is wrong: C truncates towards zero.
- (D) is wrong: The quotient is −2.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q3 Answer: (C)2 marks
Lesson 1.1.2 Q3.
Output
2 0 1
- (A) is wrong: x = 7 is skipped.
- (B) is wrong: x++ is evaluated and is non-zero.
- (D) is wrong: x++ increments x.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q4 Answer: (D)1 mark
−1 becomes a huge unsigned value.
- (A) is wrong: Comparisons give 0 or 1.
- (B) is wrong: Signed meets unsigned as unsigned.
- (C) is wrong: Comparisons give 0 or 1.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q5 Answer: 112 marks
Lesson 1.2.1 Q2.
Output
11
Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through
Q6 Answer: 322 marks
i = 1, 2, 4, 8, 16, 32 → inner ⌈i/2⌉ = 1, 1, 2, 4, 8, 16 → 32.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q7 Answer: (A)2 marks
acc = 1, 3, then 6 and 10: 16.
Output
16
- (B) is wrong: acc keeps growing.
- (C) is wrong: Both later calls add to acc.
- (D) is wrong: The two returns are 6 and 10.
Re-read: Lesson 1.3.2 — Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
Q8 Answer: 41 mark
main → f → g → h.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q9 Answer: (B)2 marks
a = 7; p now points to b; b = 6.
Output
7 6
- (A) is wrong: The multiplication is applied to b.
- (C) is wrong: a is not tripled.
- (D) is wrong: b is tripled.
Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer
Q10 Answer: (A)2 marks
Lesson 1.4.2 example.
Output
0 2
- (B) is wrong: j becomes 2.
- (C) is wrong: i is never written.
- (D) is wrong: Only j changes.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q11 Answer: 10481 mark
1000 + 48.
Re-read: Lesson 1.4.3 — Pointer arithmetic and why it scales by type size
Q12 Answer: 802 marks
a[3] becomes 30 + 50 = 80.
Output
30 80
Re-read: Lesson 1.5.1 — 1-D arrays; a[i] == *(a+i) == i[a]; arrays decaying to pointers; passing arrays to functions; the sizeof trap
Q13 Answer: 2362 marks
(4 × 7 + 6) × 4 = 136 → 236.
Re-read: Lesson 1.5.2 — 2-D arrays; row-major layout; pointer to array vs array of pointers; reading complex declarations
Q14 Answer: (B)1 mark
The array includes '\0'.
- (A) is wrong: sizeof counts \0.
- (C) is wrong: strlen excludes \0.
- (D) is wrong: Reversed.
Re-read: Lesson 1.5.3 — Strings: char arrays, '\0', read-only string literals, strlen vs sizeof, string functions and tracing string code
Q15 Answer: (A) (B) (D)2 marks
free does not change p.
- (C) is wrong: p still holds the old address.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
If you got these wrong, re-read
| Question | Marks | Topic | Re-read |
|---|---|---|---|
| Q1 | 1 | Types | Lesson 1.1.1 |
| Q2 | 1 | Operators | Lesson 1.1.2 |
| Q3 | 2 | Short-circuit | Lesson 1.1.2 |
| Q4 | 1 | Conversion | Lesson 1.1.3 |
| Q5 | 2 | Switch | Lesson 1.2.1 |
| Q6 | 2 | Loops | Lesson 1.2.2 |
| Q7 | 2 | Static | Lesson 1.3.2 |
| Q8 | 1 | Call stack | Lesson 1.3.3 |
| Q9 | 2 | Pointers | Lesson 1.4.1 |
| Q10 | 2 | Call by reference | Lesson 1.4.2 |
| Q11 | 1 | Pointer arithmetic | Lesson 1.4.3 |
| Q12 | 2 | Arrays | Lesson 1.5.1 |
| Q13 | 2 | 2-D arrays | Lesson 1.5.2 |
| Q14 | 1 | Strings | Lesson 1.5.3 |
| Q15 | 2 | Memory and macros | Lesson 1.8 |