Skip to main content

Java Topic Practice

Casting and Math.random

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

For practice use only.

Casting and Math.random MCQ Practice

Casting and Math.random Java practice with 15 questions, code tracing, and a worked explanation for every answer.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - Casting and Math.random: track the final printed value.

int completed = 18;
int bonus = 5;
int days = 3;
double average = (double) (completed + bonus) / days;
System.out.printf("%.6f", average);

All 15 casting and math.random questions

Work through the interactive quiz above first. This is the full casting and math.random question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - Casting and Math.random: track the final printed value.

    int completed = 18;
    int bonus = 5;
    int days = 3;
    double average = (double) (completed + bonus) / days;
    System.out.printf("%.6f", average);
    • 6
    • 7.666667
    • 7
    • 19.666667
    Show worked solution

    Correct answer: 7.666667

    The cast turns the entire numerator into a double before division, so decimal division is used.

  2. 2.Code Tracing 12 - Casting and Math.random: follow the variable updates.

    int total = 26;
    int count = 5;
    double result = (double) (total / count);
    System.out.print(result);
    • 5
    • 61
    • 5.0
    • 5.2
    Show worked solution

    Correct answer: 5.0

    The parentheses force integer division first. The truncated result is then converted to a double.

  3. 3.Code Tracing 28 - Casting and Math.random: trace the branch and loop path.

    double raw = 3.1;
    int offset = 3;
    int score = (int) (raw * 10) + offset;
    System.out.print(score);
    • 35
    • The code does not compile.
    • 34
    • 33
    Show worked solution

    Correct answer: 34

    Casting to int truncates the decimal part after raw * 10 is evaluated.

  4. 4.Code Tracing 39 - Casting and Math.random: watch the index changes.

    int numerator = 46;
    int first = 5;
    int second = 5;
    double value = numerator / (double) (first + second);
    System.out.printf("%.6f", value);
    • 4
    • 14.200000
    • 14
    • 4.600000
    Show worked solution

    Correct answer: 4.600000

    The denominator group is converted to double, so the final division keeps the fractional part.

  5. 5.Code Tracing 50 - Casting and Math.random: evaluate the state change step by step.

    double points = 54.75;
    int divisor = 7;
    int bucket = (int) (points / divisor);
    System.out.print(bucket);
    • 7
    • 8
    • 7.821429
    • The code does not compile.
    Show worked solution

    Correct answer: 7

    The division is decimal because points is a double, but the final cast truncates the decimal part.

  6. 6.What is printed by the following code segment?

    double d = 7.99;
    System.out.print((int) d + (int) (d + 0.5) + (int) (d * 2));
    • 31
    • 29
    • 32
    • 30
    Show worked solution

    Correct answer: 30

    The casts give 7, then 8 from the rounding idiom, then 15 from truncating 15.98, and the sum is 30.

  7. 7.What is printed by the following code segment?

    int a = 7;
    int b = 2;
    System.out.print((double) (a / b) + " " + (double) a / b);
    • 3.0 3.5
    • 3.5 3.5
    • 3.0 3.0
    • 3.5 3.0
    Show worked solution

    Correct answer: 3.0 3.5

    Casting after the division only widens the truncated 3, while casting an operand first keeps the fraction.

  8. 8.What is printed by the following code segment?

    double x = 2.5;
    double y = 3.5;
    System.out.print((int) x + (int) y + (int) (x + y));
    • 12
    • 11
    • 10
    • 6
    Show worked solution

    Correct answer: 11

    Truncation gives 2 and 3, but the sum 6.0 casts to 6, so rounding each value separately loses more than rounding once.

  9. 9.What is printed by the following code segment?

    int n = (int) (3.9 * 2 - 0.9);
    System.out.print(n);
    • 7
    • 8
    • 6
    • 5
    Show worked solution

    Correct answer: 6

    The expression evaluates to 6.9 in double arithmetic and the cast truncates it to 6.

  10. 10.What is printed by the following code segment?

    double d = 1.0 / 3;
    System.out.print((int) (d * 3) + " " + d * 3);
    • 0 1.0
    • 1 0.9999999999999999
    • 0 0.9999999999999999
    • 1 1.0
    Show worked solution

    Correct answer: 1 1.0

    One third times three rounds back to exactly 1.0 in double arithmetic, so the cast gives 1.

  11. 11.What is printed by the following code segment?

    int cents = 1999;
    double dollars = cents / 100;
    System.out.print(dollars);
    • 19.0
    • 19.99
    • 20.0
    • 19
    Show worked solution

    Correct answer: 19.0

    Both operands are int, so the division truncates to 19 before it is widened to 19.0.

  12. 12.Which expression produces a random even int from 10 through 30, inclusive?

    • (int) (Math.random() * 21) * 2 + 10
    • (int) (Math.random() * 11) * 2 + 10
    • (int) (Math.random() * 10) * 2 + 10
    • (int) (Math.random() * 11) + 10
    Show worked solution

    Correct answer: (int) (Math.random() * 11) * 2 + 10

    The even values from 10 to 30 number 11 in total, so the multiplier is 11, doubling gives an even offset, and adding 10 shifts the range.

  13. 13.Which expression produces a random int that is a multiple of 5 from 0 through 100, inclusive?

    • (int) (Math.random() * 20) * 5
    • (int) (Math.random() * 100) * 5
    • (int) (Math.random() * 21) * 5
    • (int) (Math.random() * 5) * 21
    Show worked solution

    Correct answer: (int) (Math.random() * 21) * 5

    There are 21 multiples of 5 from 0 through 100, so the multiplier is 21 and each result is scaled by 5.

  14. 14.What are all the possible values of n after the statement below?

    int n = (int) (Math.random() * 4) * (int) (Math.random() * 4);
    • 0 through 9
    • 1 through 9
    • 0 through 16
    • 0, 1, 2, 3, 4, 6, or 9
    Show worked solution

    Correct answer: 0, 1, 2, 3, 4, 6, or 9

    Each factor is 0 through 3, so the products are the pairwise products of those values, which omit 5, 7, and 8.

  15. 15.What is printed by the following code segment?

    double d = -2.7;
    int a = (int) d;
    int b = (int) (d - 0.5);
    System.out.print(a + " " + b + " " + Math.abs(a));
    • -2 -3 2
    • -3 -3 3
    • -2 -2 2
    • -3 -4 3
    Show worked solution

    Correct answer: -2 -3 2

    Casting truncates toward zero, so -2.7 gives -2 and -3.2 gives -3. Subtracting 0.5 is the rounding idiom for negatives.