Chapter 03 Flashcards
1
Q
- Assume that the following variables are defined:
int age;
double pay;
char section;
Write a single cin statement that will read input into each of these variables.
A
1.
cin»_space; age»_space; pay»_space; section;
2
Q
- Assume a string object has been defined as follows:
string description;
A) Write a cin statement that reads in a one-word string.
B) Write a statement that reads in a string that can contain multiple words separated
by blanks.
A
A)
cin»_space; description;
B)
getline (cin,description);
3
Q
3. What header files must be included in the following program? int main() { double amount = 89.7; cout << showpoint << fixed; cout << setw(8) << amount << endl; return 0; }
A
#include #include
4
Q
28 / 4 - 2 6 + 12 * 2 - 8 4 + 8 * 2 6 + 17 % 3 - 2 2 + 22 * (9 - 7) (8 + 7) * 2 (16 + 7) % 2 - 1 12 / (10 - 6) (19 - 3) * (2 + 2) / 4
A
5 22 20 6 46 30 0 3 16
5
Q
- Assume a program has the following variable definitions:
int units;
float mass;
double weight;
and the following statement:
weight = mass * units;
Which automatic data type conversion will take place?
A) mass is demoted to an int, units remains an int, and the result of mass * units is
an int.
B) units is promoted to a float, mass remains a float, and the result of mass *
units is a float.
C) units is promoted to a float, mass remains a float, and the result of mass *
units is a double.
A
c
6
Q
7. Assume a program has the following variable de nitions: int a, b = 2; float c = 4.2; and the following statement: a = b * c; What value will be stored in a? A) 8.4 B) 8 C) 0 D) None of the above
A
b
7
Q
- Assume that qty and salesReps are both integers. Use a type cast expression to
rewrite the following statement so it will no longer perform integer division.
unitsEach = qty / salesReps;
A
unitsEach = static_cast(qty)/salesReps;
8
Q
- Rewrite the following variable definition so the variable is a named constant.
int rate;
A
9.
const int RATE = 12;
9
Q
10 Complete the following table by writing statements with combined assignment operators
in the right-hand column. The statements should be equivalent to the statements
in the left-hand column.
x = x + 5; total = total + subtotal; dist = dist / rep; ppl = ppl * period; inv = inv - shrinkage; num = num % 2;
A
x +=5; total +=subtotal; dist/=rep; ppl*=period; inv-=shrinkage; num%=2
10
Q
11. Write a multiple assignment statement that can be used instead of the following group of assignment statements: east = 1; west = 1; north = 1; south = 1;
A
east = west = north = south = 1;
11
Q
- Write a cout statement so the variable divSales is displayed in a field of 8 spaces, in
fixed point notation, with a precision of 2 decimal places. The decimal point should
always be displayed.
A
cout
12
Q
- Write a cout statement so the variable totalAge is displayed in a field of 12 spaces,
in fixed point notation, with a precision of 4 decimal places.
A
13.
cout «_space;setw(12) «_space;fixed
«_space;setprecision(4) «_space;totalAge;
13
Q
- Write a cout statement so the variable population is displayed in a field of 12
spaces, left-justified, with a precision of 8 decimal places. The decimal point should
always be displayed.
A
cout
14
Q
- The __________ library function returns the cosine of an angle.
A
cos
15
Q
- The __________ library function returns the sine of an angle.
A
sin