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

3.x — Comprehensive Quiz

Posted By Alex On September 11, 2011 @ 5:02 pm In C++ Programming | 3 Comments

Quick Review

Always use parenthesis 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, particular when it comes to the order that function parameters are evaluated.

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

Comprehensive quiz

1) Evaluate the following:

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

a) 7 / 4
b) 14 % 5

3) Convert the following from binary to decimal:
a) 1101
b) 101110

4) Convert the following from decimal to binary:
a) 15
b) 53

5) Why should you never do the following:
a) int y = foo(++x, x);
b) int x = 7 / -2;
c) int x = -5 % 2;
d) float x = 0.1 + 0.1; if (x == 0.2) return true; else return false;
e) int x = 3 / 0;

Solutions

1) Show Solution [1]

2) Show Solution [1]

3) Show Solution [1]

4) Show Solution [1]

5) Show Solution [1]

 4.1 — Blocks (compound statements) and local variables [2] Index [3] 3.8 — Bitwise operators [4]