Chapter 02 Flashcards

1
Q
2.1 The following C++ program will not compile because the lines have been mixed
up.
int main()
}
// A crazy mixed up program
return 0;
#include 
cout
A
// A crazy mixed up program
#include 
using namespace std;
int main()
{
cout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
2.5 Examine the following program.
// This program uses variables and literals.
#include 
using namespace std;
int main()
{
int little;
int big;
little = 2;
big = 2000;
cout
A
2.5 Variables:
little
and
big
.
Constants: 2, 2000, “The little number is ”, “The big number is ”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

2.4 On paper, write a program that will display your name on the first line, your street
address on the second line, your city, state, and ZIP code on the third line, and
your telephone number on the fourth line. Place a comment with today s date at
the top of the program. Test your program by entering, compiling, and running it.

A
2.4
// Today's Date: September 3, 2012
#include 
using namespace std;
int main()
{
cout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
2.3 Study the following program and show what it will print on the screen.
// The Works of Wolfgang
#include 
using namespace std;
int main()
{
cout
A
2.3
The works of Wolfgang
include the following
The Turkish March
and Symphony No. 40 in G minor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
2.6 What will the following program display on the screen?
#include 
using namespace std;
int main()
{
int number;
number = 712;
cout
A

2.6 The value is number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
2.7 Which of the following are illegal variable names, and why?
x
99bottles
july97
theSalesFigureForFiscalYear98
r&d
grade_report
A

2.7
99bottles
: Variable names cannot begin with a number.
r&d
: Variable names may only use alphabetic letters, digits, or underscores

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

2.8 Is the variable name Sales the same as sales? Why or why not?

A

2.8 No. Variable names are case sensitive.

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

2.9 Refer to the data types listed in Table 2-6 for these questions.
A) If a variable needs to hold numbers in the range 32 to 6,000, what data type
would be best?
B) If a variable needs to hold numbers in the range 40,000 to +40,000, what
data type would be best?
C) Which of the following literals uses more memory? 20 or 20L

A
2.9 A)
short, or unsigned short
.
B)
int
C) They both use the same amount of memory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

2.10 On any computer, which data type uses more memory, an integer or an unsigned
integer?

A

2.10 They both use the same amount of memory.

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

2.11 What are the ASCII codes for the following characters? (Refer to Appendix B)
C
F
W

A

2.11 67, 70, 87

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

2.12 Which of the following is a character literal?
‘B’
“B”

A

2.12 ‘B’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
2.13 Assuming the char data type uses 1 byte of memory, how many bytes do the following
literals use?
'Q'
"Q"
"Sales"
'\n'
A

2.13 ‘Q’ uses one byte
“Q” uses two bytes
“Sales” uses six bytes
‘\n’ uses one byte

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 14 Write a program that has the following character variables: first, middle, and
    last. Store your initials in these variables and then display them on the screen.
A
2.14
#include 
using namespace std;
int main()
{
char first, middle, last;
first = 'T';
middle = 'E';
last = 'G';
cout << first << " " << middle << " " << last << endl;
return 0;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

2.15 What is wrong with the following program statement?

char letter = “Z”;

A

2.15 The string constant “Z” is being stored in the character variable
letter

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

2.16 What header file must you include in order to use string objects?

A

2.16 The
string
header file

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

2.17 Write a program that stores your name, address, and phone number in three separate
string objects. Display the contents of the string objects on the screen.

A
2.17
#include 
#include 
using namespace std;
int main()
{
string name = "John Smith";
string address = "224 Maple Street\nClyde, NC 28721";
string phone = "555-5050";
cout << name << endl;
cout << address << endl;
cout << phone << endl << endl;
return 0;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

2.18 Yes or No: Is there an unsigned floating point data type? If so, what is it?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. 19 How would the following number in scienti c notation be represented in E notation?
  2. 31 * 10^17
A

2.19 6.31E17

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

2.21 Is the following assignment statement valid or invalid? If it is invalid, why?
72 = amount;

A

2.21 Invalid. The value on the left of the = operator must be an lvalue.

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

2.22 How would you consolidate the following definitions into one statement?
int x = 7;
int y = 16;
int z = 28;

A

2.22

int x = 7, y = 16, z = 28;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
2.23 What is wrong with the following program? How would you correct it?
#include 
using namespace std;
int main()
{
number = 62.7;
double number;
cout << number << endl;
return 0;
}
A
2.23 The variable
number
is assigned a value before it is defined. Correct the program by
moving the statement
number = 62.7;
to the point after the variable declaration. Here is
the corrected program:
#include 
using namespace std;
int main()
{
double number;
number = 62.7;
cout << number << endl;
return 0;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

2.24 Is the following an example of integer division or floating-point division? What
value will be stored in portion?
portion = 70 / 3;

A

2.24 Integer division. The value 23 will be stored in

portion

23
Q

2.25 Write statements using the const quali er to create named constants for the following
literal values:
Literal Value Description
2.71828 Euler s number (known in mathematics as e)
5.256E5 Number of minutes in a year
32.2 The gravitational acceleration constant (in feet per second2)
9.8 The gravitational acceleration constant (in meters per second2)
1609 Number of meters in a mile

A
2.25
const float E = 2.71828;
const int MINUTES_IN_A_YEAR = 5.256E5;
const float G_FEET = 32.2;
const float G_METERS = 9.8;
const int METERS_IN_A_MILE = 1609
24
Q
  1. How many operands does each of the following types of operators require?
    _______ Unary
    _______ Binary
    _______ Ternary
A

1
2
3

25
Q
  1. How may the float variables temp, weight, and age be defined in one statement?
A

float temp, weight, age;

26
Q
  1. How may the int variables months, days, and years be defined in one statement,
    with months initialized to 2 and years initialized to 3?
A

int months=2, days, years=3;

27
Q
  1. Write assignment statements that perform the following operations with the variables
    a, b, and c.
    A) Adds 2 to a and stores the result in b.
    B) Multiplies b times 4 and stores the result in a.
    C) Divides a by 3.14 and stores the result in b.
    D) Subtracts 8 from b and stores the result in a.
    E) Stores the value 27 in a.
    F) Stores the character K in c.
    G) Stores the ASCII code for B in c.
