- Learn C++ - http://www.learncpp.com -

3.x — Chapter 3 comprehensive quiz

Quick review

Always use parentheses to disambiguate the precedence of operators if there is any question or opportunity for confusion.

The arithmetic operators all work like they do in normal mathematics. The Modulus (%) operator returns the remainder from an integer division. Beware about rounding or sign errors when the operands of integer division and modulus are negative.

The increment and decrement operators can be used to easily increment or decrement numbers. Beware of side effects, particularly when it comes to the order that function parameters are evaluated. Do not use a variable that has a side effect applied more than once in a given statement.

Relational operators can be used to compare floating point numbers. Beware using equality and inequality on floating point numbers.

Logical operators allow us to form compound conditional statements. Bitwise operators allow us to modify or query individual bits.

Comprehensive quiz

1) Evaluate the following:

a) (5 > 3 && 4 < 8)
b) (4 > 6 && true)
c) (3 >= 3 || false)
d) (true || false) ? 4 : 5

Show Solution [1]

2) Answer the following:

a) 7 / 4
b) 14 % 5

Show Solution [1]

3) Convert the following from binary to decimal:

a) 1101
b) 101110

Show Solution [1]

4) Convert the following from decimal to binary:

a) 15
b) 53

Show Solution [1]

5) Why should you never do the following:

a) int y = foo(++x, x);
b) int x = 7 / -2; // (prior to C++11)
c) int x = -5 % 2; // (prior to C++11)
d) float x = 0.1 + 0.1; if (x == 0.2) return true; else return false;
e) int x = 3 / 0;

Show Solution [1]

4.1 -- Blocks (compound statements) [2]
Index [3]
3.8a -- Bit flags and bit masks [4]