Chapter 8
SCOPE, LIFETIME, AND MORE ON FUNCTIONS
QUESTIONS
True/False
1. The scope of a local identifier extends from the point of declaration to the end of the block in which it is declared.
2. Using global variables is better style than using local variables.
3. The scope of a formal parameter is identical to the scope of a local variable declared in the outermost block of the function body.
4. The scope of an identifier does not include any nested block that contains a locally declared identifier with the same name.
5. The lifetime of a variable is a compile-time issue, whereas the scope of a variable is a run-time issue.
6. If the declaration
char firstInitial;
appears within a block, then
firstInitial is an automatic variable.7. The declaration
int age = 19;
is valid in C++.
8. Assigning a value directly to a global variable from within a function is an example of a side effect.
9. Using globally declared named constants is as dangerous as using globally declared variables.
10. Value-returning functions can only be used to return numeric values.
11. The statement
return 3 * alpha + 8;
is valid in a value-returning function but not in a void function.
12. A value-returning function that modifies the caller's actual parameters is an example of a function with side effects.
13. Given the function definition
float SomeFunc( /* in */ int n )
{
...
return x;
}
a compile-time error occurs if the data type of
x is different from float.14. The function
int SomeFunc( int delta )
{
delta++;
return 3 * delta;
}
has a side effect.
15. If one programmer in a large team is given the task of writing a single function for the team project, this programmer is more likely to need a function driver than a function stub.
16. If a programmer has developed a large portion of a complete project and wants to test the developed portion before implementing the remaining functions, this programmer is more likely to need function drivers than function stubs.
Multiple Choice
17. If a variable
a. a global variable or a formal parameter of
F.b. a local variable within
F or a formal parameter of F.c. a global variable or an actual parameter to
F.d. a local variable within
F or an actual parameter to F.18. In C++ terminology, the statement
float windSpeed;
a. is a declaration but not a definition of
b. is a definition but not a declaration of
windSpeed.c. is both a declaration and a definition of
windSpeed.d. is neither a declaration nor a definition of
windSpeed.19. Suppose the first few lines of a function are as follows:
void Calc( /* in */ float beta )
{
alpha = 3.8 * beta;
Then the variable
alpha isa. a local variable.
b. a global variable.
c. a formal parameter.
d. an actual parameter.
e. none of the above
20. What is the output of the following code fragment? (All variables are of type
alpha = 3;
beta = 20;
if (beta > 10)
{
int alpha = 5;
beta = beta + alpha;
cout << alpha << ' ' << beta << endl;
}
cout << alpha << ' ' << beta << endl;
a.
3 20b.
3 253 25
c.
5 255 25
d.
5 253 25
e.
5 253 20
21. Given the function definition
void SomeFunc( ... )
{
float alpha;
...
}
which of the following statements about
alpha is false?a. The memory allocated to
b. A formal parameter in the function heading can also be named
alpha.c. The value of
alpha is undefined at the moment control enters the function.d.
alpha cannot be accessed directly from code outside the function.22. 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.
d.
If a function contains a local variable with the same name as a global variable, the global variable takes precedence.
23. What is the output of the following program?
#include <iostream.h>
void Try( int&, int );
int x;
int y;
int z;
int main()
{
x = 1;
y = 2;
z = 3;
Try(y, x);
cout << x << ' ' << y << ' ' << z << endl;
return 0;
}
void Try( int& a,
int b )
{
int x;
x = a + 2;
a = a * 3;
b = x + a;
}
a.
10 6 3b.
10 2 3c.
1 2 3d.
1 6 3e. none of the above
24. In the following function, the declaration of
beta includes an initialization.void SomeFunc( int alpha )
{
int beta = 25;
...
}
Which of the following statements about
beta is false?a. It is initialized once only, the first time the function is called.
b. It is initialized each time the function is called.
c. It cannot be reassigned a different value within the function.
d. a and c above
e. b and c above
25. In the following function, the declaration of
beta includes an initialization.void SomeFunc( int alpha )
{
static int beta = 25;
...
}
Which of the following statements about
beta is false?a. It is initialized once only, the first time the function is called.
b. It is initialized each time the function is called.
c. It cannot be reassigned a different value within the function.
d. a and c above
e. b and c above
26. Given the function definition
void Test( /* in */ int alpha )
{
static int n = 5;
n = n + alpha;
cout << n << ' ';
}
what is the output of the following code? (Assume that
Test has not been called previously.)Test(20);
Test(30);
a.
20 30b.
25 35c.
20 50d.
25 55e.
25 60
27. Given the function definition
int Mystery( /* in */ float someVal )
{
if (someVal > 2.0)
return 3 * int(someVal);
else
return 0;
}
what is the value of the expression
Mystery(4.2) ?a. 12
b. 12.0
c. 0
d. 0.0
e. nothing--the function call is invalid
28. Given the function definition
int Trans( /* in */ int alpha,
/* in */ int beta )
{
if (alpha > beta)
return alpha + 10;
else
return 2 * beta;
}
what is printed by the following code?
cout << Trans(5, Trans(9, 4)) << endl;
a.
15b.
38c.
16d.
19e.
829. Given the function definition
Boolean IsZip( /* in */ float someFloat )
{
return (someFloat == 0.0);
}
what is the value of the expression
IsZip(2.4) ?a.
0.0b.
TRUEc.
2.4d.
FALSEe.
"someFloat == 0.0"30. Given the function prototype
Boolean IsGreater( int, int );
which of the following statements use valid calls to the
IsGreater function? (The data types of the variables are suggested by their names.)a.
someBoolean = IsGreater(someInt, 8);b.
if (IsGreater(5, someInt))intCounter++;
c.
while (IsGreater(inputInt, 23))cin >> inputInt;
d. b and c above
e. a, b, and c above
31. What happens if a value-returning function with the prototype
float Average( int, int, int);
is called by using the following statement? (
alpha and beta are int variables.)Average(alpha, 34, beta);
a. The compiler issues a syntax error message.
b. The function is executed, and the function value is discarded.
c. The function is executed, and the function value is assigned to
d. The function is not executed, and the program halts with a run-time error message.
32. The function heading
float TenToThePower( /* in */ int n )
is for a function that returns 10.0 raised to any integer power. Which of the following statements stores into
someFloat the value 10.0 raised to the power someInt?a.
TenToThePower(someFloat, someInt);b.
TenToThePower(someInt);c.
TenToThePower(someInt) = someFloat;d.
someFloat = TenToThePower(someInt);e.
someInt = TenToThePower(someFloat);33. The function heading
float TenToThePower( /* in */ int n)
is for a function that returns 10.0 raised to any integer power. Which of the following statements stores into
someFloat the value 10.0 raised to the power someInt?a.
someFloat = 4.96 * TenToThePower(8) + 2.5;b.
TenToThePower(6);c.
if (TenToThePower(someInt) > someFloat)beta = 3;
d. a and b above
e. a and c above
34. For the function definition
int SomeFunc( /* in */ int alpha,
/* in */ int beta )
{
int gamma;
alpha = alpha + beta;
gamma = 2 * alpha;
return gamma;
}
what is the function postcondition?
a.
// Postcondition: gamma == 2*alphab.
// Postcondition: alpha == alpha@entry + beta && gamma == 2*alphac.
// Postcondition: Function value == gammad.
// Postcondition: Function value == 2*alphae.
// Postcondition: Function value == 2*(alpha@entry + beta)
35. Given the function prototype
int Top( int, int );
which of the following statements contain valid calls to the
Top function?a.
someInt = 4 + Top(oneInt, anotherInt);b.
cin >> Top(oneInt, anotherInt);c.
cout << Top(5, Top(3, 4));d.
a and c abovee.
a, b, and c above36. If a module is supposed to compute the average of five numbers, which is more appropriate to use--a value-returning function or a void function?
a.
a value-returning functionb.
a void function37. If a module is supposed to convert five values measured in inches to their equivalent measures in centimeters, which is more appropriate to use--a value-returning function or a void function?
a. a value-returning function
b. a void function
38. If a module is supposed to print a line of asterisks of a given length, which is more appropriate to use--a value-returning function or a void function?
a. a value-returning function
b. a void function
39. What is the appropriate function prototype for a function that receives a character letter grade and returns its integer equivalent on a four-point grading scale?
a.
void IntEquiv( char );b.
void IntEquiv( int );c.
int IntEquiv( char );d.
int IntEquiv( char& );e.
char IntEquiv( int );40. Which of the following could cause an unexpected side effect?
a. modifying a global variable
b. changing the value of a value parameter
c. referencing a global constant
d. declaring an incoming-only parameter to be a reference parameter
e. a and d above
41.
Using the library functions available through the header file ctype.h, which of the following can be used to determine if the variable someChar contains either a digit character or a lowercase letter?a.
if (isalnum(someChar) || isdigit(someChar))b.
if (isalpha(someChar) || isdigit(someChar))c.
if (isalnum(someChar) && !isupper(someChar))d.
if (islower(someChar) || isdigit(someChar))e.
c and d above42. This question demonstrates the hazards of side effects. Given the function definition
int Power( /* in */ int& base,
/* in */ int& exp )
{
int product = 1;
while (exp >= 1)
{
product = product * base;
exp--;
}
return product;
}
what is the output of the following code? (All variables are of type
int.)n = 2;
pow = 3;
result = Power(n, pow);
cout << n << " to the power " << pow << " is " << result;
a.
2 to the power 3 is 8b.
2 to the power 0 is 8c.
0 to the power 0 is 0d.
2 to the power 3 is 143. The function
float Distance( /* in */ float velocity,
/* in */ float angle,
/* in */ float resistance )
{
cout << "Distance was called. Returning 48.5" << endl;
return 48.5;
}
is an example of:
a. a driver
b. a plug
c. a stub
d. a void function
Fill-In
44. The ____________________ of an identifier is the region of program code where it is legal to reference that identifier.
45. ____________________ rules determine where in the program an identifier can be accessed, given the point where that identifier is declared.
46. The ____________________ of a variable is the period of time during program execution when the variable has memory allocated to it.
47. A(n) ____________________ variable is one for which memory is allocated and deallocated when control enters and exits the block in which it is declared.
48. A(n) ____________________ variable is one for which memory remains allocated throughout the execution of the entire program.
49. A(n) ____________________ is any effect of one function on another that is not a part of the explicitly defined interface between them.
50. A(n) ____________________ is a dummy function that is included for testing the higher-level code.
51. A(n) ____________________ is a simple
main function that is used to call a function being tested.