Friedman/Koffman Chapter 3 Test Bank 9

Chapter 3

Short Answer

1. Why is PI a const in the program that finds the area and circumference of a circle?

Answer: So that it cannot be changed during the run of the program.

2. What is the library function that finds the square root of a number sent to it?

Answer: sqrt

3. What is the value of y after the following code executes:

float y = 3.4 + sqrt (25.0);

Answer: 8.4

4. What is the library function that finds the absolute value of an integer?

Answer: abs

5. What is top-down design?

Answer: A method of solving a problem by breaking it into smaller parts.

6. What is a void function?

Answer: A function that does not return a value.

7. What is the output of this code, given the function definition that follows:

doSomething( );

doSomething( ); // code in main

void doSomething( ) { // function definition

cout < “Hi”;

cout < “Bye”;

}

Answer: HiByeHiBye

8. Why do we use functions?

Answer: To break the program into smaller pieces.

OR

So that parts of our code may be reused.

9. In the function call

doesSomething (7, x, p);

what are 7, x and p called?

Answer: arguments

10. In the function definition

float aValue (int r, int s) {……

what are r and s called?

Answer: parameters

11. In the function prototype

int calculation (float p);

int is called ______?

Answer: the return type

12. What is the output of the following code given the function definition that follows:

cout < myFunction (7, 5, 2); // code in main

int myFunction (int a, int b, int c) // function definition

{

int p = a + 2 * b + c;

return p + 3;

}

Answer: 22

13. The program that you write to test a function is called a(n) ______.

Answer: driver

14. The region in a program where a particular name is visible or can be referenced is called the name’s ______.

Answer: scope

15. What function would you use if you wanted to read a string that might contain a blank?

Answer: getline

16. What would be the output of the following lines of code?

string one = “red”, two = “blue”;

string three = two + “and” + one;

cout < three;

Answer: blueandred

17. Functions like length, at and erase are called ______of the string class.

Answer: member functions

18. What line of code must you put in a program if you want to use the money class?

Answer: #include “money.h”

19. What is the only type of function that does not have to end with a return statement?

Answer: a void function

20. What must a call to a member function always be preceded by?

Answer: an object name (and a dot)

Multiple Choice

1. What is the value of z after the following code executes:

float x, y, z;

x =9.0;

y = 16.0;

z= sqrt ( x + y);

a) 5.0

b) 7.0

c) 337.0

d) 625.0

Answer: A

2. What is the value of x after execution of the following code?

int x, y, z;

y = 3;

z = 4;

x = pow (z, y);

a) 7

b) 12

c) 64

d) 81

Answer: C

3. What is the value of x after the following code is executed?

y = 3.7;

x = floor (y);

a) 3

b) 3.7

c) 4

d) 7

Answer: A

4. A structure chart shows:

a) the syntax of a statement

b) the output of a program

c) the relationships among the subproblems of a problem

d) stick figures on the screen

Answer: C

5. Which of the following is a valid function prototype?

a) float some function ( );

b) void nothing;

c) int (int thing);

d) void something ( );

Answer: D

6. What is the output of this line of code, given the following definition?

display (7, 3); // code in main

void display (int x, int y) { // function definition

cout < y < “ and “ < x;

}

a) 7 and 3

b) 3 and 7

c) 10

d) 7 3

Answer: B

7. What is the output of this line of code, given the following definition?

cout < aNumber (2, 5); // code in main

int aNumber (int x, int y) { // function definition

return (2 * x - y);

}

a) -6

b) -1

c) 6

d) 8

Answer: B

8. What is the value of num after the following code is executed, given the function definition that follows:

float num = findIt(5.0, 2); // code in main

float findIt (float one, int two) // function definition

{

one = one + two;

return (one / two);

}

a) 0.5

b) 2.5

c) 3

d) 3.5

Answer: D

9. What is the output of the following line of code, given the function definition that follows:

cout < calc (1, 2) + calc (2,3); // code in main

int calc (int x, int y) // function definition

{

x = x + 1;

return x % y;

}

a) 0

b) 1

c) 2

d) none of the above

Answer: A

10. Which of the following function calls is valid for the function prototype?

int calculation (int m, char p, float q); // function prototype

a) cout < calculation (3, “yes”, 2.0);

b) cout < calculation (5, p, 4.5);

c) cout < calculation (5, ‘s’);

d) cout < calculation (10, ‘r’, 7.0);

Answer: D

11. Which of the following function calls is valid for the function prototype?

void workItOut (int p, float q); // function prototype

a) cout < workItOut (2, 3.0);

b) workItOut (2, 3);

c) num = workItOut (5, 2.5);

d) workItOut (“p”, 3.7);

Answer: B

12. Which of the following would be the best function prototype for a function that calculates the volume of a box?

a) void volume (int, int, int);

b) int volume (float, float, float);

c) float volume (int, int, int);

d) float volume (float, float, float);

Answer: D

13. What would be the output of the following lines of code?

string word = “somebody”;

cout < word.at(3);

a) m

b) e

c) r

d) d

Answer: B

14. What would be the value of x after the following code has executed?

string words = “How are you?”;

int x = words.length( );

a) 9

b) 10

c) 11

d) 12

Answer: D

15. What is the value of phrase after the following code executes?

string phrase = “A nice day”, exes = “xxxx”;

phrase.replace(2, 3, exes);

a) “A xxxxce day”

b) “Axxxxice day”

c) “A xxxxe day”

d) “Axxxxce day”

Answer: C

True/False

1. To use the sqrt function in a program you should include the cmath library.

Answer: True

2. The function floor (x) rounds its argument x to the nearest integer

Answer: False

3. Function prototypes are placed at the end of a program.

Answer: False

4. Void functions return an integer.

Answer: False

5. One reason for using functions is called procedural abstraction.

Answer: True

6. Void functions may have arguments.

Answer: True

7. Functions are executed in the order in which they are defined.

Answer: False

8. A precondition is a condition that must be true before the function is called.

Answer: True

9. The values in the function data area are kept after the function ends.

Answer: False

10. In the following program outline the name var1 is visible (can be referenced) in the function fun.

int fun (int a, int b);

int main ()

{

int var1;

}

int fun (int a, int b)

{

}

Answer: False

11. In the following program outline the name var1 is visible (can be referenced) in the function fun.

int fun (int a, int b);

const int var1 = 5;

int main ()

{

}

int fun (int a, int b)

{

int c;

}

Answer: True

12. The string class is part of the C++ standard library.

Answer: True

13. The file money.h is called the implementation file.

Answer: False

14. A function prototype must be terminated with a semicolon.

Answer: True

15. It is best to wait until the whole program is written before testing your functions.

Answer: False