0
0
Javaprogramming~20 mins

Unary operators in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unary Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A5\n7
B6\n7
C5\n6
D6\n6
Attempts:
2 left
💡 Hint
Remember that x++ returns the value before incrementing, while ++x increments first then returns.
Predict Output
intermediate
2: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);
A10\n10
B-10\n-10
C-10\n10
D10\n-10
Attempts:
2 left
💡 Hint
Unary minus changes the sign, unary plus returns the value as is.
Predict Output
advanced
2: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);
A13
B15
C16
D12
Attempts:
2 left
💡 Hint
Evaluate each part step-by-step, tracking x's value changes carefully.
Predict Output
advanced
2: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);
Afalse\ntrue
Btrue\ntrue
Cfalse\nfalse
Dtrue\nfalse
Attempts:
2 left
💡 Hint
The ! operator flips the boolean value.
🧠 Conceptual
expert
3: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;
A1
B4
C3
D2
Attempts:
2 left
💡 Hint
Track the value of 'a' carefully after each unary operation.