Challenge - 5 Problems
Arithmetic Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mixed arithmetic operations
What is the output of the following Java code?
Java
public class Main { public static void main(String[] args) { int a = 10; int b = 3; int result = a / b * b + a % b; System.out.println(result); } }
Attempts:
2 left
💡 Hint
Remember the order of operations and how integer division works in Java.
✗ Incorrect
Integer division a / b equals 3 (since 10 / 3 = 3). Then 3 * b = 9. The remainder a % b is 1. Adding 9 + 1 gives 10.
❓ Predict Output
intermediate2:00remaining
Result of arithmetic with doubles
What is the output of this Java program?
Java
public class Main { public static void main(String[] args) { double x = 5.0 / 2; double y = 5 / 2.0; System.out.println(x + "," + y); } }
Attempts:
2 left
💡 Hint
Check how Java handles division when one operand is a double.
✗ Incorrect
Both divisions involve a double operand, so the division is floating-point, resulting in 2.5 for both.
❓ Predict Output
advanced2:00remaining
Output of complex arithmetic expression with increment operators
What is the output of this Java code snippet?
Java
public class Main { public static void main(String[] args) { int i = 1; int result = i++ + ++i * i++; System.out.println(result); } }
Attempts:
2 left
💡 Hint
Evaluate the increments step by step, remembering post-increment and pre-increment behavior.
✗ Incorrect
i++ uses i=1 then increments to 2; ++i increments to 3 then uses 3; i++ uses 3 then increments to 4. Expression: 1 + 3 * 3 = 1 + 9 = 10. Stepwise: i=1; i++=1 (i=2); ++i=3 (i=3); i++=3 (i=4); expression=1 + 3*3=10. Output is 10.
❓ Predict Output
advanced2:00remaining
Division by zero with floating point
What is the output of this Java program?
Java
public class Main { public static void main(String[] args) { double a = 5.0 / 0; System.out.println(a); } }
Attempts:
2 left
💡 Hint
Division by zero behaves differently for integers and floating-point numbers in Java.
✗ Incorrect
Dividing a floating-point number by zero results in Infinity, not an exception.
🧠 Conceptual
expert3:00remaining
Number of items in a map after arithmetic key operations
Consider this Java code snippet. How many entries does the map contain after execution?
Java
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Integer, Integer> map = new HashMap<>(); int x = 2; map.put(x + 1, x * 2); map.put(x++, x + 3); map.put(++x, x * x); System.out.println(map.size()); } }
Attempts:
2 left
💡 Hint
Track the value of x carefully and note that keys may overwrite existing entries.
✗ Incorrect
Stepwise: x=2
map.put(3,4) // key=3, value=4
map.put(2,5) // x++ uses 2 then x=3, value=5
map.put(4,16) // ++x increments x to 4, value=16
Keys are 3,2,4 distinct, so map size is 3.