4.2 - function in C.

4.2.1 - introduction to function, Definition and their Prototypes.


1. How are multidimensional arrays stored in memory?
Correct Answer: (A) Row-major order
2. What is the basic type of multiple dimensions array in C?
Correct Answer: (A) 2D array
3. Which of the following is true about a 2D array in C?
Correct Answer: (A) A distinct 1D array can represent each element
4. How many loops are needed to traverse a 2D array in C?
Correct Answer: (B) 2
5. Which matrix is called a rectangle matrix in a 2D array?
Correct Answer: (C) When rows and columns differ
6. What is the valid declaration for a 2D array?
Correct Answer: (C) int array[3][4];
7. What is the default return type of a C function?
Correct Answer: (A) int
8. Which of the following does not return any value?
Correct Answer: (A) Procedure
9. What is stored in a stack before calling a function?
Correct Answer: (B) Return address
10. Which of the following components form a function declaration?
Correct Answer: (A) Return type, name, parameters
11. What is the correct way to declare a 2D array without specifying the row size?
Correct Answer: (A) int array[][3];
12. Which of the following will update an element in a 2D array?
Correct Answer: (B) Scan and replace via loops
13. What is the advantage of using functions in C?
Correct Answer: (B) Efficient memory allocation
14. Which function prototype is valid in C?
Correct Answer: (D) All of the above
15. Which of the following allows functions to be reused in C?
Correct Answer: (C) Function calls
16. What is a key benefit of subprograms?
Correct Answer: (C) Reduces code repetition
17. Which of the following is incorrect about 2D array declaration?
Correct Answer: (B) int array[][] = {1,2,3};
18. Which operation is performed first when a function is called?
Correct Answer: (D) Return address stored
19. What is matrix multiplication in C?
Correct Answer: (B) Multiplying row of one matrix with column of another
20. How many loops are needed for matrix multiplication?
Correct Answer: (C) 3

4.2.2 - Function Arguments and function Call.


21. What is the default return type for a function in C if not specified?
Correct Answer: (A) int
22. Which of the following is the correct syntax for declaring a function with an argument but no return type?
Correct Answer: (B) void function(int x);
23. Which of the following is true about function arguments in C?
Correct Answer: (A) Arguments are optional
24. How are formal parameters handled in a C function?
Correct Answer: (A) They are created upon function entry and destroyed upon exit
25. What is the correct syntax for calling a function with an argument in C?
Correct Answer: (B) function(x);
26. Which of the following is a valid declaration for a function without arguments and without return value?
Correct Answer: (A) void function();
27. Which method passes a copy of an argument’s value to a function?
Correct Answer: (A) Call by value
28. Which method passes the address of the argument to a function?
Correct Answer: (B) Call by reference
29. What happens to the actual parameter when using Call by Value?
Correct Answer: (B) It does not get modified
30. Which of the following languages does not support Call by Reference by default?
Correct Answer: (D) Java
31. Which statement is true for Call by Reference in C?
Correct Answer: (B) The address of the actual argument is passed
32. In which memory area are the local variables, including function arguments, stored in C?
Correct Answer: (A) Stack
33. What is the output of this code snippet?
                void func(int *x) {
                    *x = *x + 10;
                }
                int main() {
                    int y = 5;
                    func(&y);
                    printf("%d", y);
                    return 0;
                }
                
Correct Answer: (C) 15
34. What will happen when you pass an array to a function in C?
Correct Answer: (B) Only the base address of the array is passed
35. Which of the following methods is used to modify a variable inside a function?
Correct Answer: (A) Pass by pointer
36. What is required to implement Call by Reference in C?
Correct Answer: (A) Pointers
37. Which of the following functions returns the maximum of two numbers?
Correct Answer: (C) int max(int a, int b);
38. What will be the output of the following code?
                void swap(int a, int b) {
                    int temp = a;
                    a = b;
                    b = temp;
                }
                int main() {
                    int x = 5, y = 10;
                    swap(x, y);
                    printf("%d %d", x, y);
                    return 0;
                }
                
Correct Answer: (B) 5 10
39. What is the primary difference between Call by Value and Call by Reference?
Correct Answer: (A) Call by Value passes a copy, while Call by Reference passes the address
40. What does the return statement do in a function?
Correct Answer: (A) Ends the function and returns a value to the caller

4.2.3 - types of Functions.


