C Functions Quiz - MCQ Questions and Answers

1. What is a function in C?

a) A named block of code designed to perform a specific task
b) A variable used to store data
c) A header file
d) A type of operator

Answer:

a) A named block of code designed to perform a specific task

Explanation:

A function in C is a named block of code that performs a specific task and can be called from other parts of the program.

2. How is a function declared in C?

a) returnType functionName(parameters);
b) functionName(parameters) returnType;
c) returnType(parameters) functionName;
d) functionName: returnType(parameters);

Answer:

a) returnType functionName(parameters);

Explanation:

A function in C is declared with its return type, followed by the function name and a list of parameters enclosed in parentheses.

3. What is the purpose of a return statement in a C function?

a) To exit the program
b) To send a value back to the function caller
c) To print a value
d) To terminate a loop

Answer:

b) To send a value back to the function caller

Explanation:

The return statement in a C function is used to return a value from the function to the place where the function was called.

4. Which of the following is a correct way to define a function in C?

a) int sum(int a, int b) { return a + b; }
b) function sum(int a, int b) { return a + b; }
c) sum(int a, int b): int { return a + b; }
d) int sum(a, b) { return a + b; }

Answer:

a) int sum(int a, int b) { return a + b; }

Explanation:

The correct syntax to define a function in C includes the return type, function name, parameter list, and a block of code.

5. Which keyword is used to indicate that a function does not return a value in C?

a) null
b) void
c) none
d) empty

Answer:

b) void

Explanation:

The 'void' keyword is used to specify that a function does not return a value.

6. What is function prototyping in C?

a) Writing the body of a function
b) Creating a reference to a function
c) Declaring a function before its definition
d) Calling a function before it is declared

Answer:

c) Declaring a function before its definition

Explanation:

Function prototyping in C involves declaring a function before its actual definition. This allows the function to be called before it is defined.

7. What is the output of the following C code?

   int add(int a, int b) { return a + b; }
   int main() {
       printf("%d", add(3, 4));
       return 0;
   }
a) 3
b) 4
c) 7
d) Error

Answer:

c) 7

Explanation:

The function 'add' adds the two integers 3 and 4 and returns their sum, which is 7.

8. Which of the following is true about the main function in C?

a) It must always return an integer
b) It cannot have parameters
c) It can be omitted in a C program
d) It is called at the end of the program

Answer:

a) It must always return an integer

Explanation:

The main function in C is the entry point of a program and must return an integer value, usually 0 for successful execution.

9. How are arguments passed to functions in C?

a) By reference
b) By value
c) Either by reference or by value
d) By name

Answer:

b) By value

Explanation:

In C, arguments are passed to functions by value, meaning a copy of the argument is made and used inside the function.

10. Can a function return multiple values in C?

a) Yes, directly
b) No
c) Yes, by using arrays
d) Yes, by using pointers

Answer:

d) Yes, by using pointers

Explanation:

While a function in C cannot directly return multiple values, it can do so indirectly by using pointers or by returning a structure.

11. What are global variables in C?

a) Variables declared within a function
b) Variables that can be accessed by any function in the program
c) Constants
d) Variables that store functions

Answer:

b) Variables that can be accessed by any function in the program

Explanation:

Global variables are declared outside of all functions and can be accessed by any function within the program.

12. What is recursion in C?

a) A function that calls another function
b) A function that calls itself
c) Repeating a statement multiple times
d) Looping through an array

Answer:

b) A function that calls itself

Explanation:

Recursion in C is a technique where a function calls itself directly or indirectly, allowing for solutions to certain problems.

13. What is the scope of a local variable in C?

a) Throughout the program
b) Within the block where it is declared
c) Within all functions of the same file
d) Global

Answer:

b) Within the block where it is declared

Explanation:

The scope of a local variable in C is limited to the block or function where it is declared.

14. Which of the following is an example of function overloading in C?

a) int func(); float func();
b) int func(int a); int func(float a);
c) int func(int a); int func(int a, int b);
d) Function overloading is not possible in C

Answer:

d) Function overloading is not possible in C

Explanation:

Function overloading, which is defining multiple functions with the same name but different parameters, is not possible in C.

15. How can you dynamically allocate memory for a function's local variables in C?

a) Using the malloc() function
b) By declaring them static
c) They are automatically dynamically allocated
d) Using the alloc() function

Answer:

a) Using the malloc() function

Explanation:

Dynamic memory allocation in C can be done using functions like malloc(), which is useful for allocating memory during runtime.

16. What is a static function in C?

a) A function that retains its value between calls
b) A function that can be called only once
c) A function whose scope is limited to the file in which it is declared
d) A function that does not change

Answer:

c) A function whose scope is limited to the file in which it is declared

Explanation:

A static function in C is one whose visibility is limited to the file in which it is declared, making it inaccessible from other files.

17. What is the difference between formal and actual parameters in C?

a) Formal parameters are used in function definition, actual parameters in function call
b) There is no difference
c) Actual parameters are used in function definition, formal parameters in function call
d) Formal parameters are variables, actual parameters are values

Answer:

a) Formal parameters are used in function definition, actual parameters in function call

Explanation:

Formal parameters are the variables declared in the function definition, whereas actual parameters are the values passed to the function during a function call.

18. What is a library function in C?

a) A function that must be included in all C programs
b) A function that you create and add to a library
c) A function provided by C's standard libraries
d) A function used to manage libraries

Answer:

c) A function provided by C's standard libraries

Explanation:

Library functions are those provided by C's standard libraries, such as stdio.h, stdlib.h, math.h, etc.

19. Can a C function return another function?

a) Yes
b) No
c) Only if it is a pointer to a function
d) Only if it is a static function

Answer:

c) Only if it is a pointer to a function

Explanation:

In C, a function cannot return another function directly, but it can return a pointer to a function.

20. What is the output of the following C code?

void update(int x) { x = x + 5; }
   int main() {
       int y = 5;
       update(y);
       printf("%d", y);
       return 0;
   }
a) 5
b) 10
c) 0
d) Error

Answer:

a) 5

Explanation:

Since arguments in C are passed by value, the update function modifies a copy of y, not y itself. Therefore, the value of y remains 5.

Comments