Why can a function declaration allow a call to appear before the function's definition in a C++ source file?
05 Functions Online Quiz Questions
Use this free practice quiz with 20 questions to review 05 Functions, test your knowledge, and prepare for your next test or exam.
Given void increase(int value) { ++value; }, what is the value of number after this code executes?
int number = 10;increase(number);
- A
The caller's variable becomes 11 because every parameter is passed by reference.
- B
The program is ill-formed because an integer cannot be passed to a function.
- C
The caller's variable remains 10 because the parameter is passed by value.
- D
The function changes the caller's variable only after it returns.
Consider these overloads:
int area(int side)double area(double radius)
Which overload is selected for the call area(5)?
- A
double area(double radius)becausedoubleis more general thanint. - B
Both overloads are called and their results are compared.
- C
int area(int side)because the argument5has typeint. - D
Neither overload can be selected because overloaded functions must have different names.
Which differences can distinguish valid C++ function overloads? Select all correct choices.
- A
The number of parameters
- B
The parameter types
- C
The return type alone
- D
The order of parameter types
Which properties are necessary for a practical recursive function? Select all correct choices.
- A
A base case that stops further recursive calls
- B
A recursive case that works on a smaller or simpler problem
- C
A different function name for every recursive call
- D
Eventual termination before available stack space is exhausted
True or false: A C++ function declared with return type void may use return; to exit early, but it cannot return a value.
- A
True
- B
False
True or false: Returning a local variable by value is safe, whereas returning a pointer or reference to that local variable is invalid after the function ends.
- A
True
- B
False
What C++ term describes the actual value supplied to a function call, such as 4 and 6 in multiply(4, 6)?
What integer value does the nested call square(add(2, 3)) produce?
Complete the general structure of a C++ function definition:
function_name() { statements }
Complete the sentence: A parameter can allow a function to modify the caller's original variable.
Explain the difference between a C++ function declaration and definition, including why a declaration can appear before a function call and what must agree between the declaration and definition.
What does this program print when showCount() is called twice?
void showCount() { int count = 0; ++count; std::cout << count << '\n';}
- A
It prints
1and then2. - B
It prints
1twice. - C
It prints
0twice. - D
It prints
2twice.
Which return type should a C++ function use when it does not return a value?
- A
null - B
void - C
empty - D
none
What is the main purpose of a function declaration such as int add(int first, int second);?
- A
It executes the function immediately.
- B
It provides the function body.
- C
It tells the compiler the function's name, return type, and parameter types.
- D
It creates a local variable for every parameter.
What is the C++ term for the variable listed in a function declaration or definition that receives a value from the caller?
True or false: Returning a reference to a local variable is invalid because the local variable's lifetime ends when the function returns.
- A
True
- B
False
Given void increase(int value) { ++value; }, what happens after int number = 10; increase(number);?
- A
The function changes a copy, so the caller's original variable remains unchanged.
- B
The function changes the caller's original variable directly.
- C
The program cannot compile because integers cannot be parameters.
- D
The function changes all variables with the same name.
Which function signature allows increase to modify the caller's integer variable directly through a reference parameter?
- A
void increase(int value) - B
void increase(const int value) - C
void increase(int* value) - D
void increase(int& value)
What integer value is produced by the nested call square(add(1, 2)), assuming add returns the sum of its two arguments and square returns its argument multiplied by itself?