41. What are the two types of functions in C?
Correct Answer: (B) Library functions and User-defined functions
42. What is another name for a library function in C?
Correct Answer: (A) Built-in function
43. Which of the following is a library function?
Correct Answer: (B) pow();
44. What is the main advantage of using library functions in C?
Correct Answer: (B) They do not need to be declared and defined by the programmer
45. Which of the following is an example of a user-defined function?
Correct Answer: (C) myFunction()
46. What header file is required for the sqrt() function?
Correct Answer: (B) math.h
47. Which of the following is true for user-defined functions?
Correct Answer: (B) They must be declared and defined by the programmer
48. Which function calculates the power of a number in C?
Correct Answer: (A) pow()
49. In which of the following situations would you use a user-defined function?
Correct Answer: (D) Both B and C
50. What is the main disadvantage of user-defined functions compared to library functions?
Correct Answer: (C) They must be manually declared and defined by the programmer
51. What is the purpose of the #include statement in C?
  • (A) To define user-defined functions
  • (B) To include library functions related to mathematical operations
  • (C) To enable system functions
  • (D) To optimize program performance
Correct Answer: (B) To include library functions related to mathematical operations
52. Which of the following is true about the function pow() in C?
Correct Answer: (B) It takes two arguments and returns the result of the first raised to the power of the second
53. Which of the following statements is correct for user-defined functions?
Correct Answer: (C) They can return any data type
54. Which of the following is NOT a C library function?
Correct Answer: (C) calculateArea()
55. Which function is used to find the square root of a number in C?
Correct Answer: (B) sqrt()
56. Which of the following is a major benefit of user-defined functions?
Correct Answer: (A) They reduce the overall complexity of a program by allowing code reuse
57. What is the output of the following program?
                #include 
                int add(int a, int b) {
                    return a + b;
                }
                int main() {
                    int result = add(3, 4);
                    printf("%d", result);
                    return 0;
                }
                
Correct Answer: (A) 7
58. What must be true for a function to be considered user-defined?
Correct Answer: (A) It must be written by the programmer
59. In which scenario would you most likely use a user-defined function instead of a library function?
Correct Answer: (B) When implementing custom logic specific to the program
60. Which statement about C library functions is false?
Correct Answer: (B) They must be declared by the user before use

4.2.4 - Predefined vs User Defined functions.


61. Which of the following is true about predefined functions?
Correct Answer: (B) They are also known as library functions
62. Which of the following is NOT a predefined function in C?
Correct Answer: (C) main()
63. Which of the following is NOT a part of a user-defined function in C?
Correct Answer: (D) Header file
64. What is the role of the return type in a function definition?
Correct Answer: (B) It specifies the data type of the value the function returns
65. Which keyword is used as a return type if a function does not return any value?
Correct Answer: (B) void
66. What is a "parameter" in a function?
Correct Answer: (B) A placeholder that receives values when the function is called
67. What is the main advantage of using user-defined functions?
Correct Answer: (B) They make the code more readable and reusable
68. In a user-defined function, what does the function body contain?
Correct Answer: (C) The statements that describe what the function does
69. What is the correct order for defining a user-defined function in C?
Correct Answer: (D) Function declaration, function definition, function call
70. Which of the following is an advantage of using functions in C?
Correct Answer: (B) They improve code reusability and readability
71. In the function declaration int add(int, int);, what does int before add signify?
Correct Answer: (B) The return type of the function
72. What is the purpose of function declaration in C?
Correct Answer: (B) To specify the return type and parameters of the function to the compiler
73. Which of the following can be considered as an example of a user-defined function?
Correct Answer: (C) myFunction()
74. What must be included in the function definition?
Correct Answer: (B) The function body
75. Which of the following is NOT a characteristic of predefined functions?
Correct Answer: (B) They need to be manually defined by the programmer
76. What is the correct syntax for calling a user-defined function named calculate with two integer parameters a and b?
Correct Answer: (C) calculate(a, b);
77. Which statement about function parameters is true?
Correct Answer: (C) Parameters are optional; a function can have zero parameters
78. Which of the following is an advantage of using functions in a program?
Correct Answer: (B) Functions reduce the need for repeated code
79. Which of the following describes a function call in C?
Correct Answer: (C) It executes the function
80. What is the output of the following program?
                #include <stdio.h>
                void greet() {
                    printf("Hello, World!");
                }
                int main() {
                    greet();
                    return 0;
                }
                
Correct Answer: (C) Hello, World!

4.2.5 - User Defined functions.


81. Which of the following is true about user-defined functions?
Correct Answer: (B) They are modifiable to suit the programmer's needs
82. What is the benefit of user-defined functions?
Correct Answer: (C) They make code easier to understand and maintain
83. Which statement is true about user-defined functions?
Correct Answer: (C) User-defined functions can be reused in different programs
84. What is the output of the following program?
                #include <stdio.h>
                int sum(int a, int b) {
                    return a + b;
                }
                int main() {
                    int a = 10, b = 20;
                    int result = sum(a, b);
                    printf("Sum is: %d", result);
                    return 0;
                }
                
Correct Answer: (C) Sum is: 30
85. What is the function signature of the following user-defined function?
                int multiply(int a, int b) {
                    return a * b;
                }
                
