Explore the key concepts of functions in this set of 30 MCQs. Topics covered include function declaration, calling, parameters, return types, pass by value vs. pass by reference, and more.
MCQs on Functions
Introduction to Functions
What is the primary purpose of a function in programming?
a) To store data
b) To define program structure
c) To execute a specific task
d) To display output
Which of the following is true about functions?
a) Functions cannot return values
b) Functions are blocks of reusable code
c) Functions cannot take parameters
d) Functions are only used for input-output operations
What is a function call in programming?
a) A type of variable
b) A request to execute a function
c) A method of data storage
d) A conditional expression
How do functions contribute to code reusability?
a) They store data
b) They can be reused multiple times with different parameters
c) They are only used once
d) They execute code sequentially
What is the return type of a function?
a) The type of parameter
b) The value the function returns to the caller
c) The name of the function
d) The address of the function
Function Declaration, Definition, and Calling
What is the correct syntax for declaring a function in C/C++?
a) int func(){}
b) void func(){}
c) int func(void)
d) func(int a){}
How do you define a function in C/C++?
a) By writing the return type, followed by the function name and parameters
b) By only writing the return type
c) By writing only the function name
d) By writing the function parameters only
What is required to call a function in a program?
a) Only the function name
b) The function name and arguments
c) The return type and name
d) The function parameters only
What is the function signature in C/C++?
a) The function name
b) The return type and parameters
c) The body of the function
d) The return statement
Which of the following is the correct way to call a function in C++?
a) function_name();
b) call function_name();
c) execute function_name();
d) invoke function_name();
Function Parameters and Return Types
What are function parameters?
a) The values passed to a function during its call
b) The result returned by the function
c) The function’s name
d) The type of function
Which type of return value can a function have?
a) Only integer values
b) Any type based on the return type
c) Only string values
d) Functions cannot return values
In C++, how can a function return multiple values?
a) By using an array
b) By using pointers or references
c) By using multiple return statements
d) A function can return only one value
What happens if a function is called without passing the required parameters?
a) The program crashes
b) Default values are assigned to the parameters
c) The compiler will throw an error
d) The program will run but return wrong results
What is the return type of a function that doesn’t return anything?
a) void
b) int
c) char
d) null
Pass by Value vs Pass by Reference
In “pass by value,” what is passed to the function?
a) A reference to the variable
b) A copy of the variable’s value
c) The address of the variable
d) The variable itself
In “pass by reference,” what is passed to the function?
a) A copy of the variable’s value
b) A reference or memory address of the variable
c) The variable’s return value
d) A duplicate of the variable
Which of the following best describes the effect of passing by value?
a) Changes made to the parameter affect the original argument
b) Changes made to the parameter do not affect the original argument
c) No changes are made to the parameter
d) The argument is passed by reference
In which situation would “pass by reference” be preferred over “pass by value”?
a) When you don’t want to modify the argument
b) When you want to avoid unnecessary copying of large data
c) When you want the function to work with primitive data types
d) When the function must return a value
Which of the following is true for “pass by reference” in C++?
a) It uses memory more efficiently for large data types
b) It creates a copy of the data for the function
c) It is the default method of passing parameters in C++
d) It passes data by copying only the address
Default Arguments and Function Overloading
What is a default argument in a function?
a) An argument passed by reference
b) An argument that is assigned a default value if not provided
c) An argument with no value
d) An argument passed by value
What is function overloading in C++?
a) Writing functions with the same name but different parameters
b) Writing functions with different names but similar parameters
c) Writing functions that return the same value
d) Writing functions that handle exceptions
What happens when the number of arguments in a function call does not match the declared function parameters?
a) The program runs with default values
b) An error is thrown by the compiler
c) The function will return a default value
d) The program will return wrong results
How does a function with default arguments behave when no argument is provided?
a) It will use the default value specified in the function definition
b) It will cause an error
c) It will return zero by default
d) It will return null by default
Which of the following statements about function overloading is false?
a) Overloaded functions must have different names
b) Overloaded functions must differ in the number or type of arguments
c) Function overloading helps in maintaining code clarity
d) Overloaded functions can have different return types
How does function overloading improve program readability?
a) By providing different names for similar functions
b) By keeping the same function name but changing the function signature
c) By using default arguments to simplify code
d) By reducing the number of function calls
What is the major limitation of function overloading in C++?
a) Overloading is not allowed in all programming languages
b) It only works with primitive data types
c) Overloading only works for functions with the same return type
d) The compiler cannot differentiate between overloaded functions with the same number of parameters
What is the correct syntax to declare a function with a default argument?
a) void func(int x = 10);
b) void func(int x = 0, int y);
c) void func(int x = 10, int y = 20);
d) int func(int x = 10, int y = 20);
Can you overload a function based on the return type only in C++?
a) Yes, function overloading can happen with the return type
b) No, C++ does not allow overloading based on return type only
c) Yes, as long as the parameters differ
d) No, return types must be different for overloading to occur
What is the benefit of default arguments in a function?
a) Simplifies function calls by providing automatic values
b) Increases the number of required arguments
c) Allows functions to be called without parameters
d) Forces the use of specific arguments
Answer Table
Qno
Answer
1
c) To execute a specific task
2
b) Functions are blocks of reusable code
3
b) A request to execute a function
4
b) They can be reused multiple times with different parameters
5
b) The value the function returns to the caller
6
c) int func(void)
7
a) By writing the return type, followed by the function name and parameters
8
b) The function name and arguments
9
b) The return type and parameters
10
a) function_name();
11
a) The values passed to a function during its call
12
b) Any type based on the return type
13
b) By using pointers or references
14
b) Default values are assigned to the parameters
15
a) void
16
b) A copy of the variable’s value
17
b) A reference or memory address of the variable
18
b) Changes made to the parameter do not affect the original argument
19
b) When you want to avoid unnecessary copying of large data
20
a) It uses memory more efficiently for large data types
21
b) An argument that is assigned a default value if not provided
22
a) Writing functions with the same name but different parameters
23
b) An error is thrown by the compiler
24
a) It will use the default value specified in the function definition
25
a) Overloaded functions must have different names
26
b) By keeping the same function name but changing the function signature
27
b) It only works with primitive data types
28
a) void func(int x = 10);
29
b) No, C++ does not allow overloading based on return type only
30
a) Simplifies function calls by providing automatic values