Home Subjects C Programming Functions

C Programming
Functions

C language from basics to advanced placement prep

47 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 47
Topics in C Programming
Q.11 Medium Functions
What will be the output of this code?
int counter = 0;
void increment() { static int count = 0; count++; counter++; }
main() { increment(); increment(); printf("%d %d", count, counter); }
A 0 2
B 2 2
C Compilation error - 'count' undeclared
D 2 0
Correct Answer:  C. Compilation error - 'count' undeclared
EXPLANATION

The static variable 'count' is local to increment() and inaccessible from main(). Trying to print 'count' causes compilation error: undefined identifier.

Test
Q.12 Medium Functions
Which of the following correctly declares a variadic function?
A int func(int x, ...);
B int func(...);
C int func(int ...x);
D int func(int x, int ...);
Correct Answer:  A. int func(int x, ...);
EXPLANATION

Variadic functions use ... syntax, and at least one named parameter must precede it. Option A is correct; Option B violates requirement of at least one named parameter.

Test
Q.13 Medium Functions
Which approach correctly implements a function returning multiple values?
A Using a struct containing multiple fields
B Using pointers as output parameters
C Using global variables modified by function
D A and B are both correct approaches
Correct Answer:  D. A and B are both correct approaches
EXPLANATION

Both approaches are valid in C: returning a struct with multiple fields or using pointers as output parameters. Using globals is possible but not recommended.

Test
Q.14 Medium Functions
Consider the function: void func(int *arr, int size). Which statement is correct?
A arr is a local copy of the original array
B arr receives the address of the first element, allowing modification of original
C The function cannot access the original array
D arr and size must be passed by pointers
Correct Answer:  B. arr receives the address of the first element, allowing modification of original
EXPLANATION

Array names decay to pointers in function calls. The parameter arr receives the address of the first element, so modifications affect the original array.

Test
Q.15 Medium Functions
What is the primary advantage of using inline functions?
A They save memory by reducing code duplication
B They eliminate function call overhead by substituting code at call site
C They enable recursion without stack overflow
D They improve code readability
Correct Answer:  B. They eliminate function call overhead by substituting code at call site
EXPLANATION

Inline functions request the compiler to replace function calls with actual function code, eliminating call overhead. However, compiler may ignore inline suggestion.

Test
Q.16 Medium Functions
Which of the following is true about function pointers in C?
A They cannot point to built-in functions
B They enable dynamic function calls and callbacks
C They require special declaration syntax different from regular pointers
D They cannot be stored in arrays
Correct Answer:  B. They enable dynamic function calls and callbacks
EXPLANATION

Function pointers are powerful features enabling dynamic function selection at runtime, crucial for callbacks, function arrays, and design patterns like strategy pattern.

Test
Q.17 Medium Functions
Analyze the following pointer to function declaration: int (*ptr)(int, int);
A ptr is a function that returns int pointer
B ptr is a pointer to a function taking two int parameters and returning int
C ptr is an array of function pointers
D ptr is a function returning array of ints
Correct Answer:  B. ptr is a pointer to a function taking two int parameters and returning int
EXPLANATION

The parentheses around (*ptr) indicate ptr is a pointer. The remaining (int, int) shows it points to a function taking two int arguments and returning int.

Test
Q.18 Medium Functions
What is the difference between call by value and call by reference in C?
A Call by reference creates a copy; call by value passes address
B Call by value passes copy; call by reference passes address via pointers
C No difference; C uses only call by value
D Call by reference is faster
Correct Answer:  B. Call by value passes copy; call by reference passes address via pointers
EXPLANATION

C supports call by value (default) and call by reference (using pointers). Call by value passes a copy; call by reference passes the address allowing modifications to original.

Test
Q.19 Medium Functions
Which calling convention passes parameters through the stack from right to left?
A cdecl
B stdcall
C fastcall
D pascal
Correct Answer:  A. cdecl
EXPLANATION

The cdecl (C declaration) calling convention is the standard in C, pushing parameters onto the stack from right to left, with the caller responsible for stack cleanup.

Test
Q.20 Medium Functions
What is the scope of a static variable declared inside a function?
A Local to the function, but retains value between calls
B Global scope with external linkage
C Only valid during function execution
D Accessible to all functions in the program
Correct Answer:  A. Local to the function, but retains value between calls
EXPLANATION

Static variables inside a function have local scope but static storage duration, meaning they retain their value between function calls and are initialized only once.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips