C++ Ch 5 Quiz - Functions

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 38 | Total Attempts: 32,005
| Attempts: 474 | Questions: 22
Please wait...
Question 1 / 22
0 %
0/100
Score 0/100
1. Match the following
Submit
Please wait...
About This Quiz
C++ Ch 5 Quiz - Functions - Quiz

C++ is a programming tool widely used today. If you are a programmer and you are looking for ways to better yourself in this field, then this quiz... see morewill do just that. All the best as you attempt the quiz. see less

2. Match the following items:
Submit
3. In a function with call-by-reference parameters, any changes to the formal parameters will change the actual arguments passed to the function. true or false?

Explanation

In a function with call-by-reference parameters, the formal parameters are aliases for the actual arguments passed to the function. This means that any changes made to the formal parameters within the function will directly affect the corresponding actual arguments. Therefore, the statement is true.

Submit
4. It is acceptable to have both call-by-value and call-by-reference parameters in the same function declaration. true or false?

Explanation

It is acceptable to have both call-by-value and call-by-reference parameters in the same function declaration because different parameters can be passed using different methods depending on the requirements of the function. Call-by-value parameters pass the value of the variable to the function, while call-by-reference parameters pass the memory address of the variable. This allows for flexibility in how the function can manipulate and access the values of the parameters.

Submit
5. What is the output of the following function and function call? void calculateCost(int count, float& subTotal, float& taxCost); float tax = 0.0, subTotal = 0.0; calculateCost(15, subTotal,tax); cout << "The cost for 15 items is " << subtotal << ", and the tax for " << subTotal << " is " << tax << endl; //end of fragment void calculateCost(int count, float& subTotal, float& taxCost) { if ( count < 10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; }

Explanation

The function calculateCost takes three parameters: count, subTotal, and taxCost. In the given function call, the value of count is 15, and the values of subTotal and tax are initially 0.0.

Inside the calculateCost function, there is an if-else statement. Since the count (15) is greater than or equal to 10, the else block is executed. In the else block, the subTotal is calculated as count * 0.20, which is 15 * 0.20 = 3.00.

After calculating the subTotal, the taxCost is calculated as 0.1 * subTotal, which is 0.1 * 3.00 = 0.30.

Therefore, the output of the function call is "The cost for 15 items is 3.00, and the tax for 3.00 is 0.30".

Submit
6. The following is legal in a void function return; true or false?

Explanation

In a void function, the use of "return;" is legal. This statement simply exits the function and does not return any value. It is commonly used to prematurely end the function execution or to indicate that there is no further code to be executed in the function. Therefore, the answer is true.

Submit
7. A void function can return any value   true or false?

Explanation

A void function is a function that does not return a value. It is used to perform a task or operation without returning any result. Therefore, a void function cannot return any value.

Submit
8. Testing your program should be done

Explanation

Testing your program should be done as each function is developed because it allows for early detection and correction of any issues or bugs in the code. By testing each function as it is developed, it becomes easier to identify and fix any errors before they compound and become more difficult to resolve. This approach also helps in ensuring that each function works correctly on its own before integrating it into the larger program.

Submit
9. Which of the following comments would be the best post-condition for this swap function void swap( int& left, int&right);

Explanation

The best post-condition for the swap function would be "//Postcondition: the values of left and right are exchanged." This means that after the swap function is called, the values of the variables "left" and "right" will be swapped or exchanged with each other. This accurately describes the expected outcome of the swap function.

Submit
10. Which of the following function prototypes are not valid?

Explanation

All of the given function prototypes are valid. The first two prototypes use reference parameters, which allow the function to modify the values of the variables passed in. The third prototype uses pass-by-value parameters, which create copies of the variables passed in and do not modify the original values. Therefore, all three prototypes are valid and can be used in a program.

Submit
11. Which of the following is true for a void function?

Explanation

A void function does not return any value. It is used to perform a task or execute a set of instructions without returning a value. Therefore, the correct answer is "Nothing is returned".

Submit
12. If you were to write a function for displaying the cost of an item to the screen, which function prototype would be most appropriate?

Explanation

The most appropriate function prototype for displaying the cost of an item to the screen would be "void display(float myCost)". This is because the function should have a void return type since it is not expected to return any value. Additionally, it should take a single parameter of type float, which represents the cost of the item that needs to be displayed. This allows the function to receive the cost as an argument and then display it on the screen.

Submit
13. A stub is a function that is completely defined and well tested true or false?

Explanation

A stub is a function that is not completely defined and may not be well tested. Stubs are used in software development for testing purposes, where they simulate the behavior of certain functions or modules. They are often used when the actual implementation of a function is not yet available or when testing specific scenarios. Therefore, the statement that a stub is completely defined and well tested is incorrect.

Submit
14. The precondition(s) for a function describe

Explanation

The precondition(s) for a function describe what must be true before the function executes. This means that certain conditions or requirements need to be met in order for the function to run correctly. These preconditions ensure that the function has the necessary inputs or resources to perform its intended task. Without satisfying the preconditions, the function may not work as expected or may encounter errors.

Submit
15. Which of the following is a legal call to the displayOutput function? void displayOutput(int total);

Explanation

The correct answer is "displayOutput(myTotal)". This is a legal call to the displayOutput function because it passes the variable "myTotal" as an argument, which matches the parameter type of "int" in the function declaration.

Submit
16. Given the following function declaration and local variable declarations, which of the following is not a correct function call? int myInt; float myFloat; char ch; void someFunction(int& first, float second, char third);

Explanation

The function call someFunction(1, 2.0, ch) is not correct because the first parameter is of type int&, which is a reference to an int. In the function call, a literal value 1 is passed as the argument for the first parameter, which cannot be bound to a reference.

Submit
17. What is the value of choice after the following statements? void getChoice(int& par_choice, in par_count); int choice, count=3; getChoice(choice, count); void getChoice(int& par_choice, in par_count) { if(par_count<0) par_choice =0; if(par_count = 0) par_choice=-1; else par_choice=99; return; }

Explanation

The value of "choice" after the given statements is 99. This is because the function "getChoice" is called with the arguments "choice" and "count". Inside the function, there is a series of conditional statements. Since the value of "count" is 3, the condition "par_count

Submit
18.
  Given the function definition void something ( int a, int& b ) { int c; c = a + 2; a = a * 3; b = c + a; } what is the output of the following code fragment that invokes something? (All variables are of type int.) r = 1; s = 2; t = 3; something(t, s); cout << r << ' ' << s << ' ' << t << endl;

Explanation

The output of the code fragment is "1 14 3" because the function "something" takes two parameters, an integer "a" and a reference to an integer "b". Inside the function, "c" is assigned the value of "a + 2", "a" is assigned the value of "a * 3", and "b" is assigned the value of "c + a". When the function is invoked with "t" and "s" as arguments, "t" remains unchanged, "s" becomes 14 (2 + 3 * 3), and "r" remains unchanged. Therefore, the output is "1 14 3".

Submit
19. What is wrong with the following code? int f1( int x, int y) { x = y * y; return x; int f2( float a, float& b) { if(a < b) b = a; else a=b; return 0.0; } }

Explanation

The code provided contains nested function definitions, which is not allowed in C++. Function definitions cannot be nested inside other functions. This violates the syntax rules of the language.

Submit
20. In the following function, what is passed to the first parameter? void f1( int& value1, int value2); int x,y; f1(x,y);

Explanation

In this code, the function f1 is called with two parameters: value1 and value2. The first parameter, value1, is declared as a reference to an int. When the function is called with f1(x, y), it means that the variable x (or its memory location) is passed as an argument to the first parameter, value1. Therefore, the correct answer is the variable x (or its memory location).

Submit
21. A simplified main program used to test functions is called

Explanation

A driver is a simplified main program used to test functions. It is responsible for calling the functions and providing inputs to test their functionality. It helps in verifying the correctness of the functions and ensures that they are working as expected.

Submit
22. What is the output of the following function and function call? void calculateCost(int count, float& subTotal, float taxCost); float tax = 0.0, subtotal = 0.0; calculateCost(15, subtotal,tax); cout << "The cost for 15 items is " << subtotal << ", and the tax for " << subtotal << " is " << tax << endl; //end of fragment void calculateCost(int count, float& subTotal, float taxCost) { if ( count < 10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; }

Explanation

The output of the function and function call is "The cost for 15 items is 3.00, and the tax for 3.00 is 0.00". This is because the function "calculateCost" takes in three parameters - an integer count, a float reference subTotal, and a float taxCost. Inside the function, it checks if the count is less than 10. Since 15 is greater than 10, the else statement is executed and subTotal is calculated as 15 * 0.20, which is 3.00. The taxCost is then calculated as 0.1 * subTotal, which is 0.30. However, since taxCost is not a reference parameter, the changes made inside the function do not affect the value of tax outside the function, which remains 0.00. Therefore, the output is "The cost for 15 items is 3.00, and the tax for 3.00 is 0.00".

Submit
View My Results

Quiz Review Timeline (Updated): Feb 17, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 17, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 20, 2013
    Quiz Created by
    Tcarteronw
Cancel
  • All
    All (22)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Match the following
Match the following items:
In a function with call-by-reference parameters, any changes to the...
It is acceptable to have both call-by-value and call-by-reference...
What is the output of the following function and function call? ...
The following is legal in a void function return; true or false?
A void function can return any value   true or false?
Testing your program should be done
Which of the following comments would be the best post-condition for...
Which of the following function prototypes are not valid?
Which of the following is true for a void function?
If you were to write a function for displaying the cost of an item to...
A stub is a function that is completely defined and well tested ...
The precondition(s) for a function describe
Which of the following is a legal call to the displayOutput function? ...
Given the following function declaration and local variable...
What is the value of choice after the following statements? ...
  ...
What is wrong with the following code? ...
In the following function, what is passed to the first parameter? ...
A simplified main program used to test functions is called
What is the output of the following function and function call? ...
Alert!

Advertisement