Level 2 — RecallC Programming

C Programming

30 minutes40 marksprintable — key stays hidden on paper

Chapter: 5.1 C Programming Level: 2 — Recall (definitions, standard textbook problems, short derivations) Time limit: 30 minutes Total marks: 40

Answer all questions. Assume a typical 64-bit Linux (LP64) platform unless stated otherwise. Use $...$ conventions where math is needed.


Q1. List, in order, the four stages of C compilation and state in one line what each stage produces. (4 marks)

Q2. On a typical LP64 platform, state the size in bytes returned by sizeof for each of the following: char, int, long, double, size_t, int32_t. (6 marks)

Q3. Evaluate the following expressions given int a = 5, b = 2;. Show each result. (4 marks) (a) a & b (b) a | b (c) a ^ b (d) a << 1

Q4. Given the operator precedence rules of C, evaluate the expression below and show the order of evaluation: (4 marks)

int x = 3 + 4 * 2 > 10 ? 100 : 200;

Q5. Consider:

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;

State the value of each: (a) *p, (b) *(p + 3), (c) *(arr + 1), (d) p[4]. (4 marks)

Q6. Name the four common heap/memory error types described below by giving the correct term for each. (4 marks) (a) Freeing the same pointer twice. (b) Accessing memory after it has been freed. (c) Allocating memory and never freeing it. (d) Writing past the end of an allocated buffer.

Q7. Distinguish malloc from calloc in two ways (arguments and initialization). (3 marks)

Q8. Given the union below on an LP64 platform, state (a) sizeof(union U) and (b) explain in one sentence why writing u.i then reading u.f is problematic. (3 marks)

union U { int i; double f; char c[4]; };

Q9. For the string operation below, state the value of strlen(s) and the number of bytes actually occupied including the null terminator. (4 marks)

char s[] = "hello";

Q10. Name the segment of a program's memory layout where each of the following is stored: (4 marks) (a) A global variable initialized to a non-zero value. (b) An uninitialized global variable. (c) A local variable inside a function. (d) Memory returned by malloc.

Answer keyMark scheme & solutions

Q1. (4 marks) — 1 mark each

  1. Preprocessor → expands macros/#include/#define, produces expanded translation unit (.i).
  2. Compiler → translates preprocessed C into assembly (.s).
  3. Assembler → converts assembly into machine code object file (.o).
  4. Linker → combines object files + libraries into a final executable. Why: Each stage transforms one representation into the next lower-level one.

Q2. (6 marks) — 1 mark each

  • char = 1
  • int = 4
  • long = 8 (LP64)
  • double = 8
  • size_t = 8 (64-bit)
  • int32_t = 4 (fixed-width guaranteed) Why: On LP64, long and pointers are 8 bytes; int32_t is always 4 by definition.

Q3. (4 marks) — 1 mark each. a=5 (0101), b=2 (0010). (a) a & b = 0101 & 0010 = 0000 = 0 (b) a | b = 0101 | 0010 = 0111 = 7 (c) a ^ b = 0101 ^ 0010 = 0111 = 7 (d) a << 1 = 1010 = 10

Q4. (4 marks)

  • * binds tighter than +: 4 * 2 = 8 (1 mark)
  • 3 + 8 = 11 (1 mark)
  • relational >: 11 > 10 is true → 1 (1 mark)
  • ternary selects true branch → x = 100 (1 mark) Result: x = 100.

Q5. (4 marks) — 1 mark each (a) *p = 10 (b) *(p+3) = 40 (c) *(arr+1) = 20 (d) p[4] = 50 Why: Pointer arithmetic scales by element size; arr decays to &arr[0].

Q6. (4 marks) — 1 mark each (a) Double free (b) Use-after-free (dangling pointer) (c) Memory leak (d) Buffer overflow

Q7. (3 marks)

  • malloc(size) takes one argument (total bytes); calloc(n, size) takes two (count and element size). (1.5 marks)
  • malloc leaves memory uninitialized; calloc zero-initializes all allocated bytes. (1.5 marks)

Q8. (3 marks) (a) sizeof(union U) = 8 (size of largest member double, with alignment 8). (1.5 marks) (b) Members share the same storage; writing i then reading f reinterprets the int's bit pattern as a double — this is a type-pun / undefined-or-implementation-defined read of an unwritten member, giving a garbage/meaningless value. (1.5 marks)

Q9. (4 marks)

  • strlen(s) = 5 (counts characters up to but not including \0). (2 marks)
  • Bytes occupied including null terminator = 6. (2 marks) Why: "hello" is 5 chars plus 1 terminating \0.

Q10. (4 marks) — 1 mark each (a) Data segment (initialized data) (b) BSS segment (c) Stack (d) Heap

[
  {"claim":"Q3 bitwise results for a=5,b=2","code":"a=5;b=2;result=(a&b==0) and (a|b==7) and (a^b==7) and (a<<1==10)"},
  {"claim":"Q4 ternary expression evaluates to 100","code":"x = 100 if (3 + 4*2 > 10) else 200; result=(x==100)"},
  {"claim":"Q5 pointer arithmetic values from arr={10,20,30,40,50}","code":"arr=[10,20,30,40,50]; result=(arr[0]==10 and arr[3]==40 and arr[1]==20 and arr[4]==50)"},
  {"claim":"Q9 strlen and byte count for 'hello'","code":"s='hello'; result=(len(s)==5 and len(s)+1==6)"}
]