Skip to main content

Java Topic Practice

Short-Circuit Logic

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

For practice use only.

Short-Circuit Logic MCQ Practice

Short-Circuit Logic 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 - Short-Circuit Logic: track the final printed value.

int total = 18;
int denom = 0;
int limit = 2;
boolean ok = denom != 0 && total / denom > limit;
System.out.print(ok);

All 15 short-circuit logic questions

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

  1. 1.Code Tracing 01 - Short-Circuit Logic: track the final printed value.

    int total = 18;
    int denom = 0;
    int limit = 2;
    boolean ok = denom != 0 && total / denom > limit;
    System.out.print(ok);
    • false
    • true
    • A runtime exception is thrown.
    • The code does not compile.
    Show worked solution

    Correct answer: false

    With &&, Java does not evaluate the division when denom != 0 is false.

  2. 2.Code Tracing 12 - Short-Circuit Logic: follow the variable updates.

    int views = 26;
    int extra = 7;
    boolean ok = views > 10 || extra / 0 > 1;
    System.out.print(ok);
    • The code does not compile.
    • true
    • A runtime exception is thrown.
    • false
    Show worked solution

    Correct answer: true

    The left side of || is true, so Java short-circuits and never evaluates extra / 0.

  3. 3.Code Tracing 28 - Short-Circuit Logic: trace the branch and loop path.

    String name = null;
    boolean ok = name != null && name.length() > 38;
    System.out.print(ok);
    • (no output)
    • false
    • A runtime exception is thrown.
    • true
    Show worked solution

    Correct answer: false

    Because the left side is false, && stops there and never calls length() on the null reference. This is the standard null-guard idiom.

  4. 4.Code Tracing 39 - Short-Circuit Logic: watch the index changes.

    int[] data = {11, 8, 4, 10, 6, 8};
    int i = data.length;
    boolean ok = i < data.length && data[i] > 0;
    System.out.print(ok);
    • true
    • (no output)
    • false
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: false

    The bounds check runs first and fails, so && short-circuits before the out-of-range array access is ever attempted.

  5. 5.Code Tracing 50 - Short-Circuit Logic: evaluate the state change step by step.

    int[] data = {13, 9, 6, 8, 8, 10};
    int i = data.length;
    boolean ok = data[i] > 0 && i < data.length;
    System.out.print(ok);
    • false
    • true
    • (no output)
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: A runtime exception is thrown.

    Here the guard is written second, so the array access is evaluated first and throws. Order matters when relying on short-circuiting.

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

    int[] a = {};
    boolean r = a.length > 0 && a[0] == 1;
    System.out.print(r);
    • true
    • An exception is thrown.
    • The code does not compile.
    • false
    Show worked solution

    Correct answer: false

    The length guard is false, so && never evaluates the array access and no exception occurs.

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

    String s = null;
    boolean r = s != null && s.length() > 0;
    System.out.print(r);
    • false
    • true
    • A NullPointerException is thrown.
    • The code does not compile.
    Show worked solution

    Correct answer: false

    The null check is false, so the method call is skipped. Reversing the operands would throw.

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

    int n = 0;
    boolean r = n == 0 || 10 / n > 1;
    System.out.print(r);
    • false
    • true
    • An exception is thrown.
    • The code does not compile.
    Show worked solution

    Correct answer: true

    The left operand of || is true, so the division on the right is never evaluated.

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

    int calls = 0;
    int[] d = {1, 2, 3};
    for (int i = 0; i < d.length; i++)
    {
        if (d[i] > 1 && d[i] < 3)
        {
            calls++;
        }
    }
    System.out.print(calls);
    • 2
    • 3
    • 1
    • 0
    Show worked solution

    Correct answer: 1

    Only the middle element satisfies both bounds, so the counter reaches 1.

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

    String s = "abc";
    int i = 3;
    boolean r = i < s.length() && s.charAt(i) == 'd';
    System.out.print(r);
    • true
    • An exception is thrown.
    • The code does not compile.
    • false
    Show worked solution

    Correct answer: false

    The index equals the length, so the guard fails and charAt is never called with an out-of-range index.

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

    int a = 1;
    int b = 1;
    boolean r = (a++ == 1) | (b++ == 1);
    System.out.print(r + " " + a + " " + b);
    • true 2 2
    • true 2 1
    • false 2 2
    • true 1 1
    Show worked solution

    Correct answer: true 2 2

    The single pipe is the non-short-circuit or, so both operands are evaluated and both variables are incremented.

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

    int a = 1;
    int b = 1;
    boolean r = (a++ == 1) || (b++ == 1);
    System.out.print(r + " " + a + " " + b);
    • true 2 2
    • true 2 1
    • false 2 1
    • true 1 1
    Show worked solution

    Correct answer: true 2 1

    The double pipe short-circuits after the true left operand, so b is never incremented. Compare this with the single pipe version.

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

    int[] d = {5};
    boolean r = d.length > 1 && d[1] > 0 || d[0] > 0;
    System.out.print(r);
    • false
    • An exception is thrown.
    • true
    • The code does not compile.
    Show worked solution

    Correct answer: true

    The && group short-circuits to false without touching index 1, and the || then evaluates its true second operand.

  14. 14.Why must the bounds check come first in the condition below?

    while (i < s.length() && s.charAt(i) != ' ')
    {
        i++;
    }
    • Java evaluates conditions from right to left, so the order avoids a compile error.
    • The order makes no difference, because charAt never throws.
    • Placing charAt first would make the loop run one extra time.
    • If the index reaches the length, short-circuit evaluation stops before charAt is called.
    Show worked solution

    Correct answer: If the index reaches the length, short-circuit evaluation stops before charAt is called.

    The && stops as soon as its left operand is false, which is what prevents an out-of-range call once the index reaches the length.

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

    int n = 10;
    boolean r = n > 5 || n / 0 == 1;
    boolean s2 = n < 5 && n / 0 == 1;
    System.out.print(r + " " + s2);
    • true false
    • true true
    • An exception is thrown.
    • false false
    Show worked solution

    Correct answer: true false

    Both expressions short-circuit before the division by zero, so neither throws.