Chapter 6 Complete Test Bank Functions - Test Bank | C++ Control Structures 9e by Tony Gaddis. DOCX document preview.
Starting Out with C++ from Control Structures to Objects, 9e (Gaddis)
Chapter 6 Functions
TRUE/FALSE
1. When a function is called, flow of control moves to the function's prototype.
2. A parameter is a special purpose variable that is declared inside the parentheses of a function definition.
3. A local variable and a global variable may not have the same name within a program.
4. A static variable that is defined within a function is initalized only once, the first time it is called.
5. It is possible for a function to have some parameters with default arguments and some without.
6. A function's return data type must be the same as the function's parameters.
7. One reason for using functions is to break programs into manageable units or modules.
8. You must always furnish an argument with a function call.
9. Global variables are initialized to zero by default.
10. Local variables are initialized to zero by default.
11. It is not considered good programming practice to declare all your variables globally.
12. You may use the exit() function to terminate a program, regardless of which control mechanism is executing.
MULTIPLE CHOICE
1. A collection of statements that performs a specific task is a(n)
a. | infinite loop |
b. | variable |
c. | constant |
d. | function |
e. | decision |
2. A function __________ contains the statements that make up the function.
a. | prototype |
b. | definition |
c. | call |
d. | expression |
e. | parameter list |
3. A function can have no parameters, one parameter, or many parameters and can return __________ value(s).
a. | zero to many |
b. | no |
c. | only one |
d. | a maximum of ten |
e. | None of these |
4. A function is executed when it is
a. | defined |
b. | prototyped |
c. | declared |
d. | called |
e. | None of these |
5. Functions are ideal for menu-driven programs. When the user selects a menu item, the program can __________ the appropriate function.
a. | call |
b. | append |
c. | define |
d. | declare |
e. | None of these |
6. This type of variable is defined inside a function and is not accessible outside the function.
a. | global |
b. | reference |
c. | local |
d. | counter |
e. | None of these |
7. The value in this type of variable persists between function calls.
a. | global |
b. | internal |
c. | static |
d. | dynamic |
e. | None of these |
8. These types of arguments are passed to parameters automatically if no argument is provided in the function call.
a. | local |
b. | default |
c. | global |
d. | reference |
e. | None of these |
9. When used as parameters, these types of variables allow a function to access the parameter's original argument:
a. | reference |
b. | floating-point |
c. | counter |
d. | undeclared |
e. | None of these |
10. __________ functions may have the same name as long as their parameter lists are different.
a. | Only two |
b. | Two or more |
c. | No |
d. | Un-prototyped |
e. | None of these |
11. This statement causes a function to end.
a. | end |
b. | terminate |
c. | return |
d. | release |
e. | None of these |
12. This function causes a program to terminate, regardless of which function or control mechanism is executing.
a. | exit() |
b. | terminate() |
c. | return() |
d. | continue() |
e. | None of these |
13. Which of the following causes a function to execute?
a. | a for loop |
b. | a do-while loop |
c. | a function prototype |
d. | a function call |
e. | None of these |
14. It is good programming practice to __________ your functions by writing comments that describe what they do.
a. | execute |
b. | document |
c. | retrieve |
d. | create |
e. | None of these |
15. A(n) __________ is information that is passed to a function, and a(n) __________ is information that is received by a function.
a. | function call, function header |
b. | parameter, argument |
c. | argument, parameter |
d. | prototype, header |
e. | None of these |
16. A function _________ eliminates the need to place a function definition before all calls to the function.
a. | header |
b. | prototype |
c. | argument |
d. | parameter |
e. | None of these |
17. A _________ variable is declared outside all functions.
a. | local |
b. | global |
c. | floating-point |
d. | counter |
e. | None of these |
18. If a function is called more than once in a program, the values stored in the function's local variables do not _________ between function calls.
a. | persist |
b. | execute |
c. | communicate |
d. | change |
e. | None of these |
19. A __________ argument is passed to a parameter when the actual argument is left out of the function.
a. | false |
b. | true |
c. | null |
d. | default |
e. | None of these |
20. If a function does not have a prototype, default arguments may be specified in the __________.
a. | function call |
b. | function header |
c. | execution |
d. | return type |
e. | None of these |
21. The value in a __________ variable persists between function calls.
a. | dynamic |
b. | global |
c. | floating-point |
d. | static local |
e. | counter |
22. Given the following function:
void calc (int a, int& b)
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
What is the output of the following code segment that invokes calc():
int x = 1;
int y = 2;
int z = 3;
calc(x, y);
cout << x << " " << y << " " << z << endl;
a. | 1 2 3 |
b. | 1 6 3 |
c. | 3 6 3 |
d. | 1 14 9 |
e. | 2 3 4 5 |
23. EXIT_FAILURE and ________ are named constants that may be used to indicate success or failure when the exit() function is called.
a. | EXIT_TERMINATE |
b. | EXIT_SUCCESS |
c. | EXIT_OK |
d. | RETURN_OK |
24. This is a dummy function that is called instead of the actual function it represents:
a. | main |
b. | stub |
c. | a driver |
d. | an overloaded function |
25. What will the following code display?
#include <iostream>
using namespace std;
void showDub(int);
int main()
{
int x = 2;
showDub(x);
cout << x << endl;
return 0;
}
void showDub(int num)
{
cout << (num * 2) << endl;
}
a. | 2 2 |
b. | 4 2 |
c. | 2 4 |
d. | 4 4 |
26. What will the following code display?
#include <iostream>
using namespace std;
void doSomething(int);
int main()
{
int x = 2;
cout << x << endl;
doSomething(x);
cout << x << endl;
return 0;
}
void doSomething(int num)
{
num = 0;
cout << num << endl;
}
a. | 2 0 2 |
b. | 2 2 2 |
c. | 0 0 0 |
d. | 2 0 0 |
27. What will the following code display?
#include <iostream>
using namespace std;
void doSomething(int&);
int main()
{
int x = 2;
cout << x << endl;
doSomething(x);
cout << x << endl;
return 0;
}
void doSomething(int& num)
{
num = 0;
cout << num << endl;
}
a. | 2 0 2 |
b. | 2 2 2 |
c. | 0 0 0 |
d. | 2 0 0 |
28. Which line in the following program contains the prototype showDub function?
1 #include <iostream>
2 using namespace std;
3 void showDub(int);
4 int main()
5 {
6 int x = 2;
7 showDub(x);
8 cout << x << endl;
9 return 0;
10 }
11 void showDub(int num)
12 {
13 cout << (num * 2) << endl;
14 }
a. | line 3 |
b. | line 4 |
c. | line 7 |
d. | line 11 |
29. Which line in the following program contains the header for the showDub function?
1 #include <iostream>
2 using namespace std;
3 void showDub(int);
4 int main()
5 {
6 int x = 2;
7 showDub(x);
8 cout << x << endl;
9 return 0;
10 }
11 void showDub(int num)
12 {
13 cout << (num * 2) << endl;
14 }
a. | line 3 |
b. | line 4 |
c. | line 7 |
d. | line 11 |
30. Which line in the following program contains a call to the showDub function?
1 #include <iostream>
2 using namespace std;
3 void showDub(int);
4 int main()
5 {
6 int x = 2;
7 showDub(x);
8 cout << x << endl;
9 return 0;
10 }
11 void showDub(int num)
12 {
13 cout << (num * 2) << endl;
14 }
a. | line 3 |
b. | line 4 |
c. | line 7 |
d. | line 11 |
31. What is the data type of the following function prototype's parameter variable?
int myFunction(double);
a. | int |
b. | double |
c. | void |
d. | cannot tell from the prototype |
32. What is the data type of the following function prototype's return value?
int myFunction(double);
a. | int |
b. | double |
c. | void |
d. | cannot tell from the prototype |
33. In the following function prototype, how many parameter variables does this function have?
int myFunction(double, double, double);
a. | 1 |
b. | 2 |
c. | 3 |
d. | cannot tell from the prototype |
34. What will the following code display?
#include <iostream>
using namespace std;
int getValue(int);
int main()
{
int x = 2;
cout << getValue(x) << endl;
return 0;
}
int getValue(int num)
{
return num + 5;
}
a. | 5 |
b. | 2 |
c. | 7 |
d. | getValue(x) |
35. Given the following header for a function named computeValue, which of the following is a valid call to the function?
void computeValue(int value)
a. | computeValue(10) |
b. | computeValue(10); |
c. | void computeValue(10); |
d. | void computeValue(int x); |
MULTIPLE RESPONSE
1. Select all that apply. Which of the following must be included in a function header?
a. | the data type of each parameter |
b. | the data type of the return value |
c. | the name of the function |
d. | the names of parameter variables |
2. Select all that apply. Which of the following statement(s) about global variables is(are) true?
a. | A global variable is accessible only to the main function. |
b. | A global variable is declared in the highest-level block in which it is used. |
c. | A global variable can have the same name as a variable that is declared locally within a function. |
d. | A global variable is the same as a named constant. |
e. | If a function contains a local variable with the same name as a global variable, the global variable's name takes precedence within the function. |