ch6 Flashcards

1
Q

CP
16.1 Is the following a function header or a function call?
calcTotal( );

A

6.1 Function call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
6.2 Is the following a function header or a function call?
void showResults( )
A

6.2 Function header

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
6.3 What will the output of the following program be if the user enters 10?
#include 
using namespace std;
void func1( )
{
cout > input;
if (input
A

6.3 I saw Elba

Able was I

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

6.4 The following program skeleton determines whether a person qualifies for a
credit card. To qualify, the person must have worked on his or her current job for
at least two years and make at least $17,000 per year. Finish the program by writing
the definitions of the functions qualify and noQualify. The function
qualify should explain that the applicant qualifies for the card and that the
annual interest rate is 12%. The function noQualify should explain that the
applicant does not qualify for the card and give a general explanation why.
#include
using namespace std;
// You must write definitions for the two functions qualify
// and noQualify.

int main()
{
double salary;
int years;
cout > salary;
cout > years;
if (salary >= 17000.0 && years >= 2)
qualify();
else
noQualify();
return 0;
}
A

6.4 void qualify()
{
cout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
6.5 Indicate which of the following is the function prototype, the function header,
and the function call:
void showNum(double num)
void showNum(double);
showNum(45.67);
A

6.5 Header
Prototype
Function call

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

6.6 Write a function named timesTen. The function should have an integer parameter
named number. When timesTen is called, it should display the product of number
times ten. (Note: just write the function. Do not write a complete program.)

A

6.6 void timesTen(int number)
{
cout

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

6.7 Write a function prototype for the timesTen function you wrote in Question 6.6.

A

6.7 void timesTen(int);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
6.8 What is the output of the following program?
#include 
using namespace std;
void showDouble(int); // Function prototype
int main()
{
int num;
for (num = 0; num
A
6.8 
0     0
1      2
2     4
3     6
4     8
5     10
6     12
7      14
8     16
9     18
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
6.9 What is the output of the following program?
#include 
using namespace std;
void func1(double, int); // Function prototype
int main()
{
int x = 0;
double y = 1.5;
cout
A
6.9 
0 1.5
1.5 0
0 10
0 1.5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

6.10 The following program skeleton asks for the number of hours you’ve worked and
your hourly pay rate. It then calculates and displays your wages. The function
showDollars, which you are to write, formats the output of the wages.
#include
using namespace std;
void showDollars(double); // Function prototype
int main()
{
double payRate, hoursWorked, wages;
cout > hoursWorked;
cout > payRate;
wages = hoursWorked * payRate;
showDollars(wages);
return 0;
}
// You must write the definition of the function showDollars
// here. It should take one parameter of the type double.
// The function should display the message “Your wages are $”
// followed by the value of the parameter. It should be displayed
// with 2 places of precision after the decimal point, in fixed
// notation, and the decimal point should always display.

A

6.10 void showDollars(double amount)
{
cout

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

6.11 How many return values may a function have?

A

6.11 One

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

6.12 Write a header for a function named distance. The function should return a
double and have two double parameters: rate and time.

A

6.12 double distance(double rate, double time)

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

6.13 Write a header for a function named days. The function should return an int
and have three int parameters: years, months, and weeks.

A

6.13 int days(int years, int months, int weeks)

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

6.14 Write a header for a function named getKey. The function should return a char
and use no parameters.

A
6.14 
char getKey( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

6.15 Write a header for a function named lightYears. The function should return a
long and have one long parameter: miles.

A

6.15 long lightYears(long miles)

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

6.16 What is the difference between a static local variable and a global variable

A

6.16 A static local variable’s scope is limited to the function in which it is declared. A global
variable’s scope is the portion of the program beginning at its declaration to the end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
6.17 What is the output of the following program?
#include 
using namespace std;
void myFunc(); // Function prototype
int main()
{
int var = 100;
cout
A

6.17 100
50
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
6.18 What is the output of the following program?
#include 
using namespace std;
void showVar(); // Function prototype
int main()
{
for (int count = 0; count
A
6.18 
10
11
12
13
14
15
16
17
18
19
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

6.19 What kinds of values may be specified as default arguments?

A

6.19 Literals or constants

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

6.20 Write the prototype and header for a function called compute. The function
should have three parameters: an int, a double, and a long (not necessarily in
that order). The int parameter should have a default argument of 5, and the
long parameter should have a default argument of 65536. The double parameter
should not have a default argument.

A
6.20 Prototype:
void compute(double, int = 5, long = 65536);
Header:
void compute(double x, int y, long z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

6.21 Write the prototype and header for a function called calculate. The function
should have three parameters: an int, a reference to a double, and a long (not
necessarily in that order.) Only the int parameter should have a default argument,
which is 47.

A
6.21 Prototype:
void calculate(long, double&, int = 47);
Header:
void calculate(long x, double &y, int z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
6.22 What is the output of the following program?
#include 
using namespace std;
void test(int = 2, int = 4, int = 6);
int main()
{
test();
test(6);
test(3, 9);
test(1, 5, 7);
return 0;
}
void test (int first, int second, int third)
{
first += 3;
second += 6;
third += 9;
cout
A
6.22 
5 10 15
9 10 15
6 15 15
4 11 16
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
6.23 The following program asks the user to enter two numbers. What is the output of
the program if the user enters 12 and 14?
#include 
using namespace std;
void func1(int &, int &);
void func2(int &, int &, int &);
void func3(int, int, int);
int main()
{
int x = 0, y = 0, z = 0;
cout > a >> b;
}
void func2(int &a, int &b, int &c)
{
b++;
c--;
a = b + c;
}
void func3(int a, int b, int c)
{
a = b - c;
}
A
6.23 
0 00
Enter two numbers: 12 14
12 140
14 15-1
14 15-1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
6.24 What is the output of the following program?
#include 
#include 
using namespace std;
void showVals(double, double);
int main()
{
double x = 1.2, y = 4.5;
showVals(x, y);
return 0;
}
void showVals(double p1, double p2)
{
cout
A
  1. 24

1. 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
6.25 What is the output of the following program?
#include 
using namespace std;
int manip(int);
int manip(int, int);
int manip(int, double);
int main()
{
int x = 2, y= 4, z;
double a = 3.1;
z = manip(x) + manip(x, y) + manip(y, a);
cout
A

6.25 30

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
  1. Why do local variables lose their values between calls to the function in which they
    are defined?
A
  1. Because they are created in memory when the function begins execution, and are destroyed
    when the function ends.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
  1. What is the difference between an argument and a parameter variable?
A

argument variable is a variable which is used to send a value to the functions, should not write data type before the argument variable.

parameter variable is used to accept a value into the functions. write data type before it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
  1. Where do you define parameter variables?
A
  1. Inside the parentheses of a function header.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q
4. If you are writing a function that accepts an argument and you want to make sure the
function cannot change the value of the argument, what do you do?
A

pass by value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
  1. When a function accepts multiple arguments, does it matter in what order the arguments
    are passed in?
A
5. Yes. The first argument is passed into the parameter variable that appears first inside the
function header’s parentheses. Likewise, the second argument is passed into the second
parameter, and so on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
  1. How do you return a value from a function?
A

write the keyword return followed by the value you want to return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
  1. What is the advantage of breaking your application s code into several small procedures
A
  1. It makes the program easier to manage. Imagine a book that has a thousand pages, but isn’t divided into chapters or sections. Trying to find a single topic in the book would be very difficult.
    Real-world programs can easily have thousands of lines of code, and unless they are modularized, they can be very difficult to modify and maintain.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
  1. How would a static local variable be useful?
A

static variables are not destroyed when a function returns.they exist for the lifetime of the program even though their scope is only in function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
  1. Give an example where passing an argument by reference would be useful.
A
9. A function such as the following could be written to get user input. The input is stored in the
variables that are passed as arguments.
void getValues(int &x, int &y)
{
cout << "Enter a number: ";
cin >> x;
cout << "Enter another number: ";
cin >> y;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q
  1. The _________ is the part of a function definition that shows the function name,
    return type, and parameter list.
A

function header

36
Q
  1. If a function doesn’t return a value, the word _________ will appear as its return type.
A
  1. void
37
Q
  1. Either a function s _________ or its _________ must precede all calls to the function.
A

function’s definition / prototyope

38
Q
  1. Values that are sent into a function are called _________.
A
  1. arguments
39
Q
  1. Special variables that hold copies of function arguments are called _________.
A

parameters

40
Q
  1. When only a copy of an argument is passed to a function, it is said to be passed by
    _________.
A
  1. value
41
Q
  1. A(n) _________ eliminates the need to place a function definition before all calls to the
    function.
A

a function prototype

42
Q
  1. A(n) _________ variable is defined inside a function and is not accessible outside the
    function.
A
  1. local
43
Q
  1. _________ variables are defined outside all functions and are accessible to any function
    within their scope.
A

global

44
Q
  1. _________ variables provide an easy way to share large amounts of data among all
    the functions in a program.
A
  1. global
45
Q
  1. Unless you explicitly initialize global variables, they are automatically initialized to
    _________.
A

zero

46
Q
  1. If a function has a local variable with the same name as a global variable, only the
    _________ variable can be seen by the function.
A
  1. local
47
Q
  1. _________ local variables retain their value between function calls.
A

static

48
Q
  1. The _________ statement causes a function to end immediately.
A
  1. return
49
Q
  1. _________ arguments are passed to parameters automatically if no argument is
    provided in the function call.
A

default

50
Q
  1. When a function uses a mixture of parameters with and without default arguments,
    the parameters with default arguments must be defined _________.
A
  1. last
51
Q
  1. The value of a default argument must be a(n) _________.
A

a literal value or a named constant

52
Q
  1. When used as parameters, _________ variables allow a function to access the
    parameter s original argument.
A
  1. reference
53
Q
  1. Reference variables are defined like regular variables, except there is a(n) _________ in
    front of the name.
A

ampersand(&)

54
Q
  1. Reference variables allow arguments to be passed by ____________.
A
  1. reference
55
Q
  1. The _________ function causes a program to terminate.
A

exit( )

56
Q
  1. Two or more functions may have the same name, as long as their _________ are
    different.
A
  1. parameter lists
57
Q
32. Examine the following function header, then write an example call to the function.
void showValue(int quantity)
A

Function call

int quantity = 5;
showValue (quantity);
or
showValue(16);

58
Q
  1. The following statement calls a function named half. The half function returns a
    value that is half that of the argument. Write the function.
    result = half(number);
A
  1. double half(double num)
    {
    return num / 2;
    }
59
Q
34. A program contains the following function.
int cube(int num)
{
return num * num * num;
}
Write a statement that passes the value 4 to this function and assigns its return value
to the variable result.
A

n/a

60
Q
  1. Write a function named timesTen that accepts an argument. When the function is
    called, it should display the product of its argument multiplied times 10.
A
  1. void timesTen(int num)
    {
    cout &laquo_space;(num * 10) &laquo_space;endl;
    }
61
Q
36. A program contains the following function.
void display(int arg1, double arg2, char arg3)
{
cout
A

display (age, income,initial);

is the functional call

62
Q
  1. Write a function named getNumber that uses a reference parameter variable to accept
    an integer argument. The function should prompt the user to enter a number in the
    range of 1 through 100. The input should be validated and stored in the parameter
    variable.
A
37. void getNumber(int &amp;num)
{
cout > num;
while (num  100)
{
cout > num;
}
}
63
Q

True or False

38. T F Functions should be given names that reflect their purpose.

A

t

64
Q
  1. T F Function headers are terminated with a semicolon.
A

f

65
Q
  1. T F Function prototypes are terminated with a semicolon.
A

t

66
Q
  1. T F If other functions are defined before main, the program still starts executing
    at function main.
A

t

67
Q
  1. T F When a function terminates, it always branches back to main, regardless of
    where it was called from.
A

f. After function termination it backs to function call where it is called

68
Q
  1. T F Arguments are passed to the function parameters in the order they appear in
    the function call.
A

t

69
Q
  1. T F The scope of a parameter is limited to the function which uses it.
A

t

70
Q
  1. T F Changes to a function parameter always affect the original argument as well.
A

f

71
Q
  1. T F In a function prototype, the names of the parameter variables may be left out.
A

t

72
Q
  1. T F Many functions may have local variables with the same name.
A

t

73
Q
  1. T F Overuse of global variables can lead to problems.
A

t

74
Q
  1. T F Static local variables are not destroyed when a function returns.
A

t

75
Q
  1. T F All static local variables are initialized to 1 by default.
A

f. an static variable is initialized and starts with zero

76
Q
  1. T F Initialization of static local variables only happens once, regardless of how
    many times the function in which they are defined is called.
A

t

77
Q
  1. T F When a function with default arguments is called and an argument is left out,
    all arguments that come after it must be left out as well.
A

t

78
Q
  1. T F It is not possible for a function to have some parameters with default arguments
    and some without.
A

f

79
Q
  1. T F The exit function can only be called from main.
A

f, as exit function csuses program to terminate regardless of which function or control mechanism executing

80
Q
  1. T F A stub is a dummy function that is called instead of the actual function it
    represents.
A

t

81
Q

Find the Errors
Each of the following functions has errors. Locate as many errors as you can.
56. void total(int value1, value2, value3)
{
return value1 + value2 + value3;
}

A
corrected:
int total( int value1, int value2, int value3)
{
return value1+value2+value3;
}
82
Q
57. double average(int value1, int value2, int value3)
{
double average;
average = value1 + value2 + value3 / 3;
}
A
  1. The assignment statement should read:
    average = (value1 + value2 + value3) / 3.0;
    The function is defined as a double but returns no value.
83
Q
  1. void area(int length = 30, int width)
    {
    return length * width;
    }
A
correct:
int area ( int length = 30, int width)
{
return length*width;
}
84
Q
59. void getValue(int value&)
{
cout << "Enter a value: ";
cin >> value&;
}
A
  1. The parameter should be defined as:
    int &value
    The cin statement should read:
    cin&raquo_space; value;
85
Q
60. (Overloaded functions)
int getValue()
{
int inputValue;
cout > inputValue;
return inputValue;
}
double getValue()
{
double inputValue;
cout > inputValue;
return inputValue;
}
A

see