Bird
Raised Fist0
Javaprogramming~10 mins

Unary operators in Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to increment the variable count by 1 using a unary operator.

Java
int count = 5;
count[1];
Drag options to blanks, or click blank then click option'
A-
B++
C+=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the value.
Using '+=' which is not a unary operator.
Using '-' which subtracts but is not unary increment.
2fill in blank
medium

Complete the code to negate the boolean variable isActive using a unary operator.

Java
boolean isActive = true;
isActive = [1]isActive;
Drag options to blanks, or click blank then click option'
A!
B--
C++
D~
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' or '--' which are for numeric increments or decrements.
Using '~' which is bitwise complement, not for booleans.
3fill in blank
hard

Fix the error in the code by choosing the correct unary operator to decrement num by 1.

Java
int num = 10;
num[1];
Drag options to blanks, or click blank then click option'
A*
B++
C+
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' which increases the value.
Using '+' or '*' which are binary operators and cause errors here.
4fill in blank
hard

Fill both blanks to create a map of numbers to their negations using unary operators.

Java
Map<Integer, Integer> negations = Map.of(1, [1]1, 2, [2]2);
Drag options to blanks, or click blank then click option'
A-
B++
C--
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' or '--' which change the number by one, not negate.
Using '!' which is for booleans, not numbers.
5fill in blank
hard

Fill all three blanks to create a map where keys are numbers and values are their incremented, negated, and decremented forms respectively.

Java
int one = 1, two = 2, three = 3;
Map<Integer, Integer> results = Map.of(
  one, [1]one,
  two, [2]two,
  three, [3]three);
Drag options to blanks, or click blank then click option'
A++
B-
C--
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!' which is only for booleans.
Mixing prefix and postfix operators incorrectly.

Practice

(1/5)
1.

What does the unary operator ++ do in Java?

easy
A. Negates the value of a variable
B. Increases the value of a variable by 1
C. Decreases the value of a variable by 1
D. Checks if a value is true or false

Solution

  1. Step 1: Understand the meaning of ++

    The ++ operator adds 1 to the current value of a variable.
  2. Step 2: Compare with other unary operators

    -- subtracts 1, ! flips boolean, and negation changes sign.
  3. Final Answer:

    Increases the value of a variable by 1 -> Option B
  4. Quick Check:

    ++ means add one [OK]
Hint: ++ means add one to a number quickly [OK]
Common Mistakes:
  • Confusing ++ with --
  • Thinking ++ negates the value
  • Mixing ++ with boolean negation !
2.

Which of the following is the correct **postfix** syntax to decrement a variable x by 1 using a unary operator?

int x = 5;
// Your code here
easy
A. x-1;
B. --x;
C. x--;
D. x=!1;

Solution

  1. Step 1: Identify decrement unary operators

    Both x-- and --x decrement by 1, but x-1 is an expression, not an operator.
  2. Step 2: Choose correct syntax for decrement

    x--; correctly decreases x by 1 after its current use.
  3. Final Answer:

    x--; -> Option C
  4. Quick Check:

    Use x-- to subtract one [OK]
Hint: Use x-- or --x to subtract one [OK]
Common Mistakes:
  • Using x-1; which doesn't change x
  • Confusing ! operator with decrement
  • Writing invalid syntax like x=!1;
3.

What is the output of the following Java code?

int a = 3;
System.out.println(++a);
System.out.println(a++);
System.out.println(a);
medium
A. 4 4 5
B. 3 4 5
C. 4 3 4
D. 3 3 4

Solution

  1. Step 1: Evaluate ++a

    ++a increases a before printing, so prints 4 and a becomes 4.
  2. Step 2: Evaluate a++

    a++ prints current a (4) then increases it to 5.
  3. Step 3: Print final value of a

    After previous steps, a is 5, so prints 5.
  4. Final Answer:

    4 4 5 -> Option A
  5. Quick Check:

    Prefix ++ adds before print, postfix ++ adds after print [OK]
Hint: Prefix ++ changes before print, postfix ++ after print [OK]
Common Mistakes:
  • Mixing prefix and postfix effects
  • Forgetting that ++ changes the variable
  • Assuming no change in variable after print
4.

Find the error in the following code snippet:

boolean flag = true;
flag = !flag++;
System.out.println(flag);
medium
A. No error, code runs fine
B. Missing semicolon after flag declaration
C. Logical NOT operator ! cannot be used here
D. Cannot apply ++ to a boolean variable

Solution

  1. Step 1: Check variable type and operators

    flag is boolean; ++ operator works only on numeric types, not boolean.
  2. Step 2: Identify invalid operation

    Using flag++ causes a compile-time error because booleans cannot be incremented.
  3. Final Answer:

    Cannot apply ++ to a boolean variable -> Option D
  4. Quick Check:

    ++ works only on numbers, not booleans [OK]
Hint: ++ cannot be used on boolean variables [OK]
Common Mistakes:
  • Trying to increment boolean values
  • Confusing logical NOT ! with increment ++
  • Assuming code compiles without error
5.

Given the code below, what will be the final value of count after execution?

int count = 0;
for (int i = 0; i < 3; i++) {
    count = count++ + ++count;
}
System.out.println(count);
hard
A. 14
B. 6
C. 12
D. 9

Solution

  1. Step 1: Understand count = count++ + ++count; behavior

    Binary + evaluates left operand first (count++: old value then increment), then right (++count: increment then new value).
  2. Step 2: Trace loop iterations

    Iteration 1 (count=0): count++ value=0 (count=1), ++count value=2, sum=2 -> count=2.
    Iteration 2 (count=2): count++=2 (count=3), ++count=4, sum=6 -> count=6.
    Iteration 3 (count=6): count++=6 (count=7), ++count=8, sum=14 -> count=14.
  3. Step 3: Final value after loop

    After 3 iterations, count is 14.
  4. Final Answer:

    14 -> Option A
  5. Quick Check:

    Left-to-right: postfix old + prefix new [OK]
Hint: Left operand postfix (old then inc), right prefix (inc then new) [OK]
Common Mistakes:
  • Assuming both increments add fully
  • Ignoring postfix increment side effects
  • Calculating sum without assignment effect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard