C programming –unit 3
1. What is a library function in programming?
a) A pre-written code
b) A user-defined function
c) A built-in function
d) A custom function
Answer: c) A built-in function
2. Which library function is used to calculate the square root of a number?
a) sqrt()
b) pow()
c) round()
d) ceil()
Answer: a) sqrt()
3. What is the purpose of the #include directive in C programming?
a) To include a header file
b) To define a macro
c) To declare a variable
d) To call a function
Answer: a) To include a header file
4. Which header file is used for input-output operations in C?
a) stdio.h
b) math.h
c) string.h
d) conio.h
Answer: a) stdio.h
5. What is the difference between the printf() and scanf() functions?
a) printf() is used for input, scanf() is used for output
b) printf() is used for output, scanf() is used for input
c) printf() is used for strings, scanf() is used for numbers
d) printf() is used for numbers, scanf() is used for strings
Answer: b) printf() is used for output, scanf() is used for input
6. Which library function is used to convert a string to uppercase?
a) toupper()
b) tolower()
c) strcat()
d) strcpy()
Answer: a) toupper()
7. What is the purpose of the exit() function in C programming?
a) To terminate the program
b) To return a value from a function
c) To declare a variable
d) To call a function
Answer: a) To terminate the program
8. Which library function is used to generate random numbers?
a) rand()
b) srand()
c) time()
d) random()
Answer: a) rand()
9. What is the difference between the abs() and fabs() functions?
a) abs() is used for integers, fabs() is used for floating-point numbers
b) abs() is used for floating-point numbers, fabs() is used for integers
c) abs() is used for strings, fabs() is used for numbers
d) abs() is used for numbers, fabs() is used for strings
Answer: a) abs() is used for integers, fabs() is used for floating-point numbers
10. Which library function is used to calculate the length of a string?
a) strlen()
b) strcpy()
c) strcat()
d) strcmp()
Answer: a) strlen()
11. What is the purpose of the malloc() function in C programming?
a) To allocate memory dynamically
b) To deallocate memory dynamically
c) To declare a variable
d) To call a function
Answer: a) To allocate memory dynamically
12. Which library function is used to search for a substring in a string?
a) strstr()
b) strchr()
c) strrchr()
d) strcmp()
Answer: a) strstr()
13. What is the difference between the printf() and fprintf() functions?
a) printf() is used for output to the screen, fprintf() is used for output to a file
b) printf() is used for output to a file, fprintf() is used for output to the screen
c) printf() is used for strings, fprintf() is used for numbers
d) printf() is used for numbers, fprintf() is used for strings
Answer: a) printf() is used for output to the screen, fprintf() is used for output to a file
14. Which library function is used to convert a string to lowercase?
a) toupper()
b) tolower()
c) strcat()
d) strcpy()
Answer: b) tolower()
15. What is the purpose of the calloc() function in C programming?
a) To allocate memory dynamically
b) To deallocate memory dynamically
c) To declare a variable
d) To call a function
Answer: a) To allocate memory dynamically
16. Which library function is used to compare two strings?
a) strcmp()
b) strcpy()
c) strcat()
d) strlen()
Answer: a) strcmp()
17. What is a user-defined function in C programming?
a) A built-in function
b) A pre-written code
c) A function defined by the user
d) A library function
Answer: c) A function defined by the user
18. Which of the following is a benefit of using user-defined functions?
a) Code duplication
b) Code reusability
c) Increased complexity
d) Decreased readability
Answer: b) Code reusability
19. What is the purpose of the function prototype in C programming?
a) To define the function
b) To declare the function
c) To call the function
d) To return a value from the function
Answer: b) To declare the function
20. Which of the following is a characteristic of a well-designed user-defined function?
a) Performs multiple tasks
b) Has a single entry point
c) Has multiple exit points
d) Uses global variables
Answer: b) Has a single entry point
21. How do you call a user-defined function in C programming?
a) By using the function name followed by parentheses
b) By using the function name followed by a semicolon
c) By using the function name followed by a period
d) By using the function name followed by a comma
Answer: a) By using the function name followed by parentheses
22. What is the difference between a function definition and a function declaration?
a) A function definition is the actual code, while a function declaration is the prototype
b) A function definition is the prototype, while a function declaration is the actual code
c) A function definition is used for built-in functions, while a function declaration is used for user-defined functions
d) A function definition is used for user-defined functions, while a function declaration is used for built-in functions
Answer: a) A function definition is the actual code, while a function declaration is the prototype
23. What is a function declaration in C programming?
a) Defining the function
b) Declaring the function
c) Calling the function
d) Returning a value from the function
Answer: b) Declaring the function
24. What is the purpose of the function declaration?
a) To define the function
b) To declare the function
c) To specify the function's return type
d) To specify the function's parameters
Answer: b) To declare the function
25. What is the syntax for a function declaration in C?
a) return_type function_name (parameter_list);
b) function_name (parameter_list);
c) return_type function_name ();
d) function_name ();
Answer: a) return_type function_name (parameter_list);
26. What is the return type in a function declaration?
a) The data type of the function's return value
b) The data type of the function's parameters
c) The name of the function
d) The number of parameters
Answer: a) The data type of the function's return value
27. What are the parameters in a function declaration?
a) The variables passed to the function
b) The variables returned from the function
c) The variables declared within the function
d) The variables defined outside the function
Answer: a) The variables passed to the function
28. Can you have multiple function declarations with the same name?
a) Yes, if they have different parameters
b) Yes, if they have different return types
c) No, function names must be unique
d) No, function declarations are not allowed
Answer: c) No, function names must be unique
29. What happens if you declare a function with the same name as a built-in function?
a) The built-in function is overridden
b) The user-defined function is ignored
c) A compilation error occurs
d) A runtime error occur
Answer: c) A compilation error occurs
30. Can you declare a function within another function?
a) Yes, nested functions are allowed
b) No, functions cannot be nested
c) Yes, but only if the inner function is static
d) No, but you can declare a function pointer within a function
Answer: b) No, functions cannot be nested
31. How do you declare a function that takes no arguments?
a) function_name ();
b) function_name (void);
c) function_name (NULL);
d) function_name (0);
Answer: b) function_name (void);
32. Can you declare a function that returns no value?
a) Yes, use the void return type
b) No, all functions must return a value
c) Yes, but only if the function takes no arguments
d) No, but you can return a null value
Answer: a) Yes, use the void return type
33. What is a prototype declaration in C programming?
a) A function definition
b) A function declaration
c) A variable declaration
d) A data type declaration
Answer: b) A function declaration
34. What is the purpose of a prototype declaration?
a) To define a function
b) To declare a function
c) To specify the number of parameters
d) To specify the return type
Answer: b) To declare a function
35. Where should prototype declarations be placed?
a) At the beginning of the program
b) At the end of the program
c) Before the function definition
d) After the function definition
Answer: c) Before the function definition
36. What is the syntax for a prototype declaration?
a) return_type function_name (parameter_list);
b) function_name (parameter_list);
c) return_type function_name ();
d) function_name ();
Answer: a) return_type function_name (parameter_list);
37. What is the benefit of using prototype declarations?
a) It allows for function overloading
b) It allows for function overriding
c) It helps in type checking
d) It improves code readability
Answer: c) It helps in type checking
38. Can you have multiple prototype declarations for the same function?
a) Yes, if they have different parameters
b) Yes, if they have different return types
c) No, prototype declarations must be unique
d) No, but you can have multiple function definitions
Answer: c) No, prototype declarations must be unique
39. How do prototype declarations help in preventing errors?
a) By detecting type mismatches
b) By detecting syntax errors
c) By detecting logical errors
d) By detecting runtime errors
Answer: a) By detecting type mismatches
40. Can you declare a prototype for a function that takes no arguments?
a) Yes, use the void keyword
b) No, you cannot declare a prototype for a function that takes no arguments
c) Yes, but only if the function returns no value
d) No, but you can declare a prototype for a function that returns no value
Answer: a) Yes, use the void keyword
41. How do prototype declarations improve code readability?
a) By providing a clear indication of the function's purpose
b) By providing a clear indication of the function's parameters
c) By providing a clear indication of the function's return type
d) By providing a clear indication of the function's implementation
Answer: b) By providing a clear indication of the function's parameters
42. Can you declare a prototype for a function that returns a pointer?
a) Yes, use the pointer syntax
b) No, you cannot declare a prototype for a function that returns a pointer
c) Yes, but only if the function takes no arguments
d) No, but you can declare a prototype for a function that takes no arguments
Answer: a) Yes, use the pointer syntax
43. What is an actual argument in C programming?
a) A variable used in a function call
b) A value passed to a function
c) A function itself
d) A parameter declared in a function definition
Answer: b) A value passed to a function
44. Which of the following is an example of an actual argument?
a) int x = 5;
b) func(x);
c) int func(int p) { }
d) return x;
Answer: b) func(x);
45. What is the purpose of an actual argument?
a) To declare a variable
b) To pass a value to a function
c) To define a function
d) To return a value from a function
Answer: b) To pass a value to a function
46. How many actual arguments can a function take?
a) Only one
b) Only two
c) Any number
d) No limit
Answer: c) Any number
47. What happens if the number of actual arguments does not match the number of formal parameters?
a) Error
b) Warning
c) Ignored
d) Default values are used
Answer: a) Error
48. Can an actual argument be an expression?
a) Yes
b) No
c) Only if it's a simple expression
d) Only if it's a complex expression
Answer: a) Yes
49. What is the difference between an actual argument and a formal parameter?
a) Actual argument is the value passed, formal parameter is the variable declared
b) Actual argument is the variable declared, formal parameter is the value passed
c) Actual argument is the function itself, formal parameter is the function call
d) Actual argument is the function call, formal parameter is the function itself
Answer: a) Actual argument is the value passed, formal parameter is the variable declared
50. How are actual arguments passed to a function?
a) By value
b) By reference
c) By pointer
d) By array
Answer: a) By value
51. Can an actual argument be an array?
a) Yes
b) No
c) Only if it's a one-dimensional array
d) Only if it's a multi-dimensional array
Answer: a) Yes
52. What happens if an actual argument is passed by reference?
a) A copy of the original value is created
b) The original value is modified
c) A pointer to the original value is passed
d) A reference to the original value is passed
Answer: c) A pointer to the original value is passed
53. What is a formal argument in C programming?
a) A variable used in a function call
b) A value passed to a function
c) A parameter declared in a function definition
d) A function itself
Answer: c) A parameter declared in a function definition
54. Which of the following is an example of a formal argument?
a) int x = 5;
b) func(x);
c) int func(int p) { }
d) return x;
Answer: c) int func(int p) { }
55. What is the purpose of a formal argument?
a) To declare a variable
b) To pass a value to a function
c) To define a function
d) To receive a value from a function call
Answer: d) To receive a value from a function call
56. How many formal arguments can a function have?
a) Only one
b) Only two
c) Any number
d) No limit
Answer: c) Any number
57. What happens if the number of formal arguments does not match the number of actual arguments?
a) Error
b) Warning
c) Ignored
d) Default values are used
Answer: a) Error
58. Can a formal argument be an array?
a) Yes
b) No
c) Only if it's a one-dimensional array
d) Only if it's a multi-dimensional array
Answer: a) Yes
59. How are formal arguments passed to a function?
a) By value
b) By reference
c) By pointer
d) By array
Answer: a) By value
60. What is the difference between a formal argument and an actual argument?
a) Formal argument is the value passed, actual argument is the variable declared
b) Formal argument is the variable declared, actual argument is the value passed
c) Formal argument is the function itself, actual argument is the function call
d) Formal argument is the function call, actual argument is the function itself
Answer: b) Formal argument is the variable declared, actual argument is the value passed
61. Can a formal argument have a default value?
a) Yes
b) No
c) Only if it's a primitive type
d) Only if it's a user-defined type
Answer: a) Yes
62. What happens if a formal argument is not initialized?
a) Error
b) Warning
c) Default value is used
d) Garbage value is used
Answer: d) Garbage value is used
63. What is a function definition in C programming?
a) Declaring a function
b) Defining a function
c) Calling a function
d) Returning a value from a function
Answer: b) Defining a function
64. Which of the following is an example of a function definition?
a) int add(int a, int b);
b) int add(int a, int b) { return a + b; }
c) int result = add(2, 3);
d) void main() { }
Answer: b) int add(int a, int b) { return a + b; }
65. What is the purpose of a function definition?
a) To declare a function
b) To define a function
c) To call a function
d) To return a value from a function
Answer: b) To define a function
66. What are the two parts of a function definition?
a) Function header and function body
b) Function declaration and function call
c) Function name and function parameters
d) Function return type and function arguments
Answer: a) Function header and function body
67. What is the function header in a function definition?
a) The first line of the function
b) The last line of the function
c) The part that declares the function
d) The part that defines the function
Answer: c) The part that declares the function
68. What is the function body in a function definition?
a) The part that declares the function
b) The part that defines the function
c) The part that contains the code
d) The part that returns a value
Answer: c) The part that contains the code
69. Can a function definition be nested inside another function?
a) Yes
b) No
c) Only if the inner function is static
d) Only if the outer function is global
Answer: b) No
70. How many parameters can a function definition have?
a) Only one
b) Only two
c) Any number
d) No limit
Answer: c) Any number
71. What is the purpose of the return statement in a function definition?
a) To return a value to the caller
b) To declare a variable
c) To define a function
d) To call a function
Answer: a) To return a value to the caller
72. Can a function definition be overloaded?
a) Yes
b) No
c) Only if the functions have different return types
d) Only if the functions have different parameter lists
Answer: d) Only if the functions have different parameter lists
73. How can an array be passed as a parameter to a function in C?
a) By value
b) By reference
c) By pointer
d) By array name
Answer: c) By pointer
74. What is the syntax to pass an array as a parameter to a function in C?
a) func(array_name)
b) func(&array_name)
c) func(array_name[])
d) func(*array_name)
Answer: c) func(array_name[])
75. When an array is passed as a parameter to a function, what is actually passed?
a) The entire array
b) A copy of the array
c) A pointer to the first element of the array
d) A reference to the array
Answer: c) A pointer to the first element of the array
76. How can the size of the array be passed as a parameter to a function in C?
a) By using the sizeof operator
b) By passing the size as a separate parameter
c) By using a global variable
d) By using a constant
Answer: b) By passing the size as a separate parameter
78. What is the advantage of passing arrays as parameters to functions in C?
a) It allows for code reusability
b) It allows for faster execution
c) It allows for better memory management
d) It allows for easier debugging
Answer: a) It allows for code reusability
79. How can a 2D array be passed as a parameter to a function in C?
a) By using a pointer to a pointer
b) By using a pointer to an array
c) By using a pointer to a structure
d) By using a pointer to a union
Answer: a) By using a pointer to a pointer
80. What is the difference between passing an array by value and passing an array by reference in C?
a) Passing by value creates a copy of the array, while passing by reference does not
b) Passing by value does not create a copy of the array, while passing by reference does
c) Passing by value allows for modification of the original array, while passing by reference does not
d) Passing by value does not allow for modification of the original array, while passing by reference does
Answer: a) Passing by value creates a copy of the array, while passing by reference does not
81. How can the base address of an array be obtained in C?
a) By using the & operator
b) By using the * operator
c) By using the sizeof operator
d) By using the offsetof macro
Answer: a) By using the & operator
82. What is the purpose of the const keyword when passing arrays as parameters in C?
a) To prevent modification of the original array
b) To allow modification of the original array
c) To create a copy of the array
d) To pass the array by value
Answer: a) To prevent modification of the original array
83. How can an array of structures be passed as a parameter to a function in C?
a) By using a pointer to a structure
b) By using a pointer to an array of structures
c) By using a pointer to a pointer to a structure
d) By using a pointer to a pointer to an array of structures
Answer: b) By using a pointer to an array of structures
84. What is the main difference between Call by Value and Call by Reference?
a) Call by Value passes a copy of the variable, while Call by Reference passes the actual variable.
b) Call by Value passes the actual variable, while Call by Reference passes a copy of the variable.
c) Call by Value is used for primitive types, while Call by Reference is used for reference types.
d) Call by Value is used for reference types, while Call by Reference is used for primitive types.
Answer: a) Call by Value passes a copy of the variable, while Call by Reference passes the actual variable.
85. Which of the following is an example of Call by Value?
a) func(x);
b) func(&x);
c) func(*x);
d) func(x, &x);
Answer: a) func(x);
86. Which of the following is an example of Call by Reference?
a) func(x);
b) func(&x);
c) func(*x);
d) func(x, &x);
Answer: b) func(&x);
87. What happens when a variable is passed by value to a function?
a) The original value is modified.
b) A copy of the original value is created.
c) The variable is referenced.
d) The variable is pointed to.
Answer: b) A copy of the original value is created.
88. What happens when a variable is passed by reference to a function?
a) The original value is modified.
b) A copy of the original value is created.
c) The variable is referenced.
d) The variable is pointed to.
Answer: c) The variable is referenced.
89. Can a function modify the original value of a variable passed by value?
a) Yes
b) No
c) Maybe
d) Depends on the function
Answer: b) No
90. Can a function modify the original value of a variable passed by reference?
a) Yes
b) No
c) Maybe
d) Depends on the function
Answer: a) Yes
91. What is the advantage of passing variables by value to a function?
a) Faster execution
b) Better memory management
c) Code reusability
d) Prevents modification of original value
Answer: d) Prevents modification of original value
92. What is the advantage of passing variables by reference to a function?
a) Faster execution
b) Better memory management
c) Code reusability
d) Allows modification of original value
Answer: d) Allows modification of original value
93. Can an array be passed by value to a function?
a) Yes
b) No
c) Maybe
d) Depends on the array
Answer: b) No (Arrays are passed by reference)