0
0
Javaprogramming~10 mins

Operator precedence 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 correctly calculate the result using operator precedence.

Java
int result = 5 + 3 [1] 2;
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * causes the addition to happen first, changing the result.
2fill in blank
medium

Complete the code to correctly evaluate the expression considering operator precedence.

Java
int value = 10 - 4 [1] 2;
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * changes the operation order and result.
3fill in blank
hard

Fix the error in the expression to respect operator precedence and get the correct result.

Java
int output = 20 / 4 [1] 2;
Drag options to blanks, or click blank then click option'
A/
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or / again changes the order and result unexpectedly.
4fill in blank
hard

Fill both blanks to create a map of numbers to their doubled values only if the number is greater than 3.

Java
Map<Integer, Integer> squares = IntStream.range(1, 6)
    .filter(n -> n [1] 3)
    .boxed()
    .collect(Collectors.toMap(n -> n, n -> n [2] 2));
Drag options to blanks, or click blank then click option'
A>
B*
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in filter selects wrong numbers.
Using + instead of * does addition, not doubling.
5fill in blank
hard

Fill all three blanks to create a map of uppercase strings to their lengths only if the length is greater than 4.

Java
Map<String, Integer> result = words.stream()
    .filter(word -> word.length() [1] 4)
    .collect(Collectors.toMap(word -> word.[2](), word -> word.[3]()));
Drag options to blanks, or click blank then click option'
A<
Blength
C>
DtoUpperCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in filter.
Using length instead of toUpperCase for keys.
Using toUpperCase instead of length for values.