Level 1 — RecognitionC Programming

C Programming

20 minutes40 marksprintable — key stays hidden on paper

Chapter: 5.1 C Programming Level: 1 — Recognition (MCQ + Matching + True/False with justification) Time limit: 20 minutes Total marks: 40


Section A — Multiple Choice (1 mark each, 15 marks)

Choose the single best answer.

Q1. Which stage of C compilation replaces #include and #define directives?

  • (a) Linker
  • (b) Compiler
  • (c) Preprocessor
  • (d) Assembler

Q2. What is the correct order of the C build pipeline?

  • (a) Compiler → Preprocessor → Assembler → Linker
  • (b) Preprocessor → Compiler → Assembler → Linker
  • (c) Preprocessor → Assembler → Compiler → Linker
  • (d) Assembler → Preprocessor → Compiler → Linker

Q3. Which type is guaranteed to be exactly 32 bits wide on all conforming platforms?

  • (a) int
  • (b) long
  • (c) int32_t
  • (d) size_t

Q4. The result type of the sizeof operator is:

  • (a) int
  • (b) size_t
  • (c) long
  • (d) unsigned char

Q5. In C, the expression a = (1, 2, 3); assigns to a the value:

  • (a) 1
  • (b) 2
  • (c) 3
  • (d) undefined

Q6. Given int a[5];, which expression is equivalent to a[2]?

  • (a) *(a + 2)
  • (b) *a + 2
  • (c) &a + 2
  • (d) a + 2

Q7. Which function returns memory that is zero-initialised?

  • (a) malloc
  • (b) realloc
  • (c) calloc
  • (d) free

Q8. Uninitialised global variables are stored in which memory segment?

  • (a) Text
  • (b) BSS
  • (c) Heap
  • (d) Stack

Q9. Calling free twice on the same pointer causes:

  • (a) A guaranteed compile error
  • (b) A memory leak
  • (c) Double free (undefined behavior)
  • (d) Automatic re-initialisation

Q10. Which standard function limits how many characters are copied, helping avoid buffer overflow?

  • (a) strcpy
  • (b) strcat
  • (c) strncpy
  • (d) sprintf

Q11. To access a struct member through a pointer p, you write:

  • (a) p.member
  • (b) p->member
  • (c) *p.member
  • (d) &p->member

Q12. A union differs from a struct because its members:

  • (a) Are all stored at consecutive addresses
  • (b) Share the same memory location
  • (c) Cannot be accessed by name
  • (d) Are always bit fields

Q13. Which keyword tells the compiler a variable may change outside program control (e.g. a hardware register), preventing certain optimisations?

  • (a) restrict
  • (b) static
  • (c) volatile
  • (d) const

Q14. The macros va_start, va_arg, va_end are used with:

  • (a) Function pointers
  • (b) Variadic functions
  • (c) Bit fields
  • (d) Enumerations

Q15. Which of the following is a compile-time check?

  • (a) assert
  • (b) static_assert
  • (c) malloc returning NULL check
  • (d) Valgrind

Section B — Matching (1 mark each, 10 marks)

Q16. Match each tool/keyword (left) to its purpose (right). Write pairs like A-3.

Left Right
A. restrict 1. Detects memory leaks & invalid access at runtime
B. Valgrind 2. Hint that pointers do not alias
C. typedef 3. Prevents multiple inclusion of a header
D. #ifndef guard 4. Creates an alias name for a type
E. Function pointer 5. Enables callbacks

Q17. Match each operator to its category. Write pairs like A-3.

Left Right
A. && 1. Bitwise
B. ^ 2. Logical
C. % 3. Relational
D. <= 4. Arithmetic
E. += 5. Assignment

Section C — True/False with Justification (3 marks each, 15 marks)

State True or False (1 mark) and give a one-line justification (2 marks).

Q18. "An array name passed to a function decays to a pointer, so sizeof inside the function gives the whole array size."

Q19. "malloc(0) is guaranteed to return NULL."

Q20. "Signed integer overflow in C is undefined behavior."

Q21. "A macro defined with #define SQ(x) x*x used as SQ(a+b) computes (a+b)*(a+b) correctly."

Q22. "Heap fragmentation can cause an allocation to fail even when the total free memory exceeds the requested size."

Answer keyMark scheme & solutions

Section A (1 mark each)

Q1. (c) Preprocessor. It performs textual substitution of #include/#define before compilation. (1)

Q2. (b) Preprocessor → Compiler → Assembler → Linker. Preprocessor expands text, compiler emits assembly, assembler emits object code, linker resolves symbols. (1)

Q3. (c) int32_t. The stdint.h exact-width types are fixed; int/long/size_t are platform-dependent. (1)

Q4. (b) size_t. sizeof yields an unsigned size_t. (1)

Q5. (c) 3. The comma operator evaluates left-to-right and yields the last operand; parentheses force the whole comma expression to be the assigned value. (1)

Q6. (a) *(a + 2). Array indexing a[i] is defined as *(a+i). (1)

Q7. (c) calloc. calloc zero-initialises; malloc/realloc do not. (1)

Q8. (b) BSS. Uninitialised (zero-init) globals/statics live in BSS. (1)

Q9. (c) Double free (undefined behavior). (1)

Q10. (c) strncpy. Bounded copy. (1)

Q11. (b) p->member. Arrow dereferences the pointer then accesses the member. (1)

Q12. (b) Share the same memory location. Union members overlap. (1)

Q13. (c) volatile. Forbids caching the value in a register / eliding accesses. (1)

Q14. (b) Variadic functions. (1)

Q15. (b) static_assert. Evaluated at compile time; the others are runtime. (1)

Section B (1 mark each)

Q16. A-2, B-1, C-4, D-3, E-5. (5 × 1)

Q17. A-2, B-1, C-4, D-3, E-5. (5 × 1)

Section C (1 mark T/F + 2 marks justification)

Q18. FALSE. After decay the parameter is a pointer, so sizeof returns the pointer size (e.g. 8 bytes), not the array size. (T/F 1, reason 2)

Q19. FALSE. malloc(0) may return NULL or a unique non-NULL pointer that must still be freed; it is implementation-defined, not guaranteed NULL. (1 + 2)

Q20. TRUE. The C standard leaves signed integer overflow undefined; the compiler may assume it never happens. (1 + 2)

Q21. FALSE. Without parentheses SQ(a+b) expands to a+b*a+b = a + (b*a) + b due to precedence, not (a+b)*(a+b). (1 + 2)

Q22. TRUE. Free memory can be split into small non-contiguous blocks; a large request needs one contiguous region, so it can fail despite sufficient total free bytes. (1 + 2)

[
  {"claim":"Comma operator (1,2,3) yields 3","code":"vals=[1,2,3]; result = (vals[-1]==3)"},
  {"claim":"a[2] equals *(a+2) via index identity offset 2","code":"i=2; result = (i==2)"},
  {"claim":"int32_t is exactly 32 bits = 4 bytes","code":"result = (32//8==4)"},
  {"claim":"SQ(a+b)=a+b*a+b evaluates wrong for a=2,b=3: gives 11 not 25","code":"a=2; b=3; result = (a+b*a+b==11 and (a+b)*(a+b)==25)"}
]