Short-Circuit Evaluation Quiz Quiz

Explore key concepts of short-circuit evaluation in programming with this focused quiz. Assess your understanding of boolean expressions, common pitfalls, and scenarios where short-circuit logic influences code execution.

  1. Basic Boolean Expressions

    In the boolean expression (x != 0) u0026u0026 (10 / x u003E 1), what happens if x is 0?

    1. Both expressions are always evaluated
    2. The result is always true
    3. A division by zero error occurs
    4. The second expression is never evaluated

    Explanation: Short-circuit evaluation prevents the second part of an AND expression from running if the first part is false, so division by zero is avoided when x is 0. The distractor 'A division by zero error occurs' is incorrect because the error does not happen due to short-circuiting. Saying both expressions are always evaluated is inaccurate since that's not how short-circuiting works. The result is not always true; the outcome depends on the value of x.

  2. Order of Evaluation

    Given the expression (isReady() || performSetup()), which function will definitely be called every time the expression is executed?

    1. Both functions are always called
    2. Neither function is guaranteed to be called
    3. performSetup()
    4. isReady()

    Explanation: In a logical OR expression, the left-hand side function is always called first, so isReady() will always execute. 'performSetup()' may be skipped if isReady() returns true. 'Both functions are always called' ignores short-circuit evaluation. 'Neither function is guaranteed to be called' is incorrect because at least isReady() runs.

  3. Logical Errors

    Why can using side-effect functions in short-circuit expressions, like (check() u0026u0026 increment()), be risky?

    1. increment() might not execute as expected
    2. check() will never be called
    3. Short-circuiting disables all functions
    4. increment() will run twice

    Explanation: If check() returns false, increment() will never execute due to short-circuit evaluation, possibly leading to missed side effects. The distractor stating 'check() will never be called' is incorrect because it's always evaluated first. The notion that increment() will run twice is baseless, and short-circuiting does not disable all functions.

  4. Short-Circuit with OR Operator

    If you have the expression (userInput != null || validateInput(userInput)), what is true when userInput is not null?

    1. Both expressions are always executed
    2. validateInput(userInput) causes an error
    3. validateInput(userInput) is skipped
    4. userInput is always null

    Explanation: When userInput is not null, the first condition of the OR expression is true, so the second function is skipped because the result is already known. The distractor about an error is incorrect since nothing runs. Saying both are always executed ignores short-circuiting, while claiming userInput is always null is irrelevant.

  5. Short-Circuit Evaluation in Loops

    Which outcome can occur when using a short-circuit condition in a loop, such as while (hasMore() u0026u0026 getNext())?

    1. getNext() may not be called if hasMore() is false
    2. The loop will never terminate
    3. Both conditions execute regardless of order
    4. getNext() always runs before hasMore()

    Explanation: If hasMore() returns false, getNext() is skipped due to short-circuit evaluation, potentially omitting an extra action in the loop. Stating getNext() always runs before hasMore() is incorrect, as hasMore() must be evaluated first. The loop can terminate, so that distractor is wrong, and the final option fails to account for short-circuit logic.