Bird
Raised Fist0
Javaprogramming~20 mins

Arithmetic operators in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Arithmetic Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
    }
}
A12
B9
C10
D13
Attempts:
2 left
💡 Hint
Remember the order of operations and how integer division works in Java.
❓ Predict Output
intermediate
2: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);
    }
}
A2.5,2.5
B2,2
C2,2.5
D2.0,2.0
Attempts:
2 left
💡 Hint
Check how Java handles division when one operand is a double.
❓ Predict Output
advanced
2: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);
    }
}
A9
B12
C11
D10
Attempts:
2 left
💡 Hint
Evaluate the increments step by step, remembering post-increment and pre-increment behavior.
❓ Predict Output
advanced
2: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);
    }
}
AArithmeticException
BInfinity
CNaN
D0
Attempts:
2 left
💡 Hint
Division by zero behaves differently for integers and floating-point numbers in Java.
🧠 Conceptual
expert
3: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());
    }
}
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Track the value of x carefully and note that keys may overwrite existing entries.

Practice

(1/5)
1. Which of the following is the correct symbol for the modulus operator in Java, which gives the remainder of a division?
easy
A. *
B. /
C. -
D. %

Solution

  1. Step 1: Understand the modulus operator

    The modulus operator returns the remainder after division of two numbers.
  2. Step 2: Identify the correct symbol in Java

    In Java, the symbol for modulus is %.
  3. Final Answer:

    % -> Option D
  4. Quick Check:

    Modulus operator = % [OK]
Hint: Modulus operator is always % in Java math [OK]
Common Mistakes:
  • Confusing modulus % with division /
  • Using * or - instead of %
  • Thinking modulus returns quotient
2. Which of the following Java statements correctly calculates the product of two integers a and b?
easy
A. int result = a / b;
B. int result = a + b;
C. int result = a * b;
D. int result = a % b;

Solution

  1. Step 1: Identify the multiplication operator

    In Java, the multiplication operator is *.
  2. Step 2: Check the statement that uses multiplication

    The statement int result = a * b; correctly multiplies a and b.
  3. Final Answer:

    int result = a * b; -> Option C
  4. Quick Check:

    Multiplication operator = * [OK]
Hint: Use * for multiplication in Java expressions [OK]
Common Mistakes:
  • Using + instead of * for multiplication
  • Confusing division / with multiplication
  • Using % which is remainder, not product
3. What is the output of the following Java code?
int x = 10;
int y = 3;
System.out.println(x / y);
medium
A. 3
B. 3.3333333
C. 3.0
D. 4

Solution

  1. Step 1: Understand integer division in Java

    Both x and y are integers, so division drops the decimal part.
  2. Step 2: Calculate integer division result

    10 divided by 3 is 3.333..., but integer division returns 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Integer division drops decimals = 3 [OK]
Hint: Integer division drops decimals, result is integer [OK]
Common Mistakes:
  • Expecting decimal output with int division
  • Confusing / with % operator
  • Assuming automatic float conversion
4. Identify the error in the following Java code snippet:
int a = 5;
int b = 0;
int c = a / b;
System.out.println(c);
medium
A. Division by zero causes runtime error
B. Variable c is not declared
C. Missing semicolon after declaration
D. Incorrect use of modulus operator

Solution

  1. Step 1: Analyze the division operation

    The code divides a by b, where b is zero.
  2. Step 2: Understand division by zero in Java

    Dividing an integer by zero causes a runtime ArithmeticException error.
  3. Final Answer:

    Division by zero causes runtime error -> Option A
  4. Quick Check:

    Division by zero throws error [OK]
Hint: Never divide by zero; it crashes the program [OK]
Common Mistakes:
  • Ignoring division by zero error
  • Thinking it returns zero or infinity
  • Confusing syntax error with runtime error
5. Given the code below, what will be the value of result after execution?
int a = 15;
int b = 4;
int result = a / b + a % b * 2;
hard
A. 11
B. 9
C. 8
D. 7

Solution

  1. Step 1: Calculate integer division and modulus

    15 / 4 = 3 (integer division), 15 % 4 = 3 (remainder).
  2. Step 2: Apply operator precedence and compute expression

    Expression is 3 + 3 * 2. Multiplication first: 3 * 2 = 6. Then addition: 3 + 6 = 9.
  3. Step 3: Re-check calculation

    Wait, re-check: 15 / 4 = 3, 15 % 4 = 3, 3 * 2 = 6, 3 + 6 = 9.
  4. Final Answer:

    9 -> Option B
  5. Quick Check:

    Division + (modulus * 2) = 9 [OK]
Hint: Remember * before + in mixed expressions [OK]
Common Mistakes:
  • Adding before multiplying
  • Confusing division and modulus results
  • Forgetting integer division truncates decimals
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