A
A) b=a + 2;
B) a = 4 * b;
C) b = a/3.14;
D) a = b - 8;
E) a= 27;
F) c = 'K';
G) char c;
     c = 'B';
28
Q
5. Is the following comment written using single-line or multi-line comment symbols?
/* This program was written by M. A. Codewriter*/
A

multi

29
Q
6. Is the following comment written using single-line or multi-line comment symbols?
// This program was written by M. A. Codewriter
A

sigle

30
Q
7. Modify the following program so it prints two blank lines between each line of text.
#include 
using namespace std;
int main()
{
cout
A
7.
#include 
int main()
{
cout
31
Q
8. What will the following programs print on the screen?
A) #include 
using namespace std;
int main()
{
int freeze = 32, boil = 212;
freeze = 0;
boil = 100;
cout
A

0

100

32
Q
B) #include 
using namespace std;
int main()
{
int x = 0, y = 2;
x = y * 4;
cout << x << endl << y << endl;
return 0;
}
A

8

2

33
Q
C) #include 
using namespace std;
int main()
{
cout << "I am the incredible";
cout << "computing\nmachine";
cout << "\nand I will\namaze\n";
cout << "you.";
return 0;
}
A
I am the incredible computing
machine
and i will
amaze
you
34
Q
D) #include 
using namespace std;
int main()
{
cout << "Be careful\n";
cout << "This might/n be a trick ";
cout << "question\n";
return 0;
}
A

Be careful

This might/n be a trick question

35
Q
E) #include 
using namespace std;
int main()
{
int a, x = 23;
a = x % 2;
cout << x << endl << a << endl;
return 0;
}
A

23

1

36
Q
Multiple Choice
9. Every complete statement ends with a
A) period
B) # symbol
C) semicolon
D) ending brace
A

c

37
Q
10. Which of the following statements is correct?
A) #include (iostream)
B) #include {iostream}
C) #include 
D) #include [iostream]
E) All of the above
A

c

38
Q
11. Every C++ program must have a
A) cout statement.
B) function main.
C) #include statement.
D) All of the above
A

b

39
Q
12. Preprocessor directives begin with a
A) #
B) !
C) <
D) *
E) None of the above
A

a

40
Q
13. The following data
72
'A'
"Hello World"
2.8712
are all examples of
A) Variables
B) Literals or constants
C) Strings
D) None of the above
A

b

41
Q
14. A group of statements, such as the contents of a function, is enclosed in
A) Braces {}
B) Parentheses ()
C) Brackets <>
D) All of the above will do
A

a

42
Q
15. Which of the following are not valid assignment statements? (Circle all that apply.)
A) total = 9;
B) 72 = amount;
C) profit = 129
D) letter = 'W';
A

bc

43
Q
  1. Which of the following are not valid cout statements? (Circle all that apply.)
    A) cout
A

bcd

44
Q
17. Assume w = 5, x = 4, y = 8, and z = 2. What value will be stored in result in each of
the following statements?
A) result = x + y;
B) result = z * 2;
C) result = y / x;
D) result = y - z;
E) result = w % 2;
A
A) 12
B)4
C)2
D)6
E)1
45
Q
18. How would each of the following numbers be represented in E notation?
A) 3.287 * 10^6
B) -978.65 * 10^12
C) 7.65491 * 10^-3
D) -58710.23 * 10^-4
A

3.287E6
-9.7865E14
765491E-3
-5.871023E0

46
Q
19. The negation operator is
A) Unary
B) Binary
C) Ternary
D) None of the above
A

A

47
Q
20. A(n) \_\_\_\_\_\_\_\_\_\_\_ is like a variable, but its value is read-only and cannot be changed
during the program s execution.
A) secure variable
B) uninitialized variable
C) named constant
D) locked variable
A

C

48
Q
  1. When do preprocessor directives execute?
    A) Before the compiler compiles your program
    B) After the compiler compiles your program
    C) At the same time as the compiler compiles your program
    D) None of the above
A

A

49
Q
  1. T F A variable must be defined before it can be used.
A

T

50
Q
  1. T F Variable names may begin with a number.
A

f

51
Q
  1. T F Variable names may be up to 31 characters long.
A

t

52
Q
  1. T F A left brace in a C++ program should always be followed by a right brace
    later in the program.
A

t

53
Q
  1. T F You cannot initialize a named constant that is declared with the const
    modifier.
A

f