Chapter 06 Starting out with C++ Functions
Download file with the answers
Chapter 06 Starting out with C++ Functions
1 file(s) 149.51 KB
Not a member!
Create a FREE account here to get access and download this file with answers
Chapter Six
MULTIPLE CHOICE
1. This is a collection of statements that performs a specific task.
a. infinite loop
b. variable
c. constant
d. function
e. None of these
2. A function __________ contains the statements that make up the function.
a. definition
b. prototype
c. call
d. expression
e. parameter list
3. A function can have zero to many parameters, and it can return this many values.
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. In a function header, you must furnish
a. data type(s) of the parameters
b. data type of the return value
c. the name of function
d. names of parameter variables
e. All of these
6. Functions are ideal for use in menu-driven programs. When a user selects a menu item, the program can ________ the appropriate function.
a. call
b. prototype
c. define
d. declare
e. None of these
7. 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
8. The value in this type of local variable persists between function calls.
a. global
b. internal
c. static
d. dynamic
e. None of these
9. 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. Relational
e. None of these
10. 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
11. This statement causes a function to end.
a. end
b. terminate
c. return
d. release
e. None of these
12. _________ functions may have the same name, as long as their parameter lists are different.
a. Only two
b. Two or more
c. Zero
d. Un-prototyped
e. None of these
13. This function causes a program to terminate, regardless of which function or control mechanism is executing.
a. terminate()
b. return()
c. continue()
d. exit()
e. None of these
14. Given the following function definition
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 fragment that invokes calc?
(All variables are of type int)
x = 1;
y = 2;
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. None of these
15. This is a statement that causes a function to execute.
a. for loop
b. do-while loop
c. function prototype
d. function call
e. None of these
16. It is a good programming practice to ____________ your functions by writing comments that describe what they do.
a. execute
b. document
c. eliminate
d. prototype
e. None of these
17. 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
18. Which of the following statements about global variables is 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. 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.
e. All of these are true
19. 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
20. A ___________ variable is declared outside all functions.
a. local
b. global
c. floating-point
d. counter
e. None of these
21. 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
22. A(n) _________ argument is passed to a parameter when the actual argument is left out of the function call.
a. false
b. true
c. null
d. default
e. None of these
23. If a function does not have a prototype, default arguments may be specified in the function ___________.
a. call
b. header
c. execution
d. return type
e. None of these
24. 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
e. None of these
25. The value in a(n) _______ variable persists between function calls.
a. dynamic
b. local
c. counter
d. static local
26. This is a dummy function that is called instead of the actual function it represents.
a. main function
b. stub
c. driver
d. overloaded function
27. What is the output of the following program?
#include
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
c. 2
4
b. 4
2
d. 4
4
28. What is the output of the following program?
#include
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
c. 0
0
0
b. 2
2
2
d. 2
0
0
29. What is the output of the following program?
#include
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
c. 0
0
0
b. 2
2
2
d. 2
0
0
30. Which line in the following program contains the prototype for the showDub function?
1 #include
2 using namespace std;
3
4 void showDub(int);
5
6 int main()
7 {
8 int x = 2;
9
10 showDub(x);
11 cout << x << endl;
12 return 0;
13 }
14
15 void showDub(int num)
16 {
17 cout << (num * 2) << endl;
18 }
a. 4 c. 10
b. 6 d. 15
31. Which line in the following program contains the header for the showDub function?
1 #include
2 using namespace std;
3
4 void showDub(int);
5
6 int main()
7 {
8 int x = 2;
9
10 showDub(x);
11 cout << x << endl;
12 return 0;
13 }
14
15 void showDub(int num)
16 {
17 cout << (num * 2) << endl;
18 }
a. 4 c. 10
b. 6 d. 15
32. Which line in the following program contains a call to the showDub function?
1 #include
2 using namespace std;
3
4 void showDub(int);
5
6 int main()
7 {
8 int x = 2;
9
10 showDub(x);
11 cout << x << endl;
12 return 0;
13 }
14
15 void showDub(int num)
16 {
17 cout << (num * 2) << endl;
18 }
a. 4 c. 10
b. 6 d. 15
33. Look at the following function prototype.
int myFunction(double);
What is the data type of the funtion’s parameter variable?
a. int c. void
b. double d. Can’t tell from the prototype
34. Look at the following function prototype.
int myFunction(double);
What is the data type of the funtion’s return value?
a. int c. void
b. double d. Can’t tell from the prototype
35. Look at the following function prototype.
int myFunction(double, double, double);
How many parameter variables does this function have?
a. 1 c. 3
b. 2 d. Can’t tell from the prototype
36. What is the output of the following program?
#include
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 c. 7
b. 2 d. “getValue(x)”
37. Here is the header for a function named computeValue:
void computeValue(int value)
Which of the following is a valid call to the function?
a. computeValue(10)
b. computeValue(10);
c. void computeValue(10);
d. void computeValue(int x);
TRUE/FALSE
1. True/False: When a function is called, flow of control moves to the function’s prototype.
2. True/False: A parameter is a special-purpose variable that is declared inside the parentheses of a function definition.
3. True/False: A local variable and a global variable may not have the same name within the same program.
4. True/False: A static variable that is defined within a function is initialized only once, the first time the function is called.
5. True/False: It is possible for a function to have some parameters with default arguments and some without.
6. True/False: A function’s return data type must be the same as the function’s parameter(s).
7. True/False: One reason for using functions is to break programs into manageable units, or modules.
8. True/False: You must furnish an argument with a function call.
9. True/False: Global variables are initialized to zero by default.
10. True/False: Local variables are initialized to zero by default.
11. True/False: It is not considered good programming practice to declare all of your variables globally.
12. True/False: You may use the exit() function to terminate a program, regardless of which control mechanism is executing.
Leave a reply