Ch.5 Flashcards

1
Q

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);

A

someFunction(1, 2.0, ch);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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;

}

A

The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A simplified version of a function which is used to test the main program is called

A

a stub

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Testing your program should be done

A

as each function is developed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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;

}

}

A

Function definitions may not be nested.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When a void function is called, it is known as

A

an executable statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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;

A

1 14 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If you were to write a function for displaying the cost of an item to the screen, which function prototype would be most appropriate?

A

void display(float myCost);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In the following function, what is passed to the first parameter?

void f1( int& value1, int value2);

int x,y;

f1(x,y);

A

the variable x (or its memory location)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following are true?

A) As long as the function is defined anywhere in your program, it can be used anywhere else.

B) A function definition can contain another function definition.

C) A function can call another function.

D) If you have function prototypes, the order in which you define your functions is important.

A

A function can call another function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the following function definitions and program fragments, what is the output?

void f1(int& z, int &q)

{

int temp;

temp = q;

q = z;

z = temp;

}

void f2( int& a, int& b)

{

if( a < b)

f1(a,b);

else

a = b;

}

int x = 3, y = 4;

f2(y,x);

cout << x <<” “ << y << endl;

A

3 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A simplified main program used to test functions is called

A

a driver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Call-by-reference parameters are passed

A

the actual argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you need a function to get both the number of items and the cost per item from a user, which would be a good function declaration to use?

A

void getData(int& count, float& cost);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The postcondition of a function

A

tells what will be true after the function executes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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;

}

A

The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;

17
Q

You should make a parameter a reference parameter if

A

you need the function to change the value of the argument passed to the function.

18
Q

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;

}

A

99

19
Q

If you write a function that should use call-by-reference, but forget to include the ampersand

A

the program will run with incorrect results

20
Q

Which of the following is true for a void function?

A

Nothing is returned

21
Q

Using functions in a program is called

A

Procedural Abstraction

22
Q

The values or variables listed in the function declaration are called ________ to the function

A

(formal) parameters

23
Q

When the address of the actual argument is passed to the formal parameter, this is called

A

call-by-reference –or- pass-by-reference

24
Q

A ________ is a main program that only checks that functions execute correctly

A

driver

25
Q

What type of value does a void function return?

A

nothing

26
Q

A function that does not return a value is known as a

A

void

27
Q

What symbol is used to signify that a parameter is a reference parameter?

A

ampersand (&)

28
Q

What is the correct way to call the following function?

void setDisplay( );

A

Set Display();

29
Q

A ________ is a simplified version of a function used to test the main program.

A

stub

30
Q

Pre and post conditions for a function should be written (before/after) the function definition is written

A

before

31
Q

It is acceptable to have both call-by-value and call-by-reference parameters in the same function declaration

A

True

32
Q

A void function may not be used in an output statement.

A

TRUE

33
Q

Functions can return at most one value.

A

TRUE

34
Q

In a function with call-by-reference parameters, the values of the actual arguments are passed to the function

A

FALSE

35
Q

It is illegal to call other functions from inside a function definition

A

FALSE