Challenge - 5 Problems
Unary Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pre-increment and post-increment
What is the output of the following Java code?
Java
int x = 5; System.out.println(x++); System.out.println(++x);
Attempts:
2 left
💡 Hint
Remember that x++ returns the value before incrementing, while ++x increments first then returns.
✗ Incorrect
The first print outputs 5 because x++ returns the current value then increments. After that, x becomes 6. The second print uses ++x, which increments x to 7 first, then prints 7.
❓ Predict Output
intermediate2:00remaining
Output of unary minus and plus operators
What is the output of this Java code snippet?
Java
int a = 10; int b = -a; int c = +b; System.out.println(b); System.out.println(c);
Attempts:
2 left
💡 Hint
Unary minus changes the sign, unary plus returns the value as is.
✗ Incorrect
Variable b is assigned -a, so b is -10. Variable c is +b, which keeps the value -10. So both print -10.
❓ Predict Output
advanced2:30remaining
Output with mixed unary operators and expressions
What will be printed by this Java code?
Java
int x = 3; int y = x++ + ++x + --x + x--; System.out.println(y);
Attempts:
2 left
💡 Hint
Evaluate each part step-by-step, tracking x's value changes carefully.
✗ Incorrect
Step by step:
Initial x=3
- x++ returns 3, x becomes 4
- ++x increments to 5, returns 5
- --x decrements to 4, returns 4
- x-- returns 4, x becomes 3
Sum: 3 + 5 + 4 + 4 = 16
❓ Predict Output
advanced2:00remaining
Output of unary logical complement operator
What is the output of this Java code?
Java
boolean flag = false; System.out.println(!flag); System.out.println(!!flag);
Attempts:
2 left
💡 Hint
The ! operator flips the boolean value.
✗ Incorrect
First print: !false is true.
Second print: !!false is !(!false) which is !true = false.
🧠 Conceptual
expert3:00remaining
Effect of unary operators on variable values
Given the following Java code, what is the final value of variable 'a' after execution?
Java
int a = 1; int b = a++ + ++a + a-- + --a;
Attempts:
2 left
💡 Hint
Track the value of 'a' carefully after each unary operation.
✗ Incorrect
Initial a=1
Evaluate b:
a++ returns 1, a=2
++a increments a to 3, returns 3
a-- returns 3, a=2
--a decrements a to 1, returns 1
Sum for b: 1+3+3+1=8 (not asked)
Final a after all operations is 1.