Subprogram Control in C Programming MCQ Questions and Answers
1. Which of the following correctly declares a function that takes an int and returns a pointer to char?
A) char *f(int);
B) char (*f)(int);
C) char *f(int*);
D) char f*(int);
Answer: A
2. In C, a function prototype must appear before its use to:
A) allocate memory for the function
B) inform the compiler of the function’s return type and parameter types
C) cause the linker to inline the function
D) convert the function to an inline function
Answer: B
3. Consider int add(int a, int b); — what is add here?
A) A function definition
B) A function declaration (prototype)
C) A function call
D) A macro
Answer: B
4. Which storage class gives a variable internal linkage and preserves its value across function calls?
A) auto
B) register
C) static
D) extern
Answer: C
5. Which statement about parameter passing in C is correct?
A) C supports call-by-reference natively.
B) C only supports call-by-value; call-by-reference is simulated using pointers.
C) C supports call-by-name.
D) C supports both call-by-value and call-by-reference natively.
Answer: B
6. What is the output of this code?
void f(int x) { x = 10; }
int main() { int a = 5; f(a); printf(“%d”, a); }
A) 10
B) 5
C) Undefined behavior
D) Compiler error
Answer: B
7. Which header declares the setjmp and longjmp functions?
A) <stdlib.h>
B) <stdio.h>
C) <setjmp.h>
D) <signal.h>
Answer: C
8. Which of the following is a correct way to define a function pointer that points to a function returning int and taking two int parameters?
A) int (*fp)(int,int);
B) int *fp(int,int);
C) int fp(*)(int,int);
D) int (*fp)(int,int)();
Answer: A
9. What does the inline keyword suggest to the C compiler?
A) Force the function to be in a separate translation unit
B) Request the compiler to expand the function inline where it is called
C) Make the function recursive
D) Disable optimization for the function
Answer: B
10. Which is true about recursive functions?
A) They cannot be static.
B) They always cause segmentation fault.
C) Each recursive call uses a new stack frame.
D) They never use additional memory.
Answer: C
11. What is the correct way to declare a function that accepts a variable number of arguments?
A) void f(…);
B) void f(int, …);
C) void f(va_list);
D) void f(varargs);
Answer: B
12. Which of these is used to retrieve arguments in a variadic function?
A) va_start, va_arg, va_end
B) arg_start, arg_get, arg_end
C) var_start, var_arg, var_end
D) getargs, nextarg, endargs
Answer: A
13. Where does a function’s local (automatic) variables typically reside at runtime?
A) Heap
B) Data segment
C) Stack
D) Code segment
Answer: C
14. What is the effect of declaring a variable as register?
A) It forces the compiler to store the variable in CPU register.
B) It hints that the variable may be heavily used and can be stored in CPU register.
C) It makes the variable global.
D) It makes the variable persistent across function calls.
Answer: B
15. Which of the following is NOT a valid reason to use function pointers?
A) Implementing callbacks
B) Dynamic dispatch of functions
C) Passing functions as arguments
D) Increasing compile-time type checking for overloaded functions (C has no overloading)
Answer: D
16. In C, extern applied to a function declaration means:
A) The function is inline.
B) The function has external linkage (visible across translation units).
C) The function is private to the translation unit.
D) The function must be defined as static.
Answer: B
17. What is the result of the following code?
int f(void) { return 3; }
int main() { int (*p)() = f; printf(“%d”, p()); }
A) 3
B) Compiler error due to missing prototype parameters
C) Undefined behavior
D) Linker error
Answer: A
18. Which of the following is true about forward declaration of functions?
A) It is optional in all cases.
B) It allows functions to be used before their full definition.
C) It is required only for static functions.
D) It allocates memory for the function.
Answer: B
19. Which C standard introduced function prototypes with type checking?
A) K&R C (pre-ANSI)
B) ANSI C (C89/C90)
C) C99
D) C11
Answer: B
20. Which of the following correctly shows passing an array to a function?
A) void f(int a[])
B) void f(int a[10])
C) void f(int *a)
D) All of the above are valid parameter declarations for an integer array parameter.
Answer: D
21. Which is the correct way to define a function returning a pointer to a function that returns int?
A) int (*f(void))(void);
B) int *f()(void);
C) int (*f())(void);
D) int f(void)(*);
Answer: A
22. What happens if a non-void function reaches the closing brace without a return statement?
A) The compiler will always generate an error.
B) Behavior is undefined if the returned value is used.
C) It returns zero by default.
D) It returns the last value stored in a register.
Answer: B
23. Consider:
int x = 5;
void f(void) { x = 10; }
Function f updates x. What is x here?
A) Local variable in f
B) Global variable with external linkage
C) Static local variable
D) Automatic variable of main
Answer: B
24. Which keyword restricts a pointer to be used only to access the object it originally pointed to (used for optimization)?
A) const
B) volatile
C) restrict
D) static
Answer: C
25. Which is true about volatile variables?
A) The compiler may cache volatile variables in registers and optimize accesses away.
B) Volatile indicates a variable might be modified outside the current program flow and prevents certain optimizations.
C) Volatile makes the variable thread-safe automatically.
D) Volatile implies static storage duration.
Answer: B
26. Which of these signatures is valid for main?
A) int main(void)
B) int main(int argc, char *argv[])
C) int main(int argc, char **argv)
D) All of the above
Answer: D
27. What is the purpose of extern “C” in C++ when using C functions?
A) Enforce C++ name mangling for interoperability
B) Indicate that the function uses C linkage (disable C++ name mangling)
C) Make the function inline in C++
D) Convert the C function into a method of a class
Answer: B
28. What is the output of:
void g(int *p) { *p = *p + 1; }
int main() { int a = 1; g(&a); printf(“%d”, a); }
A) 1
B) 2
C) 0
D) Undefined behavior
Answer: B
29. Which statement about function overloading in C is correct?
A) Supported via #define macros.
B) Supported natively by the language.
C) Not supported in C.
D) Supported only for inline functions.
Answer: C
30. Consider static int counter(void) { static int c = 0; return ++c; } called three times. What values are returned?
A) 1, 1, 1
B) 1, 2, 3
C) 0, 0, 0
D) Undefined behavior
Answer: B
31. Which construct allows you to jump out of nested function calls back to a saved state?
A) goto
B) setjmp and longjmp
C) break
D) return
Answer: B
32. Which of the following is a potential danger of using longjmp?
A) It can bypass automatic object destruction in C++ (if used in mixed code).
B) It increases stack memory.
C) It always results in memory leaks in C.
D) It forces variables to be global.
Answer: A
33. For which reason would you choose to pass a large struct to a function via pointer rather than by value?
A) To copy the struct (faster).
B) To avoid copying overhead and reduce stack usage.
C) Because pointers are type safe and structs are not.
D) To ensure the function cannot modify the struct contents.
Answer: B
34. What is the purpose of typedef in relation to function pointers?
A) typedef cannot be used with function pointers.
B) To create an alias for complex function pointer types for readability.
C) To allocate memory for a function pointer.
D) To convert a function pointer into a function.
Answer: B
35. Which of the following is true about extern variables with no initializer in a header file?
A) They allocate storage in every file that includes the header.
B) They declare the variable without defining it; one definition should exist elsewhere.
C) They cause linker errors by default.
D) They make the variable static.
Answer: B
36. What is the output of:
void f(int a[]) { a[0] = 9; }
int main() { int a[1] = {1}; f(a); printf(“%d”, a[0]); }
A) 1
B) 9
C) Undefined behavior
D) Compiler error
Answer: B
37. Which of the following describes the lifetime of an automatic local variable?
A) Whole program execution
B) Until program exit
C) Only during execution of its block/function
D) Until garbage collector frees it
Answer: C
38. Which specifier ensures that a function has internal linkage?
A) extern
B) static
C) register
D) inline
Answer: B
39. Which of these statements is correct about const parameters in function prototypes?
A) const in a parameter declaration is always part of the function signature.
B) Top-level const in parameters of pointer types affects the pointer, not the pointed-to type.
C) The const qualifier on value parameters has no effect on callers, only on the implementation.
D) const parameters cannot be used in prototypes.
Answer: C
40. What will this function call print?
int f(int x) { if (x==0) return 0; return x + f(x-1); }
printf(“%d”, f(3));
A) 3
B) 6
C) 0
D) Undefined behavior
Answer: B
41. Which is the correct way to declare a function that does not take any parameters (explicitly) in ANSI C?
A) void f()
B) void f(void)
C) int f()
D) int f(void, …)
Answer: B
42. What does the register storage class prohibit?
A) Taking the address of the variable with & in some compilers.
B) Using the variable inside loops.
C) Using the variable as a function parameter.
D) Initializing the variable.
Answer: A
43. Which is true about function declarations in different translation units?
A) Declarations must be identical in type and linkage to avoid undefined behavior.
B) They can differ freely; linker will unify them.
C) Only names must match; parameter types may differ.
D) Return types can differ if parameter types match.
Answer: A
44. Which of the following will cause undefined behavior?
A) Returning address of a local (automatic) variable from a function.
B) Returning a pointer to a static local variable.
C) Returning a pointer to dynamically allocated memory.
D) Returning an integer value.
Answer: A
45. What is a trampoline when discussing function calls?
A) A compiler bug
B) A small piece of code used to transfer control (e.g., for nested functions in some compilers)
C) A function pointer type
D) The calling convention name
Answer: B
46. Which calling convention detail is typically handled by the compiler/ABI, not the C language specification?
A) Number and types of function parameters
B) How arguments are passed (registers vs stack) and who cleans the stack
C) Function names
D) Whether recursion is allowed
Answer: B
47. Which function attribute (GCC) can be used to check printf-style formatting in custom functions?
A) __attribute__((noreturn))
B) __attribute__((format(printf, x, y)))
C) __attribute__((inline))
D) __attribute__((pure))
Answer: B
48. What will be the output?
int sum(int n) { if (n==1) return 1; return n + sum(n-1); }
printf(“%d”, sum(4));
A) 4
B) 10
C) 0
D) Stack overflow
Answer: B
49. Which of the following is a valid way to call a function via a pointer?
A) (*fp)(args);
B) fp(args);
C) Both A and B
D) Neither A nor B
Answer: C
50. What is the default linkage of a non-static function in C?
A) No linkage
B) Internal linkage
C) External linkage
D) Linkage changes based on compiler flags
Answer: C
51. Which statement correctly describes tail recursion?
A) A recursive call that is the last operation in a function and can be optimized to iterative form by the compiler.
B) A recursion that always returns zero.
C) Recurrence with two recursive calls.
D) A recursion that uses global variables only.
Answer: A
52. Which header file is required for qsort and bsearch functions that take function pointers?
A) <stdlib.h>
B) <string.h>
C) <stddef.h>
D) <stdio.h>
Answer: A
53. Which of the following declarations defines a pointer to a function that takes no arguments and returns void?
A) void (*p)(void);
B) void *p(void);
C) void p(*)(void);
D) (*void p)(void);
Answer: A
54. What happens if two functions in different files have the same static function name?
A) Linker error due to duplicate symbol.
B) They refer to the same function.
C) Each has internal linkage; no conflict — they are distinct.
D) Undefined behavior at runtime.
Answer: C
55. Which of the following is permitted inside a function definition?
A) Defining another non-nested function (standard C disallows nested functions).
B) Declaring local variables.
C) Defining a label for goto.
D) B and C only.
Answer: D
56. Which is true about function prototypes with const parameters where the parameter is passed by value?
A) const affects the caller semantics.
B) const prevents the function from modifying its local copy, but callers are unaffected.
C) const makes the parameter have external linkage.
D) const on value parameter changes calling convention.
Answer: B
57. Which mechanism is commonly used in C to simulate method dispatch (like OOP) for different concrete types?
A) Function pointers in structs (vtable-like structures)
B) Macros only
C) Inline functions exclusively
D) #include trickery
Answer: A
58. Which keyword would you use to give a function internal linkage and keep its symbol hidden from other translation units?
A) extern
B) hidden
C) static
D) private
Answer: C
59. Which of the following is true about the main function’s return value?
A) It has no effect on the program exit status.
B) Returning 0 indicates successful termination to the host environment.
C) Returning 1 always indicates success.
D) main must always return void.
Answer: B
60. What will sizeof a function pointer yield?
A) Size of the function’s return type
B) Size of a pointer on the platform (implementation-defined)
C) Always 4
D) Always 8
Answer: B
61. Which of these is a valid reason to declare a function static in a .c file?
A) To allow other translation units to call it.
B) To restrict it to the file and avoid symbol collisions.
C) To automatically inline it.
D) To make it variadic.
Answer: B
62. Which of following is true for extern and static when applied to variables at file scope?
A) extern makes a variable visible only in the file.
B) static gives a variable internal linkage; extern gives it external linkage.
C) Both are equivalent.
D) extern makes the variable stored in stack.
Answer: B
63. When working with function pointers, which cast is safest when interfacing with void *?
A) Cast the function pointer to void* directly and back — this is undefined by the standard; avoid it.
B) Standard guarantees casting between void* and function pointers is safe.
C) Use integer types to store function pointers.
D) Use memcpy to move pointer bits — also undefined.
Answer: A
64. What is the typical return type of comparison functions used with qsort?
A) void
B) int (negative, zero, positive semantics)
C) char
D) bool
Answer: B
65. Which is a valid reason to use extern in header files?
A) To define global variables in headers.
B) To declare global variables so that exactly one .c file defines them.
C) To make local variables global.
D) To inline the function.
Answer: B
66. What does this function do?
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
A) Swaps the addresses stored in a and b.
B) Swaps the values pointed to by a and b.
C) Does nothing.
D) Causes undefined behavior.
Answer: B
67. Which of the following is true about nested functions in GNU C?
A) They are part of standard C and portable.
B) GNU C allows nested functions as a compiler extension; not portable to standard C.
C) Nested functions are undefined in GNU C.
D) They are implemented via macros in GNU C.
Answer: B
68. What is the behavior of calling a function through an uninitialized function pointer?
A) It calls a default function.
B) Undefined behavior.
C) It returns zero.
D) It prints an error at compile time.
Answer: B
69. Which of the following ensures a function argument cannot be modified inside the function (for pointer parameter)?
A) const int *p
B) int * const p
C) const int * const p
D) A and C both ensure pointed-to value cannot be modified.
Answer: D
70. Which function attribute or qualifier can help the compiler optimize functions that do not examine global memory and have no side effects besides return value based on parameters?
A) volatile
B) pure or const attributes (compiler-specific)
C) noreturn
D) register
Answer: B
71. What will be printed by:
int f(int n) { if (n==0) return 1; return n * f(n-1); }
printf(“%d”, f(3));
A) 6
B) 3
C) 1
D) Undefined behavior
Answer: A
72. Which keyword prevents the compiler from optimizing access to a variable because the variable may change at any time (e.g., memory-mapped I/O)?
A) const
B) volatile
C) register
D) restrict
Answer: B
73. For void (*vp)(void); which of the following calls is valid?
A) vp;
B) vp();
C) (*vp);
D) &vp();
Answer: B
74. What happens when you declare int foo(); (empty parentheses) in old-style C use?
A) It declares a function with unspecified parameters (K&R style) — less type checking.
B) It declares a function that takes no arguments.
C) It declares a function pointer.
D) It is a syntax error in all standards.
Answer: A
75. Which of the following is true about the order of evaluation of function arguments in C?
A) The order is left-to-right guaranteed.
B) The order is right-to-left guaranteed.
C) The order is unspecified (implementation-defined) and should not be relied upon.
D) Arguments are evaluated in parallel.
Answer: C
76. Where should you put the prototypes of functions that are shared between multiple .c files?
A) In one .c file only.
B) In a header file included by the .c files.
C) In a .txt file.
D) Inlining in each .c file separately is required.
Answer: B
77. Which of the following is correct syntax for a function returning an array of ints?
A) int f[]();
B) Not possible — functions cannot return arrays directly; use pointers or structs.
C) int[] f(void);
D) int *f[]();
Answer: B
78. Which is a correct approach for making a function visible only within a single translation unit?
A) Declare it extern in the header.
B) Define it in the header.
C) Mark it static in the .c file.
D) Use volatile keyword.
Answer: C
79. What does this call accomplish?
qsort(arr, n, sizeof(*arr), cmp);
A) Sorts arr using n as a pivot.
B) Performs sorting using cmp as comparator pointer function.
C) Shuffles arr.
D) Copies memory from arr to cmp.
Answer: B
80. Which of the following is true about parameter names in a function declaration (prototype) vs definition?
A) Parameter names are required in prototypes.
B) Parameter names in prototypes are optional; only types are necessary.
C) Names must be identical between prototype and definition.
D) Names in prototypes determine link-time symbol names.
Answer: B
81. What does noreturn attribute indicate about a function?
A) The function will not return to its caller (e.g., calls exit or infinite loop).
B) The function cannot be called.
C) It must be inlined.
D) The function returns void.
Answer: A
82. Which of the following best describes undefined behavior (UB) when using functions incorrectly?
A) Compiler will always detect and report it.
B) Results are unpredictable — program may crash, corrupt data, or appear to work.
C) UB results in deterministic errors across platforms.
D) UB means the compiler will correct your code automatically.
Answer: B
83. Which of these is a safe practice when returning dynamically allocated memory from a function?
A) Return a pointer to a malloced block and document that the caller must free it.
B) Return address of a local array.
C) Return pointer to static local variable when unique copy per call is required.
D) None of the above.
Answer: A
84. Which statement is true about calling convention differences across platforms?
A) All platforms use the same calling convention mandated by C standard.
B) Calling conventions vary with ABIs; code relying on specific register usage may be non-portable.
C) The C standard fixes which registers are used for arguments.
D) Calling conventions only matter for high-level languages, not C.
Answer: B
85. What is required to convert a function name to a function pointer?
A) Use & operator: &func or simply func — both are acceptable.
B) Use *func.
C) You cannot get pointer to a function.
D) Use function_ptr(func).
Answer: A
86. If a function is declared static inline in a header included by multiple .c files, what happens?
A) Each translation unit may define its own internal (static) inline copy; no linker conflict.
B) Linker error due to multiple definitions.
C) The function becomes globally visible.
D) The header becomes invalid.
Answer: A
87. What does the expression (&f)==(&g) compare when f and g are function names?
A) Always true if f and g are different functions.
B) Compares the addresses of functions f and g, valid but implementation-defined behavior for comparison of function pointers.
C) Causes compile error.
D) Undefined to compare function pointers.
Answer: B
88. Which of the following can be passed to qsort as a comparator function?
A) A function with signature int cmp(const void *, const void *)
B) A function with signature void cmp(const void *, const void *)
C) A function pointer to main
D) Any function taking two ints
Answer: A
89. Which of these operations is NOT valid on function pointers?
A) Assignment from another function pointer of same type.
B) Comparison for equality with another pointer of same type.
C) Adding an integer to increment pointer by functions.
D) Indirect call using (*fp)() or fp().
Answer: C
90. Which of the following is true for functions with extern declaration but no definition anywhere?
A) The linker will provide a default implementation.
B) Linker error: undefined reference at link time.
C) The program will run but crash at first call.
D) Compiler will inline a stub.
Answer: B
91. Which of the following is an effect of optimizing compilers on function calls?
A) They may inline small functions to avoid call overhead.
B) They can change function signatures silently.
C) They always convert recursion to loops.
D) They remove function prototypes.
Answer: A
92. Which use of memcpy is safe for moving structs but not for function pointers?
A) Using memcpy to copy a struct containing function pointers is portable for all platforms.
B) Copying function pointers as part of structs via memcpy is typically fine but casting between data pointers and function pointers is not strictly portable.
C) memcpy always fails for function pointers.
D) memcpy converts function pointers to integers.
Answer: B
93. What is the main difference between call by value and call by reference?
A) Call by value passes variable address; call by reference passes copy.
B) Call by value passes copy of the value; call by reference passes an alias/address so callee can modify caller’s storage.
C) They are identical in C.
D) Call by reference is the only method in C.
Answer: B
94. Which of the following is a correct prototype for a function pointer type alias using typedef?
A) typedef int (*cmp_t)(const void *, const void *);
B) typedef (*cmp_t) int(const void *, const void *);
C) typedef int cmp_t(const void *, const void *);
D) typedef int cmp_t(*)(const void *, const void *);
Answer: A
95. Which of the following statements about return in main is true in hosted environments?
A) return 0; indicates successful termination; falling off end of main also returns 0 in C99 and later.
B) return 0; always causes a runtime error.
C) Falling off the end of main returns 1.
D) main must call exit explicitly.
Answer: A
96. What is the behavior of inline functions with external linkage when compiled with optimization off?
A) They are always inlined.
B) Compiler may or may not inline; an externally visible out-of-line copy is often emitted.
C) They are forbidden.
D) They become static.
Answer: B
97. Which of the following is legal C code for invoking a function pointer stored in array fp where fp is void (*fp[5])(int)?
A) fp;
B) (*fp[2])(10);
C) Both A and B
D) fp(2)(10);
Answer: C
98. Which of the following best describes the restrict qualifier?
A) Means pointer cannot be null.
B) Indicates that for the lifetime of the pointer, only it or equivalents will be used to access the object — enabling optimization.
C) Ensures pointer is volatile.
D) Makes pointer const.
Answer: B
99. Which statement is true about malloc and returning pointer from function?
A) Returning pointer to malloced memory is safe; caller must free it.
B) Returning pointer to malloced memory is illegal.
C) malloced memory is freed automatically on return.
D) malloc must only be used in main.
Answer: A
100. Which of the following is a valid technique to allow different modules to use the same function name with different implementations without symbol collision?
A) Making each function static in its module (internal linkage).
B) Using unique prefixes or namespaces in names.
C) Using function pointer tables per module (dynamic dispatch).
D) Any of the above depending on desired behavior.
Answer: D
