Introduction to C++ Programming, Y. Daniel Liang
Chapter 2 Primitive Data Types and Operations
Section 2.3 Identifiers
1
Is every letter in a C++ keyword in lowercase?
A.
true
B.
false
2
Which of the following is a valid identifier?
A.
_343
B.
class
C.
9X
D.
8+9
E.
radius
Section 2.4 Variables
3
Which of the following are correct names for variables according to the naming conventions adopted by this book?
A.
radius
B.
Radius
C.
RADIUS
D.
findArea
E.
FindArea
4
Which of the following are correct ways to declare variables?
A.
int length; int width;
B.
int length, width;
C.
int length; width;
D.
int length, int width;
Section 2.5 Assignment Statements and Assignment Expressions
5
____________ is the assignment operator.
A.
==
B.
:=
C.
=
D.
=:
6
To assign a value 1 to variable x, you write
A.
1 = x;
B.
x = 1;
C.
x := 1;
D.
1 := x;
E.
x == 1;
7
which of the following assignment statements are incorrect.
A.
i = j = k = 1;
B.
i = 1; j = 1; k = 1;
C.
i = 1 = j = 1 = k = 1;
D.
i == j == k == 1;
8
To declare and initialize an int variable i, use
A.
int i = 1;
B.
int i(1);
C.
int i == 1;
D.
int i(1.0);
Section 2.6 Constants
9
To declare a constant MAX_LENGTH inside a function with value 99.98, you write
A.
const MAX_LENGTH = 99.98;
B.
const float MAX_LENGTH = 99.98;
C.
double MAX_LENGTH = 99.98;
D.
const double MAX_LENGTH = 99.98;
10
Which of the following is a constant, according to the naming conventions adopted by this book?
A.
MAX_VALUE
B.
Test
C.
read
D.
ReadInt
E.
COUNT
11
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159.
A.
variables
B.
functions
C.
constants
D.
classes
Section 2.7 Numeric Data Types and Operations
12
Which of these data type requires the most amount of memory?
A.
long
B.
int
C.
short
D.
double
13
What is result of 45 / 4?
A.
10
B.
11
C.
11.25
D.
12
14
Which of the following expression will yield 0.5?
A.
1 / 2
B.
1.0 / 2
C.
(double) (1 / 2)
D.
(double) 1 / 2
E.
1 / 2.0
15
Which of the following expression results in a value 1?
A.
2 % 1
B.
15 % 4
C.
25 % 5
D.
37 % 6
16
25 % 1 is _____
A.
1
B.
2
C.
3
D.
4
E.
0
17
-25 % 5 is _____
A.
1
B.
2
C.
3
D.
4
E.
0
18
24 % 5 is _____
A.
1
B.
2
C.
3
D.
4
E.
0
19
-24 % 5 is _____
A.
-1
B.
-2
C.
-3
D.
-4
E.
0
20
-24 % -5 is _____
A.
3
B.
-3
C.
4
D.
-4
E.
0
21
To add a value 1 to variable x, you write
A.
1 + x = x;
B.
x += 1;
C.
x := 1;
D.
x = x + 1;
E.
x = 1 + x;
22
To add number to sum, you write (Note: C++ is case-sensitive)
\
A.
number += sum;
B.
number = sum + number;
C.
sum = Number + sum;
D.
sum += number;
E.
sum = sum + number;
23
Analyze the following code.
  #include <iostream>
  using namespace std;
  int main()
  {
    int month = 09;
    cout << "month is " << month;
  }
A.
The program displays month is 09
B.
The program displays month is 9
C.
The program displays month is 9.0
D.
The program has a syntax error, because 09 is an incorrect literal value.
24
What is y displayed in the following code?
  #include <iostream>
  using namespace std;
  int main()
  {
    int x = 1;
    int y = x = x + 1;
    cout << "y is " << y;
  }
