Challenge - 5 Problems
Increment-Decrement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mixed pre and post increment
What is the output of this C++ code snippet?
C++
int x = 5; int y = x++ + ++x; std::cout << y << std::endl;
Attempts:
2 left
💡 Hint
Remember that post-increment returns the value before incrementing, pre-increment increments first.
✗ Incorrect
x++ returns 5 then x becomes 6; ++x increments x to 7 then returns 7; sum is 5 + 7 = 12.
❓ Predict Output
intermediate2:00remaining
Effect of multiple increments in one expression
What is the output of this C++ code?
C++
int a = 3; int b = ++a + a++ + ++a; std::cout << b << std::endl;
Attempts:
2 left
💡 Hint
Evaluate increments from left to right, updating 'a' after each operation.
✗ Incorrect
++a increments a to 4 and returns 4; a++ returns 4 then increments a to 5; ++a increments a to 6 and returns 6; sum = 4 + 4 + 6 = 14.
❓ Predict Output
advanced2:00remaining
Output with decrement operators in complex expression
What is the output of this C++ code?
C++
int i = 4; int j = i-- - --i + i--; std::cout << j << std::endl;
Attempts:
2 left
💡 Hint
Remember post-decrement returns value before decrement, pre-decrement decrements first.
✗ Incorrect
i-- returns 4 then i=3; --i decrements i to 2 returns 2; i-- returns 2 then i=1; sum = 4 - 2 + 2 = 4.
❓ Predict Output
advanced2:00remaining
Value of variable after increments and decrements
What is the value of 'x' after running this code?
C++
int x = 10; x = ++x + x++ - --x + x--; std::cout << x << std::endl;
Attempts:
2 left
💡 Hint
Track the value of x carefully after each increment or decrement.
✗ Incorrect
Stepwise: ++x increments x to 11 returns 11; x++ returns 11 then x=12; --x decrements x to 11 returns 11; x-- returns 11 then x=10; sum=11+11-11+11=22; x assigned 22.
❓ Predict Output
expert3:00remaining
Output of complex expression with increments and decrements
What is the output of this C++ code snippet?
C++
int n = 5; int result = n++ + ++n - --n + n-- + n; std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
Evaluate each increment/decrement carefully and update 'n' after each operation.
✗ Incorrect
n++ returns 5 (n=6); ++n increments n to 7 returns 7; --n decrements n to 6 returns 6; n-- returns 6 (n=5); n=5; sum=5+7-6+6+5=17.