Correct Answer: (B) int multiply(int, int)
86. In a user-defined function, what does the keyword return do?
Correct Answer: (B) It ends the function and sends a value back to the calling function
87. Which part of the user-defined function contains the actual code?
Correct Answer: (C) Function body
88. In the given code, what is int sum(int a, int b) called?
                int sum(int a, int b) {
                    return a + b;
                }
                
Correct Answer: (B) Function definition
89. Which of the following is an advantage of user-defined functions?
Correct Answer: (C) They can reduce code repetition
90. Which of the following correctly calls a function named add with arguments 5 and 10?
Correct Answer: (B) add(5, 10);
91. What is the output of the following code?
                #include <stdio.h>
                int multiply(int a, int b) {
                    return a * b;
                }
                int main() {
                    int x = 5, y = 6;
                    int result = multiply(x, y);
                    printf("Product is: %d", result);
                    return 0;
                }
                
Correct Answer: (B) Product is: 30
92. Which of the following is necessary when defining a user-defined function?
Correct Answer: (B) Defining the function before using it

4.2.6 - Recursion.


93. What is a user-defined function in C?
Correct Answer: (B) A function created by the programmer
94. Which of the following is a valid syntax for a function declaration in C?
Correct Answer: (A) int function()
95. What does the keyword 'void' indicate in a function declaration?
Correct Answer: (A) The function does not return any value
96. What is the default return type of a function in C?
Correct Answer: (A) int
97. What is recursion?
Correct Answer: (A) A function calling itself
98. Which of the following problems is best solved using recursion?
Correct Answer: (C) Calculating factorial
99. Which of the following is an example of a library function in C?
Correct Answer: (A) printf()
100. What is the output of the following code?
                int main() {
                    int x = 5;
                    printf("%d", fact(x));
                    return 0;
                }
                int fact(int n) {
                    if (n == 0)
                        return 1;
                    else
                        return n * fact(n - 1);
                }
                
Correct Answer: (A) 120
101. What is the key difference between call by value and call by reference?
Correct Answer: (A) Call by value passes a copy; call by reference passes the address
102. Which of the following function types does not return any value?
Correct Answer: (B) void
103. Which of the following is necessary for recursion to work properly?
Correct Answer: (B) Base case
104. Which of the following is true about recursion?
Correct Answer: (A) It requires more memory than an iterative solution
105. In C, which header file is required to use the library function pow()?
Correct Answer: (B) math.h
106. What is a function prototype in C?
Correct Answer: (A) A blueprint of a function
107. What is the termination condition in recursion called?
Correct Answer: (B) Base case
108. Which function call method allows the actual value to remain unchanged in the calling function?
Correct Answer: (A) Call by value
109. Which of the following library functions is used to find the length of a string?
Correct Answer: (A) strlen()
110. What happens if a recursive function lacks a base case?
Correct Answer: (A) Infinite recursion
111. Which type of variable stores the return address of the function call in recursion?
Correct Answer: (A) Stack
112. What is the primary advantage of recursion over iteration?
Correct Answer: (A) Simplicity and clarity in solving complex problems

4.2.7 - Function With Variable Number of Argument.


113. What is a function in C programming?
Correct Answer: (A) A block of code performing a specific task
114. Which of the following is a built-in function in C?
Correct Answer: (B) printf()
115. Which header file is required for using printf() and scanf() functions?
Correct Answer: (B) stdio.h
116. Which function call method allows the actual value to remain unchanged in the calling function?
Correct Answer: (A) Call by value
117. Which function in C is used to allocate memory dynamically?
Correct Answer: (A) malloc()
118. In C, which keyword is used to declare a function returning no value?
Correct Answer: (A) void
119. What is the default return type of a function in C if no return type is explicitly declared?
Correct Answer: (A) int
120. How many times can the main() function be called in a C program?
Correct Answer: (A) Only once
121. Which function is used to compare two strings in C?
Correct Answer: (A) strcmp()
122. Which of the following is the correct syntax to declare a function in C?
Correct Answer: (A) return_type function_name(parameter list);
123. Which of these is a valid function declaration in C?
Correct Answer: (D) int sum(int)
124. Which function would you use to find the length of a string in C?
Correct Answer: (A) strlen()
125. What is recursion in C?
Correct Answer: (A) A function calling itself
126. Which of the following functions can change the value of the actual parameters passed to it?
Correct Answer: (B) Call by reference
127. Which function is used to open a file in C?
Correct Answer: (A) fopen()
128. What is the return type of the malloc() function in C?
Correct Answer: (A) void*
129. Which C function is used to get the length of an array?
Correct Answer: (A) sizeof()
130. Which function is used to release the memory allocated dynamically in C?
Correct Answer: (A) free()
131. Which of the following is a function that does not return any value?
Correct Answer: (A) void function()
132. What does the return keyword do in a function?
Correct Answer: (A) It exits the function and returns a value