A.
y is 0.
B.
y is 1 because x is assigned to y first.
C.
y is 2 because x + 1 is assigned to x and then x is assigned to y.
D.
The program has a syntax error since x is redeclared in the statement int y = x = x + 1.
Section 2.8 Numeric Type Conversions
25
To assign a double variable d to an float variable x, you write
A.
x = (long)d
B.
x = (int)d;
C.
x = d;
D.
x = (float)d;
26
What is the printout of the following code:
double x = 5.5;
int y = (int)x;
cout << "x is " << x << " and y is " << y;
A.
x is 5 and y is 6
B.
x is 6.0 and y is 6.0
C.
x is 6 and y is 6
D.
x is 5.5 and y is 5
E.
x is 5.5 and y is 5.0
27
Which of the following assignment statements is legal?
A.
float f = -34;
B.
int t = 23;
C.
short s = 10;
D.
int t = (int)false;
E.
int t = 4.5;
28
What is the value of (double)5/2?
A.
2;
B.
2.5;
C.
3;
D.
2.0;
E.
3.0;
29
What is the value of (double)(5/2)?
A.
2;
B.
2.5;
C.
3;
D.
2.0;
E.
3.0;
30
If you attempt to add an int, a long, and a double, the result will be a __________ value.
A.
float
B.
int
C.
long
D.
double
Section 2.9 Character Data Type and Operations
31
Which of the following is the correct expression of character 4?
A.
4
B.
"4"
C.
'\0004'
D.
'4'
32
A character is stored in __________.
A.
one byte
B.
two bytes
C.
three bytes
D.
four bytes
33
Suppose x is a char variable with a value 'b'. What is the printout of the statement cout << ++x?
A.
a
B.
b
C.
c
D.
d
34
Which of the following statement prints smith\exam1\test.txt?
A.
cout << "smith\exam1\test.txt";
B.
cout << "smith\\exam1\\test.txt";
C.
cout << "smith\"exam1\"test.txt";
D.
cout << "smith"\exam1"\test.txt";
35
Suppose i is an int type variable. Which of the following statements display the character whose ASCII is stored in variable i?
A.
cout << i;
B.
cout << (char)i;
C.
cout << (int)i;
D.
cout << i;
36
The ASCII of 'a' is 97. What is the ASCII for 'c'?
A.
96
B.
97
C.
98
D.
99
37
What is the printout of cout << 'z' - 'a'?
A.
25
B.
26
C.
a
D.
z
38
You can assign a value in _____ to an int variable.
A.
'x'
B.
120
C.
120.0
D.
"x"
E.
"120"
39
Which of the following assignment statements is correct?
A.
char c = 'd';
B.
char c = 100;
C.
char c = "d";
D.
char c = "100";
40
Note that the ASCII for character A is 65. The expression 'A' + 1 evaluates to ________.
A.
66
B.
B
C.
A1
D.
Illegal expression
41
Analyze the following code.
  #include <iostream>
  using namespace std;
  int main()
  {
    int i;
    int j;
    cout << "Enter an integer: ";
    cin >> j;
    i = (i + 4);
  }
A.
The program cannot compile because j is not initialized.
B.
The program cannot compile because i does not have an initial value when it is used in i = i + 4;
C.
The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4;
D.
The program compiles and runs fine.
Section 2.12 Case Studies
42
The __________ function returns a raised to the power of b.
A.
power(a, b)
B.
exponent(a, b)
C.
pow(a, b)
D.
pow(b, a)
43
The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
A.
76.02
B.
76
C.
76.0252175
D.
76.03
44
The function time(0) returns ________________ .
A.
the current time.
B.
the current time in seconds.
C.
the current time in seconds since midnight.
D.
the current time in seconds since midnight, January 1, 1970.
E.
the current time in seconds since midnight, January 1, 1970 GMT (the Unix time).
Section 2.14 Programming Style and Documentation
45
Programming style is important, because ______________.
A.
a program may not compile if it has a bad style
B.
good programming style can make a program run faster
C.
good programming style makes a program more readable
D.
good programming style helps reduce programming errors
46
According to the C++ naming convention adopted by this book, which of the following names can be variables?
A.
FindArea
B.
findArea
C.
totalLength
D.
TOTAL_LENGTH
E.
class
Section 2.15 Programming Errors
47
If a program compiles fine, but it produces incorrect result, then the program suffers __________.
A.
a compilation error
B.
a runtime error
C.
a logic error