Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 2 Elementary Programming


Section 2.3 Reading Input from the Keyboard
2.1  If you enter 1 2 3, when you run this program, what will be the output?

 #include <iostream>
 using namespace std;

 int main()
 {
   // Prompt the user to enter three numbers
   double number1, number2, number3;
   cout << "Enter three numbers: ";
   cin >> number1 >> number2 >> number3;

   // Compute average
   double average = (number1 + number2 + number3) / 3;

   // Display result
   cout << average << endl;

   return 0;
 }
A. 1
B. 2
C. 3
D. 4

2.2  What is the exact output of the following code?

  double area = 3.5;
  cout << "area";
  cout << area;
A. 3.53.5
B. 3.5 3.5
C. area3.5
D. area 3.5

2.3  _______ is the code with natural language mixed with some program code.
A. A program
B. A statement
C. Pseudocode
D. A flowchart diagram

2.4  ___________ are called stream insertion and stream extraction operators for sending output to the console and reading from the console, respectively.
A. >> and <<
B. << and >>

Section 2.4 Identifiers
2.5  Is every letter in a C++ keyword in lowercase?
A. true
B. false

2.6  Which of the following is a valid identifier?
A. _343
B. class
C. 9X
D. 8+9
E. radius

Section 2.5 Variables
2.7  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

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

2.9  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.6 Assignment Statements and Assignment Expressions
2.10  ____________ is the assignment operator.
A. ==
B. :=
C. =
D. =:

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

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

2.13  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.7 Named Constants
2.14  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;

2.15  To declare a constant MAX_LENGTH using the define directive, you write ______.
A. #define MAX_LENGTH = 99.98;
B. #define MAX_LENGTH = 99.98
C. #define MAX_LENGTH 99.98;
D. #define MAX_LENGTH 99.98

2.16  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

2.17  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.8 Numeric Data Types and Operations
2.18  Which of these data type requires the most amount of memory?
A. long
B. int
C. short
D. double
E. long double

2.19  Analyze the following code:

 #include <iostream>
 using namespace std;

 int main()
 {
   int n = 10000 * 10000 * 10000;
   cout << "n is " << n << endl;

   return 0;
 }
A. The program displays n is 1000000000000
B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted.
C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because C++ does not report errors on overflow.
D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program is aborted.
E. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program continues to execute because C++ does not report errors on underflow.

2.20  What is result of 45 / 4?
A. 10
B. 11
C. 11.25
D. 12

2.21  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

2.22  Which of the following expression results in a value 1?
A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6

2.23  25 % 1 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.24  -25 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.25  24 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.26  -24 % 5 is _____
A. -1
B. -2
C. -3
D. -4
E. 0

2.27  -24 % -5 is _____
A. 3
B. -3
C. 4
D. -4
E. 0

2.28  Analyze the following code.

 #include <iostream>
 using namespace std;

 int main()
 {
   int month = 09;
   cout << "month is " << month;

   return 0;
 }
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.

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

   return 0;
 }
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 compile error since x is redeclared in the statement int y = x = x + 1.

Section 2.9 Evaluating Expressions and Operator Precedence
2.30  What is the result of (4 + 1) * ((5 - 2) / 2)?
A. 4
B. 5
C. 5.0
D. 7.5
E. 5.5

Section 2.10 Case Study: Displaying the Current Time
2.31  The time function is defined in the ________ header file.
A. time
B. ctime
C. cctype
D. cstdlib
E. cmath

2.32  Invoking time(0) returns _______________.
A. the hour, minute, and second of the current time.
B. the elapsed time in milliseconds since Midnight, Jan 1, 1970 GMT.
C. the elapsed time in seconds since Midnight, Jan 1, 1970 GMT.
D. the elapsed time in minutes since Midnight, Jan 1, 1970 GMT.

Section2.11 Augmented Assignment Operators
2.33  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;

2.34  What is x after the following statements?

int x = 2;
int y = 1;
x *= y + 1;
A. x is 1.
B. x is 2.
C. x is 3.
D. x is 4.

2.35  Suppose x is 1. What is x after x += 2?
A. 0
B. 1
C. 2
D. 3
E. 4

2.36  Suppose x is 1. What is x after x -= 1?
A. 0
B. 1
C. 2
D. -1
E. -2

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

2.38  Which of the following statements are the same?

(A) x -= x + 4
(B) x = x + 4 - x
(C) x = x - (x + 4)
A. (A) and (B) are the same
B. (A) and (C) are the same
C. (B) and (C) are the same
D. (A), (B), and (C) are the same

Section 2.12 Increment and Decrement Operators
2.39  Are the following four statements equivalent?

number += 1;
number = number + 1;
number++;
        ++number;
A. Yes
B. No

2.40  What will be the output of the following code?


int i = 1;
int j = ++i;
cout << "i is " << i;
        cout << " and j is " << j;
A. i is 1 and j is 1
B. i is 1 and j is 2
C. i is 2 and j is 1
D. i is 2 and j is 2

2.41  What will be the output of the following code?


int i = 1;
int j = i++;
cout << "i is " << i;
        cout << " and j is " << j;
A. i is 1 and j is 1
B. i is 1 and j is 2
C. i is 2 and j is 1
D. i is 2 and j is 2

Section 2.13 Numeric Type Conversions
2.42  To assign a double variable d to an float variable x, you write
A. x = static_cast<long>(d)
B. x = static_cast<int>(d);
C. x = d;
D. x = static_cast<float>(d);

2.43  What will be displayed by the following code?

double x = 5.5;
int y = static_cast<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

2.44  What is the value of static_cast<double>(5)/2?
A. 2;
B. 2.5;
C. 3;
D. 2.0;
E. 3.0;

2.45  What is the value of static_cast<double>(5/2)?
A. 2;
B. 2.5;
C. 3;
D. 2.0;
E. 3.0;

2.46  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

2.47  Analyze the following code.

 #include <iostream>
 using namespace std;

 int main()
 {
   int i;
   int j;
   cout << "Enter integers for i and j: ";
   cin >> i;
   cin >> j;

   i = (i + 4);

   return 0;
 }
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 cannot compile because i is not initialized.
D. The program compiles and runs fine.

Section 2.14 Software Development Process
2.48  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)

2.49  pow(2.0, 3) returns __________.
A. 9
B. 8
C. 1
D. 0

2.50  pow(4.0, 1 / 2) returns __________.
A. 0
B. 1
C. 2
D. 3

2.51  pow(4.0, 1.0 / 2) returns __________.
A. 0
B. 1
C. 2
D. 3

2.52  The expression static_cast<int>(76.0252175 * 100) / 100 evaluates to _________.
A. 76.02
B. 76
C. 76.0252175
D. 76.03