Chapter 4: MCQs on Functions in C++ | C++ Programming

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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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){}
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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);
  9. 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
  10. 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

QnoAnswer
1c) To execute a specific task
2b) Functions are blocks of reusable code
3b) A request to execute a function
4b) They can be reused multiple times with different parameters
5b) The value the function returns to the caller
6c) int func(void)
7a) By writing the return type, followed by the function name and parameters
8b) The function name and arguments
9b) The return type and parameters
10a) function_name();
11a) The values passed to a function during its call
12b) Any type based on the return type
13b) By using pointers or references
14b) Default values are assigned to the parameters
15a) void
16b) A copy of the variable’s value
17b) A reference or memory address of the variable
18b) Changes made to the parameter do not affect the original argument
19b) When you want to avoid unnecessary copying of large data
20a) It uses memory more efficiently for large data types
21b) An argument that is assigned a default value if not provided
22a) Writing functions with the same name but different parameters
23b) An error is thrown by the compiler
24a) It will use the default value specified in the function definition
25a) Overloaded functions must have different names
26b) By keeping the same function name but changing the function signature
27b) It only works with primitive data types
28a) void func(int x = 10);
29b) No, C++ does not allow overloading based on return type only
30a) Simplifies function calls by providing automatic values

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top