0
0
Javaprogramming~10 mins

Unary operators in Java - Interactive Code Practice

Choose your learning style9 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.