Skip to main content

Java Topic Practice

Primitive Expressions

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

For practice use only.

Primitive Expressions MCQ Practice

Primitive Expressions 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 - Primitive Expressions: track the final printed value.

int a = 18;
int b = 3;
int c = 5;
int d = 2;
System.out.print(a + b * c - d);

All 15 primitive expressions questions

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

  1. 1.Code Tracing 01 - Primitive Expressions: track the final printed value.

    int a = 18;
    int b = 3;
    int c = 5;
    int d = 2;
    System.out.print(a + b * c - d);
    • 27
    • 24
    • 31
    • 103
    Show worked solution

    Correct answer: 31

    Multiplication is evaluated before the left-to-right addition and subtraction.

  2. 2.Code Tracing 12 - Primitive Expressions: follow the variable updates.

    int score = 26;
    int scale = 7;
    score++;
    scale -= 1;
    System.out.print(score * scale);
    • 182
    • 189
    • 156
    • 162
    Show worked solution

    Correct answer: 162

    The increment and compound assignment both update their variables before the multiplication.

  3. 3.Code Tracing 28 - Primitive Expressions: trace the branch and loop path.

    int a = 38;
    int b = 3;
    int c = 10;
    int d = 3;
    int result = a % b * c + d;
    System.out.print(result);
    • 11
    • 26
    • 38
    • 23
    Show worked solution

    Correct answer: 23

    % and * have the same precedence and are evaluated left to right before addition.

  4. 4.Code Tracing 39 - Primitive Expressions: watch the index changes.

    int result = 46;
    int b = 5;
    int c = 5;
    int d = 5;
    result -= (b + c) * d;
    System.out.print(result);
    • -4
    • 66
    • 230
    • 31
    Show worked solution

    Correct answer: -4

    The right side is grouped and multiplied before the compound subtraction updates result.

  5. 5.Code Tracing 40 - Primitive Expressions: evaluate the state change step by step.

    int a = 46;
    int b = 5;
    int c = 5;
    int d = 5;
    int result = (a + b) / (c - d);
    System.out.print(result);
    • 42
    • 5
    • 0
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: A runtime exception is thrown.

    The grouped denominator c - d is 0. Integer division by zero throws an ArithmeticException in Java.

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

    int a = 9;
    int b = 4;
    double c = a / b + (double) a / b;
    System.out.print(c);
    • 4.5
    • 4.25
    • 2.0
    • 4.0
    Show worked solution

    Correct answer: 4.25

    The first division is int arithmetic and truncates to 2. The second casts first, giving 2.25. Their sum is 4.25.

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

    int n = 7;
    n += n / 2;
    n *= 2;
    n -= n % 5;
    System.out.print(n);
    • 18
    • 15
    • 20
    • 22
    Show worked solution

    Correct answer: 20

    n becomes 7 + 3, or 10, then 20, and 20 % 5 is 0 so nothing is subtracted.

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

    int x = 5;
    int y = x++ + 2;
    int z = ++x + 2;
    System.out.print(x + " " + y + " " + z);
    • 7 8 9
    • 6 7 8
    • 7 7 8
    • 7 7 9
    Show worked solution

    Correct answer: 7 7 9

    The postfix form uses 5 then raises x to 6, so y is 7. The prefix form raises x to 7 first, so z is 9.

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

    double d = 5;
    int i = (int) (d / 2) + (int) d / 2;
    System.out.print(i);
    • 4
    • 5
    • 2
    • 6
    Show worked solution

    Correct answer: 4

    The first cast truncates 2.5 to 2. The second casts d to 5 and then divides in int arithmetic, giving 2. Their sum is 4.

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

    int p = 17;
    int r = p % 10 * 10 + p / 10;
    System.out.print(r);
    • 17
    • 71
    • 77
    • 11
    Show worked solution

    Correct answer: 71

    The modulus and division share precedence with multiplication and run left to right, so this swaps the two digits.

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

    int a = 3;
    int b = a * a - a / a + a % a;
    System.out.print(b);
    • 9
    • 7
    • 8
    • 11
    Show worked solution

    Correct answer: 8

    Multiplication, division, and modulus all bind tighter than the additive operators, giving 9 - 1 + 0.

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

    double total = 10;
    total /= 4;
    total *= 4;
    System.out.print(total);
    • 8.0
    • 10
    • 2.5
    • 10.0
    Show worked solution

    Correct answer: 10.0

    total is a double, so the division keeps 2.5 and multiplying returns exactly 10.0. An int variable would have given 8.

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

    int total = 10;
    total /= 4;
    total *= 4;
    System.out.print(total);
    • 8
    • 10
    • 2
    • 12
    Show worked solution

    Correct answer: 8

    Integer division truncates 2.5 to 2, and the information is gone, so multiplying back gives 8 rather than 10.

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

    int v = -9;
    System.out.print(v / 2 + " " + v % 2 + " " + Math.abs(v % 2));
    • -5 1 1
    • -4 -1 1
    • -4 1 1
    • -5 -1 1
    Show worked solution

    Correct answer: -4 -1 1

    Division truncates toward zero and the remainder follows the sign of the dividend, so the parts are -4 and -1.

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

    int a = 2;
    int b = 3;
    System.out.print(a + b + "" + a + b);
    • 2323
    • 55
    • 523
    • 23
    Show worked solution

    Correct answer: 523

    The first two operands are added because both are int. Once the empty string appears, everything after it concatenates.