Flow Mastery: Advanced Control Structures Challenge Quiz

  1. Short-Circuit Evaluation with Side Effects

    Consider two functions in a language with short-circuiting (operator precedence: u0026u0026 before ||, evaluated left-to-right): T() increments countT and returns true, and F() increments countF and returns false. Starting with countT = 0 and countF = 0, what is the outcome of evaluating the chain: if (F() u0026u0026 T()) { branch1 } else if (T() || F()) { branch2 } else { branch3 }?

    1. branch2 executes; countT = 1; countF = 1
    2. branch1 executes; countT = 1; countF = 1
    3. branch2 executes; countT = 2; countF = 1
    4. branch3 executes; countT = 0; countF = 1
    5. branch2 excutes; countT = 1; countF = 0
  2. Else Binding Without Braces

    In the following brace-less pseudo-code, assume each else binds to the nearest unmatched if: if (x u003E 0) if (y u003E 0) sign = 'Q1'; else sign = 'Non-positive x'; With x = 5 and y = -2, what is the final value of sign after this sequential conditional execution?

    1. Non-positive x
    2. Q1
    3. Non-positive y
    4. Undefined (no assignment occures)
    5. Q4
  3. Choosing the Correct Loop Invariant

    Consider this iterative minimum-finding routine over array A[0..n-1]: min = A[0]; for (i = 1; i u003C n; i++) { if (A[i] u003C min) min = A[i]; } Which statement is the strongest correct loop invariant that holds at the start of each iteration for index i?

    1. min equals the minimum of A[0..i-1].
    2. min equals the minimum of A[0..i].
    3. min is always A[0] until the loop finishes.
    4. min equals the maximum of A[0..i-1].
    5. min equals the minimum of A[1..i].
  4. Do-While Executes At Least Once

    In a language with do-while semantics (the loop body executes at least once), consider the sequential code: x = 10; count = 0; do { x = x * x - 1; count = count + 1; } while (x u003C 10); After the loop terminates, what are the final values of x and count?

    1. x = 99; count = 1
    2. x = 9; count = 0
    3. x = 99; count = 2
    4. x = 100; count = 1
    5. x = 9; count = 1
  5. Combining Continue and Break in a For Loop

    Given this iterative control flow with both continue and break, where % denotes remainder: sum = 0; for (i = 1; i u003C= 5; i++) { if (i % 2 == 0) continue; sum = sum + i; if (sum u003E 5) break; } After the loop finishes, what are the final values of sum and i?

    1. sum = 9; i = 5
    2. sum = 9; i = 6
    3. sum = 4; i = 5
    4. sum = 1; i = 5
    5. sum = 9; i = five