Search

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

2) Answer the following:

a) 7 / 4
b) 14 % 5

Show Solution

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

Show Solution

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

Show Solution

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

4.1 -- Blocks (compound statements)
Index
3.8a -- Bit flags and bit masks

22 comments to 3.x — Chapter 3 comprehensive quiz

  • Speeds03

    What I don’t understand from the quiz is #4; whether or not i’m using 4 bit, 8 bit etc… How would I know that with 15 decimal, I should start of with “15 >= 8” instead of “15 >= 16”. The same goes for 53 decimal (or any other number for that matter…

    • Thomas

      You must always think in bytes allocated for the given number, because that is the smallest unit that can be addressed in memory. If you allocated an int for it, that should mean 4 bytes (at least, on 32 bit architectures). In a case like this quiz, you only need to check whether the given number can be stored in 1 byte (8 bits, like a char or bool). If it’s too big for that, then you must go up to 2 bytes (like short int). If it’s still too big, then 4 bytes (int) and so on.

    • Alex

      When using method 2 of converting a decimal to binary, always start with the largest power of 2 that is smaller than your starting decimal number. So if your number is 15, start with 8 (2^3). If your number is 53, start with 32 (2^5).

  • Gocha

    Great tutorial!
    Whether I was learning c++ or other programming language somewhere else, everything seemed much complicated than on this site. I would say that this is the site where everything is detailed and well explained, even through c++ is really hard to learn/teach, I follow this tutorial without any troubles so far, I wish you all the best, my friend!

  • Yongxin

    It turns out we don’t have further quizzes. How to access more?

  • Bogdan

    In C++11 the modulo division for negatives is defined now.

  • nj

    hii alex !! kudos for this awesome work of yours.

    but why don’t we have quizzes after chapter 6?
    please add them soon.

  • Jarvis Chen

    In the comprehensive quiz, there is a line break missing.


    1) Evaluate the following:

    a) (5 > 3 && 4 < 8) b) (4 > 6 && true)

    Just a small thing, but if you can make something perfect, why not? πŸ˜‰

    EDIT: Awesome website btw!!! I am looking forward to more quizzes!

  • Aryan

    Hey Alex!
    Can you please explain the 1st question d part? I don’t know how and where did that bitwise OR operator got in between 4 and 5.

  • SyJ

    Thanks, this really helped me. I didn’t move on until I understood this lesson and the one before it. Please note you mention enum (which I don’t know yet) I think you previously used an enum but removed it. Please can you clarify this and remove that part in the lesson thank you

  • functionLover

    alex! you should perhaps consider adding donation links to this page.
    this tutorial format is far more helpful than many books out there.

  • bert

    Did you mean to use "particularly" in the following:

    " Beware of side effects, particular when it comes to the order that function parameters are evaluated."

  • For quiz question 5c, the wording of "Why should you never do the following" might be a bit misleading, when

    is valid for even/odd checking even if x is negative.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter