1.
A void function can return any value
true or 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.
2.
The following is legal in a void function
return;
true or 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.
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?
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.
4.
It is acceptable to have both call-by-value and call-by-reference parameters in the
same function declaration.
true or 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.
5.
A stub is a function that is completely defined and well tested
true or 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.
6.
Which of the following is a legal call to the displayOutput function?
void displayOutput(int total);
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.
7.
The precondition(s) for a function describe
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.
8.
Which of the following is true for a void function?
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".
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;
}
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".
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;
}
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".
11.
Which of the following function prototypes are not 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.
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;
}
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.
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?
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.
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;
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".
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);
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.
16.
In the following function, what is passed to the first parameter?
void f1( int& value1, int value2);
int x,y;
f1(x,y);
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).
17.
A simplified main program used to test functions is called
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.
18.
Which of the following comments would be the best post-condition for this swap
function void swap( int& left, int&right);
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.
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;
}
}
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.
20.
Testing your program should be done
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.