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 increment operators
What is the output of the following C code?
C
int main() { int a = 5; int b = a++ + ++a; printf("%d\n", b); return 0; }
Attempts:
2 left
💡 Hint
Remember that a++ uses the value before increment, ++a increments before use.
✗ Incorrect
Initially, a=5. a++ returns 5 then a becomes 6. ++a increments a to 7 then returns 7. So b = 5 + 7 = 12.
❓ Predict Output
intermediate2:00remaining
Value of variable after increments
What is the value of variable x after executing this code?
C
int main() { int x = 3; x = ++x + x++ + ++x; printf("%d\n", x); return 0; }
Attempts:
2 left
💡 Hint
Evaluate each increment carefully and track x's value step by step.
✗ Incorrect
Step 1: ++x increments x to 4 and returns 4.
Step 2: x++ returns 4 then increments x to 5.
Step 3: ++x increments x to 6 and returns 6.
Sum: 4 + 4 + 6 = 14, but x is assigned this sum, so x=14.
However, the expression uses x on left and right, so the final x is 14.
But the question asks for x after assignment, which is 14.
🔧 Debug
advanced2:00remaining
Identify the error in increment usage
Which option will cause a compilation error in C?
C
int main() { int a = 5; int b = /* expression */; printf("%d\n", b); return 0; }
Attempts:
2 left
💡 Hint
Look carefully at the operator sequences and how C parses them.
✗ Incorrect
Option B has 'a+++++a' which is invalid because C cannot parse '+++++' as valid operators. It causes a syntax error.
Other options are valid expressions with increments.
❓ Predict Output
advanced2:00remaining
Output of complex increment and decrement
What is the output of this C program?
C
int main() { int i = 2; int j = i++ + --i + i-- + ++i; printf("%d\n", j); return 0; }
Attempts:
2 left
💡 Hint
Track the value of i carefully after each increment or decrement.
✗ Incorrect
Start i=2
1) i++ returns 2, i=3
2) --i decrements i to 2, returns 2
3) i-- returns 2, i=1
4) ++i increments i to 2, returns 2
Sum: 2 + 2 + 2 + 2 = 8
So output is 8, option A.
Correct answer is C.
❓ Predict Output
expert3:00remaining
Final value after chained increments and decrements
What is the value of variable x after this code runs?
C
int main() { int x = 1; x = x++ + ++x + --x + x--; printf("%d\n", x); return 0; }
Attempts:
2 left
💡 Hint
Evaluate each increment/decrement carefully and update x after each step.
✗ Incorrect
Start x=1
1) x++ returns 1, x=2
2) ++x increments x to 3, returns 3
3) --x decrements x to 2, returns 2
4) x-- returns 2, x=1
Sum: 1 + 3 + 2 + 2 = 8
But x is assigned this sum, so x=8
Option D is 8, so correct answer is C.