Revel for Liang C++, Fourth 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.

Chapter 2 Elementary Programming


Section 2.2 Writing a Simple Program
2.1  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.2  _______ is the code with natural language mixed with some program code.
A. A program
B. A statement
C. Pseudocode
D. A flowchart diagram

Section 2.3 Reading Input from the Keyboard
2.3  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.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? Please select all that apply.
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? Please select all that apply.
A. radius
B. Radius
C. RADIUS
D. findArea
E. FindArea

2.8  Which of the following are correct ways to declare variables? Please select all that apply.
A. int length; int width;
B. int length, width;
C. int length; width;
D. int length, int width;

Section 2.6 Assignment Statements and Assignment Expressions
2.9  ____________ is the assignment operator.
A. ==
B. :=
C. =
D. =:

2.10  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.11  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.12  To declare and initialize an int variable i, use ___________. Please select all that apply.
A. int i = 1;
B. int i(1);
C. int i == 1;
D. int i(1.0);

Section 2.7 Named Constants
2.13  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.14  Which of the following is a constant, according to the naming conventions adopted by this book? Please select all that apply.
A. MAX_VALUE
B. Test
C. read
D. ReadInt
E. COUNT

2.15  To improve readability and maintainability, you should declare a _________ for PI instead of using literal values such as 3.14159.
A. variable
B. function
C. constant
D. class

Section 2.8 Numeric Data Types and Operations
2.16  Which of the following types can be used to define a floating-point numbers?
A. int
B. short
C. long
D. double

2.17  Which of these data type requires the most amount of memory?
A. long
B. int
C. short
D. double
E. long double

2.18  Which data type is the best to store a non-negative integer?
A. long
B. int
C. long long
D. unsigned
E. short

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

2.20  Which of the following is a hex literal?
A. 12
B. 0x2
C. x2
D. 01

2.21  Which of the following is incorrect?
A. 1_2
B. 0.4_56
C. 1_200_229
D. _4544

2.22  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.23  Which of the following expression results in a value 1?
A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6

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

2.25  25 % 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 % 20 is _____.
A. 1
B. 2
C. 3
D. 4
E. 0

2.28  34 % 7 is _____.
A. 3
B. 4
C. 5
D. 6
E. 0

2.29  To write 2.1 ^ 2.4 in C++, use _____
A. pow(2.4, 2.1)
B. pow(2.1, 2.4)
C. 2.1 * 2.4
D. 2.1 * 2.1

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

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

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

2.33  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)

Section 2.9 Evaluating Expressions and Operator Precedence
2.34  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.35  The time function is defined in the ________ header file.
A. time
B. ctime
C. cctype
D. cstdlib
E. cmath

2.36  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.

Section 2.11 Augmented Assignment Operators
2.37  To add a value 1 to variable x, you write ___________. Please select all that apply.
A. 1 + x = x;
B. x += 1;
C. x := 1;
D. x = x + 1;
E. x = 1 + x;

2.38  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.39  Suppose x is 1. What is x after x += 2?
A. 0
B. 1
C. 2
D. 3
E. 4

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

2.41  To add number to sum, you write ___________. Please select all that apply. (Note: C++ is case-sensitive)
A. number += sum;
B. number = sum + number;
C. sum = Number + sum;
D. sum += number;
E. sum = sum + number;

2.42  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.43  Are the following four statements equivalent?

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

2.44  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.45  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.46  Which of the following statements are true? Please select all that apply.
A. Any expression can be used as a statement in C++.
B. The expression x++ can be used as a statement.
C. The statement x = x + 5 is also an expression.
D. The statement x = y = z = 0 is legal.

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

2.48  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.49  Which of the following expression will yield 0.5? Please select all that apply.
A. 1 / 2
B. 1.0 / 2
C. static_cast<double>(1 / 2)
D. static_cast<double>(1) / 2
E. 1 / 2.0

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

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

2.52  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.53  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.54  The expression static_cast<int>(76.0252175 * 100) / 100 evaluates to _________.
A. 76.02
B. 76
C. 76.0252175
D. 76.03

Section 2.15 Case Study: Counting Monetary Units
2.55  10.03 * 100 is ____________.
A. not guaranteed to be 1003 because of floating-point numbers are represented in approximation.
B. 1003
C. 1002
D. 1004
E. 1001

Section 2.16 Common Errors
2.56  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.