Free Practice Quiz Question List

2 Variables and Data Types Online Quiz Questions

Use this free practice quiz with 20 questions to review 2 Variables and Data Types, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

Which statement correctly distinguishes initialization from assignment in C++?

  1. A

    width = 10; creates a new object and gives it its first value.

  2. B

    int width{10}; creates an object and gives it its first value.

  3. C

    width = 25; declares a new object named width.

  4. D

    int width{10}; replaces the value of an existing object.

02
Choose one
1 point

What value and type result from this declaration? int result{7 / 2};

  1. A

    3

  2. B

    3.0

  3. C

    3 with type int

  4. D

    3.5 with type double

03
Choose one
1 point

Which statement calculates a fractional per-person amount when int total{7}; and int people{2}; are already declared?

  1. A

    double per_person{static_cast<double>(total) / people};

  2. B

    double per_person{total / people};

  3. C

    int per_person{static_cast<double>(total) / people};

  4. D

    double per_person{static_cast<int>(total / people)};

04
Choose all
1 point

Which of the following can be valid C++ identifiers? Select all correct choices.

  1. A

    item_count

  2. B

    2nd_item

  3. C

    score2

  4. D

    Score

  5. E

    class

05
Choose all
1 point

Which statements correctly describe short-circuit evaluation in C++? Select all correct choices.

  1. A

    The right side of A && B is evaluated only if A is true.

  2. B

    The right side of A || B is evaluated only if A is false.

  3. C

    Both operands of && and || are always evaluated.

  4. D

    Short-circuiting can guard a division by checking that its denominator is nonzero first.

  5. E

    Short-circuiting changes an integer into a floating-point value.

06
True or false
1 point

True or false: After const int days_in_week{7}; has been initialized, days_in_week = 8; is an error.

  1. A

    True

  2. B

    False

07
True or false
1 point

True or false: Subtracting a larger value from a smaller value in unsigned arithmetic produces an ordinary negative result.

  1. A

    True

  2. B

    False

08
Written response
1 point

What C++ built-in type should be used for a variable that stores either true or false?

09
Written response
1 point

Which C++ operator produces the remainder of integer division?

10
Fill in the blank
1 point

Complete the description of this declaration: price{19.99}; declares a variable using .

11
Fill in the blank
1 point

Complete the equivalent form: x += y is equivalent to .

12
Open ended
1 point

Explain why 7 / 2 produces 3 in an integer expression and describe how to write the calculation so that it produces 3.5. Include why the timing of the conversion matters.

13
Choose one
1 point

What value does int result{2 + 3 * 4}; initialize result with, and why?

  1. A

    20 because addition is evaluated first

  2. B

    14 because multiplication has higher precedence

  3. C

    24 because the operands are evaluated from right to left

  4. D

    9 because only the first two operands are combined

14
True or false
1 point

In C++, converting true to an integer produces 1, while converting false produces 0.

  1. A

    True

  2. B

    False

15
Choose one
1 point

What value is stored in int whole{7 / 2};?

  1. A

    3

  2. B

    3.5

  3. C

    2

  4. D

    A compilation error

16
Choose one
1 point

Why does if (denominator != 0 && numerator / denominator > 2) avoid evaluating the division when denominator is zero?

  1. A

    The division is always evaluated before the comparison

  2. B

    The expression converts the denominator to a Boolean value

  3. C

    The division is evaluated only when denominator != 0 is true

  4. D

    The expression is invalid because && cannot combine comparisons

17
Choose one
1 point

Which statement would cause a compilation error after these declarations: const int maximum_attempts{3}; int attempts{0};?

  1. A

    maximum_attempts;

  2. B

    attempts += 1;

  3. C

    maximum_attempts = 4;

  4. D

    attempts = 1;

18
Choose one
1 point

What value is stored in double per_person{static_cast<double>(total) / people}; when int total{7}; and int people{2};?

  1. A

    3

  2. B

    3.5

  3. C

    2

  4. D

    7.0

19
Written response
1 point

What C++ cast syntax names the preferred general-purpose form for an explicit type conversion?

20
Written response
1 point

In the complete example, what numeric value does total have after the ten-percent discount is applied?