All topics
Operators
Arithmetic, comparison, logical and bitwise operators, the ternary operator, and precedence.
Sort:
3 questions
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
3 -1
Open full question →Common mistakes
- ✗Expecting
3.5: dividing oneintby another drops the fraction, so7 / 2is3. - ✗Expecting a non-negative remainder, as in math: in C++ the remainder takes the sign of the dividend, so
-7 % 3is-1.
Follow-up questions
- →How would you change the expression so that the division prints
3.5? - →What does
7 % -3evaluate to, and why?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
8 10
Open full question →Common mistakes
- ✗Expecting
5fora:+has higher precedence than<<, so1 << n + 1is1 << 3, which is8. - ✗Expecting
11forb:?:has lower precedence than+, so20 + 1is the whole third operand, andbgets10.
Follow-up questions
- →Where would you add parentheses so that
abecomes5andbbecomes11? - →Why does
std::cout << n > 1;fail to compile whilestd::cout << (n > 1);works?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
5 1
Open full question →Common mistakes
- ✗Expecting
520 1:||evaluates its right operand only when the left one isfalse, andis_small(5)is alreadytrue. - ✗Expecting
true: by defaultstd::coutprints aboolas1or0, not as a word.
Follow-up questions
- →What would the program print if
||were replaced with&&? - →How does short-circuit evaluation make
d != 0 && n / d > 2safe whendis zero?