C++ Ch 5 Quiz - Functions

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 38 | Total Attempts: 29,972
Questions: 22 | Attempts: 457

SettingsSettingsSettings
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 will do just that. All the best as you attempt the quiz.


Questions and Answers
  • 1. 

    A void function can return any value   true or false?

    • A.

      True

    • B.

      False

    Correct Answer
    B. 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.

    Rate this question:

  • 2. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 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?

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 4. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 5. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    B. 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.

    Rate this question:

  • 6. 

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

    • A.

      Void displayOutput(myTotal);

    • B.

      DisplayOutput(int mytotal);

    • C.

      DisplayOutput(myTotal);

    • D.

      Cout

    Correct Answer
    C. DisplayOutput(myTotal);
    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.

    Rate this question:

  • 7. 

    The precondition(s) for a function describe

    • A.

      What is true after the function is executed

    • B.

      What the function does

    • C.

      How to call the function

    • D.

      What must be true before the function executes

    Correct Answer
    D. What must be true before the function executes
    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.

    Rate this question:

  • 8. 

    Which of the following is true for a void function?

    • A.

      There cannot be a return statement.

    • B.

      The value of void should be returned.

    • C.

      The value of 0 should be returned.

    • D.

      Nothing is returned

    Correct Answer
    D. Nothing is returned
    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".

    Rate this question:

  • 9. 

    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; }

    • A.

      The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;

    • B.

      The cost for 15 items is 0.00, and the tax for 3.00 is 0.00;

    • C.

      The cost for 15 items is 0.00, and the tax for 3.00 is 0.30;

    • D.

      The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;

    Correct Answer
    A. The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;
    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".

    Rate this question:

  • 10. 

    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; }

    • A.

      The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;

    • B.

      The cost for 15 items is 0.00, and the tax for 3.00 is 0.00;

    • C.

      The cost for 15 items is 0.00, and the tax for 3.00 is 0.30;

    • D.

      The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;

    Correct Answer
    D. The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;
    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".

    Rate this question:

  • 11. 

    Which of the following function prototypes are not valid?

    • A.

      Void doSomething(int& x, int y);

    • B.

      Void doSomething( int& x, int& y);

    • C.

      Void doSomething( int x, int y);

    • D.

      All are valid

    Correct Answer
    D. All are 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.

    Rate this question:

  • 12. 

    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; }

    • A.

      3

    • B.

      0

    • C.

      -1

    • D.

      99

    Correct Answer
    D. 99
    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 < 0" is not satisfied, so it moves to the next condition. The condition "par_count = 0" is also not satisfied, so it goes to the else statement where "par_choice" is assigned a value of 99. Therefore, the value of "choice" is 99 after the function call.

    Rate this question:

  • 13. 

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

    • A.

      Void display();

    • B.

      Void display(float myCost);

    • C.

      Int display (float myCost);

    • D.

      Float display();

    Correct Answer
    B. Void display(float myCost);
    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.

    Rate this question:

  • 14. 

    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;

    • A.

      1 14 3

    • B.

      1 10 3

    • C.

      5 14 3

    • D.

      1 14 9

    • E.

      None

    Correct Answer
    A. 1 14 3
    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".

    Rate this question:

  • 15. 

    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);

    • A.

      SomeFunction(1, 2.0, ch);

    • B.

      SomeFunction(myInt, myFloat, ch);

    • C.

      SomeFunction(myInt, 2.0, 'c');

    • D.

      SomeFunction(myInt, myFloat, '1');

    Correct Answer
    A. SomeFunction(1, 2.0, ch);
    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.

    Rate this question:

  • 16. 

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

    • A.

      The value of x

    • B.

      Nothing, it is a void function

    • C.

      The value of y

    • D.

      The variable x (or its memory location)

    Correct Answer
    D. The variable x (or its memory location)
    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).

    Rate this question:

  • 17. 

    A simplified main program used to test functions is called

    • A.

      A stub

    • B.

      Abstraction

    • C.

      Polymorphism

    • D.

      A driver

    Correct Answer
    D. A driver
    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.

    Rate this question:

  • 18. 

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

    • A.

      //Postcondition: None

    • B.

      //Postcondition: the values of left and right are exchanged.

    • C.

      //Postcondition: left has the value of right

    • D.

      //Postcondition: left and right are unchanged

    Correct Answer
    B. //Postcondition: the values of left and right are exchanged.
    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.

    Rate this question:

  • 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; } }

    • A.

      Neither function should return a value

    • B.

      Function definitions may not be nested

    • C.

      Both parameters to f2 should be pass-by reference

    • D.

      In f2, a can not be assigned b.

    • E.

      Nothing is wrong

    Correct Answer
    B. Function definitions may not be nested
    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.

    Rate this question:

  • 20. 

    Testing your program should be done

    • A.

      As each function is developed

    • B.

      At the end of the coding

    • C.

      Only if there appear to be problems

    • D.

      Only if your instructor requires it.

    Correct Answer
    A. As each function is developed
    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.

    Rate this question:

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.