IT168 Skill Assessment Test 2 Definitive Study Guide

General Course Overview and Assessment Guidelines

  • Course Information: IT168 — Structured Problem Solving Using The Computer (Fall 2026 Skill Assessment Test 2 Practice Set).

  • Core Subject Focus: Heavy emphasis on control structures, including decisions, single and nested loops, sentinel values, input validation, and structured logical thinking.

  • Test Section Structure and Scoring:

    • Section A (Multiple Choice): 30 questions (M01 through M30), worth 5 points each (total 150 points).

    • Section B (Short Answer): 30 multi-part questions (S01 through S30), worth 10 points each (total 300 points).

    • Section C (Programming Problems): 40 individual programming problems (P01 through P40), worth 50 points each.

Section structure overview

Execution, Tracing, and Programming Environment Rules

  • Timed Practice Protocol:

    • Complete programming problems in one full hour per problem.

    • Write code using a plain-text editor or physical paper without relying on Eclipse IDE, autocomplete features, or automated compiler error corrections.

    • Complete a manual trace and logic verification before compiling code.

  • Snippet Execution Context:

    • All code snippets are assumed to execute within public static void main(String[] args).

    • Standard import import java.util.Scanner; is assumed to be present.

    • Variables explicitly mentioned as pre-declared are available in scope.

    • Snippets are completely independent and may contain intentional syntax or runtime errors.

  • Code Tracing Rules:

    • Iteration Definition: An iteration is defined as one entry into a loop body, including any entry prematurely interrupted by break or continue statements.

    • Condition Evaluation: Loop condition checks must be counted separately from loop body executions.

    • Output Format: Evaluate exact program-produced text (ignore prompt strings or stack trace messages). Quotes in answer choices delimit string literals and are not output. "then" denotes line breaks. Preserve leading whitespace, literal dots, and blank lines; ignore trailing spaces.

  • Scope and Syntax Restrictions:

    • Allowed Java Features: Standard primitive types, arithmetic operators, relational/logical operators (&&, ||, !), if-else branching, while, do-while, basic for loops, break, continue, Scanner input, and basic String methods (length(), charAt()).

    • Prohibited Features: Custom methods or classes outside main, arrays (except the unused args parameter), Java Collections Framework, recursion, Java Streams, regular expressions, String.split(), StringBuilder, or String.repeat().

  • Data and Program Input Assumptions:

    • All numeric input values are valid integers unless specified otherwise.

    • Source code file name must match the class name with .java extension.

    • Sentinels and rejected values do not count as valid data.

Section A: Multiple Choice Questions and Detailed Traces

  • M01: Updating a running total

    • Project: IT168_SA2_M01 | Package: it168.sa2.m01 | Class: QuestionM01

    • Code Snippet:java int x = 1; int total = 0; while (x < 6) { total += x + 1; System.out.println(x + ":" + total); x += 2; } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • Initial: x = 1, total = 0

    • Iteration 1: x = 1 (1<61 < 6 is true). total += 2 (\n      total = 2\n      ). Prints 1:2. x becomes 3.

    • Iteration 2: x = 3 (3<63 < 6 is true). total += 4 (\n      total = 6\n      ). Prints 3:6. x becomes 5.

    • Iteration 3: x = 5 (5<65 < 6 is true). total += 6 (\n      total = 12\n      ). Prints 5:12. x becomes 7.

    • Termination: x = 7 (7<67 < 6 is false).

    • Answer: A ("1:2", then "3:6", then "5:12")

  • M02: A first pass through do-while

    • Project: IT168_SA2_M02 | Package: it168.sa2.m02 | Class: QuestionM02

    • Code Snippet:java int n = 8; do { System.out.print(n + " "); n -= 2; } while (n < 5); System.out.println("|" + n); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • Iteration 1: Prints 8. n updated to 6. Condition check 6<56 < 5 is false. Loop terminates.

    • After loop: Prints |6.

    • Total Output: 8 |6

    • Answer: B ("8 |6")

  • M03: A descending for loop

    • Project: IT168_SA2_M03 | Package: it168.sa2.m03 | Class: QuestionM03

    • Code Snippet:java for (int value = 11; value >= 2; value -= 3) { System.out.println(value); } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • value = 11 (11≥211 \ge 2): executes body (1)

    • value = 8 (8≥28 \ge 2): executes body (2)

    • value = 5 (5≥25 \ge 2): executes body (3)

    • value = 2 (2≥22 \ge 2): executes body (4)

    • value = -1 (−1≥2-1 \ge 2 is false): loop ends.

    • Answer: D (4)

  • M04: A multiplicative update

    • Project: IT168_SA2_M04 | Package: it168.sa2.m04 | Class: QuestionM04

    • Code Snippet:java int value = 3; int rounds = 0; while (value < 80) { value *= 3; rounds++; } System.out.println(rounds + " " + value); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • Initial: value = 3, rounds = 0

    • Check 3<803 < 80 (true) -> Iteration 1: value = 9, rounds = 1

    • Check 9<809 < 80 (true) -> Iteration 2: value = 27, rounds = 2

    • Check 27<8027 < 80 (true) -> Iteration 3: value = 81, rounds = 3

    • Check 81<8081 < 80 (false) -> Loop exits.

    • Output: 3 81

    • Answer: D ("3 81")

  • M05: continue in a for loop

    • Project: IT168_SA2_M05 | Package: it168.sa2.m05 | Class: QuestionM05

    • Code Snippet:java for (int i = 1; i <= 6; i++) { if (i % 3 == 0) { continue; } System.out.print(i + " "); } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace: i values 1,2,3,4,5,61, 2, 3, 4, 5, 6. When i = 3 or i = 6, i % 3 == 0 evaluates to true, so continue skips the print statement. Printed values: 1 2 4 5.

    • Answer: C (1 2 4 5)

  • M06: continue in a do-while loop

    • Project: IT168_SA2_M06 | Package: it168.sa2.m06 | Class: QuestionM06

    • Code Snippet:java int n = 0; do { n++; if (n % 2 == 0) { continue; } System.out.print(n + " "); } while (n < 5); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • n becomes 1: 1 % 2 == 0 false, prints 1, condition 1<51 < 5 true.

    • n becomes 2: 2 % 2 == 0 true, continue skips print, condition 2<52 < 5 true.

    • n becomes 3: 3 % 2 == 0 false, prints 3, condition 3<53 < 5 true.

    • n becomes 4: 4 % 2 == 0 true, continue skips print, condition 4<54 < 5 true.

    • n becomes 5: 5 % 2 == 0 false, prints 5, condition 5<55 < 5 false -> loop terminates.

    • Answer: A (1 3 5)

  • M07: Where break occurs

    • Project: IT168_SA2_M07 | Package: it168.sa2.m07 | Class: QuestionM07

    • Code Snippet:java int total = 0; for (int n = 1; n <= 8; n++) { total += n; if (total >= 10) { break; } System.out.println(n + ":" + total); } System.out.println("Total=" + total); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • n = 1: total = 1, prints 1:1

    • n = 2: total = 3, prints 2:3

    • n = 3: total = 6, prints 3:6

    • n = 4: total = 10 (10≥1010 \ge 10 true) -> break terminates loop before printing 4:10.

    • Final print: Total=10

    • Answer: A ("1:1", then "2:3", then "3:6", then "Total=10")

  • M08: A sentinel and a filtered sum

    • Project: IT168_SA2_M08 | Package: it168.sa2.m08 | Class: QuestionM08

    • Input Tokens: 7 -2 4 0 99

    • Code Snippet:java Scanner input = new Scanner(System.in); int value = input.nextInt(); int count = 0; int sum = 0; while (value != 0) { count++; if (value > 0) { sum += value; } value = input.nextInt(); } System.out.println(count + " " + sum); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • Read 7: count = 1, sum = 7, read -2

    • Read -2: count = 2, sum = 7 (not positive), read 4

    • Read 4: count = 3, sum = 11, read 0

    • Read 0: condition value != 0 fails, loop ends.

    • Prints: 3 11 (99 remains unread).

    • Answer: D ("3 11")

  • M09: Counting validation attempts

    • Project: IT168_SA2_M09 | Package: it168.sa2.m09 | Class: QuestionM09

    • Input Tokens: -1 11 6 8

    • Code Snippet:java Scanner input = new Scanner(System.in); int value; int reads = 0; do { value = input.nextInt(); reads++; } while (value < 1 || value > 10); System.out.println(reads + ":" + value); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • Read -1: reads = 1, condition −1<1-1 < 1 true.

    • Read 11: reads = 2, condition 11>1011 > 10 true.

    • Read 6: reads = 3, condition 6<1∥6>106 < 1 \parallel 6 > 10 false -> loop ends.

    • Output: 3:6

    • Answer: A ("3:6")

  • M10: A short-circuit guard

    • Project: IT168_SA2_M10 | Package: it168.sa2.m10 | Class: QuestionM10

    • Code Snippet:java int divisor = 3; while (divisor != 0 && 12 / divisor > 2) { System.out.print(divisor + " "); divisor--; } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • divisor = 3: 3≠03 \neq 0 and 12/3=4>212/3 = 4 > 2 (true), prints 3, divisor becomes 2.

    • divisor = 2: 2≠02 \neq 0 and 12/2=6>212/2 = 6 > 2 (true), prints 2, divisor becomes 1.

    • divisor = 1: 1≠01 \neq 0 and 12/1=12>212/1 = 12 > 2 (true), prints 1, divisor becomes 0.

    • divisor = 0: divisor != 0 evaluates to false; short-circuit evaluation prevents 12 / 0 execution. Loop ends cleanly.

    • Answer: D (It prints 3 2 1 and terminates normally.)

  • M11: Combining divisibility conditions

    • Project: IT168_SA2_M11 | Package: it168.sa2.m11 | Class: QuestionM11

    • Code Snippet:java for (int i = 1; i <= 7; i++) { if (i % 2 == 0 || i % 3 == 0 && i > 4) { System.out.print(i + " "); } } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace: Precedence of && is higher than ||. Condition is (i % 2 == 0) || ((i % 3 == 0) && (i > 4)).

    • i = 1: false

    • i = 2: 2%2==02 \% 2 == 0 true -> prints 2

    • i = 3: false || (true && false) -> false

    • i = 4: 4%2==04 \% 2 == 0 true -> prints 4

    • i = 5: false

    • i = 6: 6%2==06 \% 2 == 0 true -> prints 6

    • i = 7: false

    • Answer: D (2 4 6)

  • M12: Negating an interval test

    • Project: IT168_SA2_M12 | Package: it168.sa2.m12 | Class: QuestionM12

    • Code Snippet:java int x = 9; int steps = 0; while (!(x >= 2 && x <= 4)) { x -= 2; steps++; } System.out.println(steps + " " + x); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace: Condition !(x >= 2 && x <= 4) means loop continues while x < 2 or x > 4.

    • x = 9: not in [2,4][2,4] -> x = 7, steps = 1

    • x = 7: not in [2,4][2,4] -> x = 5, steps = 2

    • x = 5: not in [2,4][2,4] -> x = 3, steps = 3

    • x = 3: 3∈[2,4]3 \in [2,4] -> condition false, loop ends.

    • Output: 3 3

    • Answer: B ("3 3")

  • M13: A rectangular nested loop

    • Project: IT168_SA2_M13 | Package: it168.sa2.m13 | Class: QuestionM13

    • Code Snippet:java int visits = 0; for (int row = 1; row <= 4; row++) { for (int col = 2; col <= 7; col += 2) { visits++; } } System.out.println(visits); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace: Outer loop runs 4 times (row = 1, 2, 3, 4). Inner loop runs 3 times per row (col = 2, 4, 6). Total iterations = 4×3=124 \times 3 = 12.

    • Answer: C (12)

  • M14: A changing inner bound

    • Project: IT168_SA2_M14 | Package: it168.sa2.m14 | Class: QuestionM14

    • Code Snippet:java int visits = 0; for (int row = 1; row <= 4; row++) { for (int col = 1; col <= row; col += 2) { visits++; } } System.out.println(visits); &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • row = 1: col = 1 (1 visit)

    • row = 2: col = 1 (1 visit)

    • row = 3: col = 1, 3 (2 visits)

    • row = 4: col = 1, 3 (2 visits)

    • Total visits = 1+1+2+2=61 + 1 + 2 + 2 = 6.

    • Answer: B (6)

  • M15: The order of nested iterations

    • Project: IT168_SA2_M15 | Package: it168.sa2.m15 | Class: QuestionM15

    • Code Snippet:java for (int row = 1; row <= 2; row++) { for (int col = 1; col <= row + 1; col++) { System.out.print(row + "/" + col + " "); } } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • row = 1: col ranges 1 to 2 -> prints 1/1 1/2

    • row = 2: col ranges 1 to 3 -> prints 2/1 2/2 2/3

    • Answer: D (1/1 1/2 2/1 2/2 2/3)

  • M16: Skipping the diagonal

    • Project: IT168_SA2_M16 | Package: it168.sa2.m16 | Class: QuestionM16

    • Code Snippet:java for (int row = 1; row <= 3; row++) { for (int col = 1; col <= 3; col++) { if (row == col) { continue; } System.out.print(col); } System.out.println(); } &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution Trace:

    • row = 1: skip col = 1, print 2, 3 -> 23

    • row = 2: print 1, skip col = 2, print 3 -> 13

    • row = 3: print 1, 2, skip col = 3 -> 12

    • Answer: A ("23", then "13", then "12")

  • M17: Breaking only the inner loop

    • Project: IT168_SA2_M17 | Package: it168.sa2.m17 | Class: QuestionM17

    • Code Snippet:

    int count = 0;
    for (int row = 1; row <= 3; row++) {
        for (int col = 1; col <= 4; col++) {
            if (row + col > 4) {
                break;
            }
            count++;
        }
    }
    System.out.println(count);
    &nbsp;&nbsp;&nbsp;&nbsp;```
    * **Execution Trace:**
    * `row = 1`: `col = 1, 2, 3` (1+1,1+2,1+3≤41+1, 1+2, 1+3 \le 4) -> `count` increases by 3; `col = 4` breaks.
    * `row = 2`: `col = 1, 2` (2+1,2+2≤42+1, 2+2 \le 4) -> `count` increases by 2; `col = 3` breaks.
    * `row = 3`: `col = 1` (3+1≤43+1 \le 4) -> `count` increases by 1; `col = 2` breaks.
    * Total `count` = 3 + 2 + 1 = 6$.\n  * **Answer:** B (6)\n\n* **M18: Skipping an entire row**\n  * **Project:** `IT168_SA2_M18` | **Package:** `it168.sa2.m18` | **Class:** `QuestionM18`\n  * **Code Snippet:**\n    ```java\n    int row = 0;\n    while (row < 3) {\n        row++;\n        if (row == 2) {\n            continue;\n        }\n        for (int col = 1; col <= row; col++) {\n            System.out.print(row);\n        }\n        System.out.println();\n    }\n    ```\n  * **Execution Trace:**\n    * `row = 1`: prints `1`, then newline.\n    * `row = 2`: `continue` skips loop body and newline.\n    * `row = 3`: prints `333`, then newline.\n  * **Answer:** A ("1", then "333")\n\n* **M19: An inner counter that is not reset**\n  * **Project:** `IT168_SA2_M19` | **Package:** `it168.sa2.m19` | **Class:** `QuestionM19`\n  * **Code Snippet:**\n    ```java\n    int col = 1;\n    for (int row = 1; row <= 3; row++) {\n        while (col <= 2) {\n            System.out.print(col);\n            col++;\n        }\n        System.out.println("#");\n    }\n    ```\n  * **Execution Trace:**\n    * `row = 1`: `col = 1` prints `1`, `col = 2` prints `2`, `col` becomes `3`. Prints `#`. Output: `12#`\n    * `row = 2`: `col = 3`, 3 \le 2 false. Prints `#`. Output: `#`\n    * `row = 3`: `col = 3`, 3 \le 2 false. Prints `#`. Output: `#`\n  * **Answer:** C ("12#", then "#", then "#")\n\n* **M20: A subtotal for each row**\n  * **Project:** `IT168_SA2_M20` | **Package:** `it168.sa2.m20` | **Class:** `QuestionM20`\n  * **Code Snippet:**\n    ```java\n    for (int row = 1; row <= 3; row++) {\n        int total = 0;\n        for (int col = 1; col <= row; col++) {\n            total += col;\n        }\n        System.out.print(total + " ");\n    }\n    ```\n  * **Execution Trace:**\n    * `row = 1`: `total` = 1 -> prints `1 `\n    * `row = 2`: `total` = 1 + 2 = 3 -> prints `3 `\n    * `row = 3`: `total` = 1 + 2 + 3 = 6 -> prints `6 `\n  * **Answer:** C (1 3 6)\n\n* **M21: The scope of a for-loop variable**\n  * **Project:** `IT168_SA2_M21` | **Package:** `it168.sa2.m21` | **Class:** `QuestionM21`\n  * **Code Snippet:**\n    ```java\n    for (int k = 0; k < 3; k++) {\n        System.out.print(k + " ");\n    }\n    System.out.println(k);\n    ```\n  * **Explanation:** `k` is declared in the `for` loop header and its scope is limited to the loop block. Referencing `k` outside the loop causes a compilation error.\n  * **Answer:** B (It does not compile because k is out of scope in the final statement.)\n\n* **M22: The end of a do-while statement**\n  * **Project:** `IT168_SA2_M22` | **Package:** `it168.sa2.m22` | **Class:** `QuestionM22`\n  * **Code Snippet:**\n    ```java\n    int n = 3;\n    do {\n        System.out.println(n);\n        n--;\n    } while (n > 0)\n    ```\n  * **Explanation:** A `do-while` statement requires a terminating semicolon `;` after the condition expression `while (n > 0);`. Without it, the code fails to compile.\n  * **Answer:** C (It does not compile because the do-while statement lacks its ending semicolon.)\n\n* **M23: The type of a loop condition**\n  * **Project:** `IT168_SA2_M23` | **Package:** `it168.sa2.m23` | **Class:** `QuestionM23`\n  * **Code Snippet:**\n    ```java\n    int count = 3;\n    while (count) {\n        System.out.println(count);\n        count--;\n    }\n    ```\n  * **Explanation:** Java requires a expression evaluating to type `boolean` in control conditions. Passing an integer expression like `count` results in a compilation error.\n  * **Answer:** C (It does not compile because the condition has type int, not boolean.)\n\n* **M24: An error after several iterations**\n  * **Project:** `IT168_SA2_M24` | **Package:** `it168.sa2.m24` | **Class:** `QuestionM24`\n  * **Code Snippet:**\n    ```java\n    for (int divisor = 3; divisor >= 0; divisor--) {\n        System.out.print(12 / divisor + " ");\n    }\n    ```\n  * **Execution Trace:** `divisor = 3` prints `4 `, `divisor = 2` prints `6 `, `divisor = 1` prints `12 `. When `divisor = 0`, evaluating `12 / 0` throws an `ArithmeticException` at runtime.\n  * **Answer:** A (It prints 4 6 12, then integer division by zero causes a runtime error.)\n\n* **M25: An extra semicolon**\n  * **Project:** `IT168_SA2_M25` | **Package:** `it168.sa2.m25` | **Class:** `QuestionM25`\n  * **Code Snippet:**\n    ```java\n    int k;\n    for (k = 1; k <= 3; k++); {\n        System.out.println(k);\n    }\n    ```\n  * **Execution Trace:** The semicolon `;` immediately following the `for` header acts as an empty statement body. The loop iterates until `k = 4`. Afterwards, the independent block `{ System.out.println(k); }` executes once, printing `4`.\n  * **Answer:** B (4 on one line)\n\n* **M26: A skipped update**\n  * **Project:** `IT168_SA2_M26` | **Package:** `it168.sa2.m26` | **Class:** `QuestionM26`\n  * **Code Snippet:**\n    ```java\n    int n = 2;\n    while (n < 8) {\n        if (n % 2 == 0) {\n            continue;\n        }\n        n++;\n    }\n    System.out.println(n);\n    ```\n  * **Execution Trace:** `n = 2`. 2 \% 2 == 0evaluatestotrue.‘continue‘skips‘n++‘andjumpsbacktoconditionevaluationevaluates to true. `continue` skips `n++` and jumps back to condition evaluation2 < 8. `n` remains 2, creating an infinite loop.\n  * **Answer:** C (It compiles, but never reaches the print statement because n stays 2.)\n\n* **M27: A String loop boundary**\n  * **Project:** `IT168_SA2_M27` | **Package:** `it168.sa2.m27` | **Class:** `QuestionM27`\n  * **Code Snippet:**\n    ```java\n    String text = "LOOP";\n    for (int i = 0; i <= text.length(); i++) {\n        System.out.print(text.charAt(i));\n    }\n    ```\n  * **Execution Trace:** `text.length()` returns 4. Valid indices are 0 to 3. Loop iterates `i` from 0 to 4 (`i <= 4`). Prints `L`, `O`, `O`, `P`. When `i = 4`, `text.charAt(4)` throws a `StringIndexOutOfBoundsException` at runtime.\n  * **Answer:** D (It prints LOOP, then an out-of-bounds character access causes a runtime error.)\n\n* **M28: Rows with no inner iterations**\n  * **Project:** `IT168_SA2_M28` | **Package:** `it168.sa2.m28` | **Class:** `QuestionM28`\n  * **Code Snippet:**\n    ```java\n    for (int row = 3; row >= 1; row--) {\n        for (int col = row; col < 3; col++) {\n            System.out.print(col);\n        }\n        System.out.print("|");\n    }\n    ```\n  * **Execution Trace:**\n    * `row = 3`: inner condition 3 < 3 false (0 iterations) -> prints `|`\n    * `row = 2`: inner condition 2 < 3 true -> prints `2`, loop ends -> prints `|`\n    * `row = 1`: inner condition 1 < 3prints‘1‘,prints `1`,2 < 3 prints `2` -> prints `12`, loop ends -> prints `|`\n    * Total output: `|2|12|`\n  * **Answer:** B ("|2|12|")\n\n* **M29: Changing the control variable in the body**\n  * **Project:** `IT168_SA2_M29` | **Package:** `it168.sa2.m29` | **Class:** `QuestionM29`\n  * **Code Snippet:**\n    ```java\n    for (int i = 1; i <= 7; i++) {\n        if (i % 2 == 1) {\n            i++;\n        }\n        System.out.print(i + " ");\n    }\n    ```\n  * **Execution Trace:**\n    * `i = 1`: odd -> `i++` makes `i = 2`, prints `2 `, loop increment makes `i = 3`.\n    * `i = 3`: odd -> `i++` makes `i = 4`, prints `4 `, loop increment makes `i = 5`.\n    * `i = 5`: odd -> `i++` makes `i = 6`, prints `6 `, loop increment makes `i = 7`.\n    * `i = 7`: odd -> `i++` makes `i = 8`, prints `8 `, loop increment makes `i = 9` (9 \le 7 false) -> loop ends.\n  * **Answer:** B (2 4 6 8)\n\n* **M30: Turning row and column tests into a pattern**\n  * **Project:** `IT168_SA2_M30` | **Package:** `it168.sa2.m30` | **Class:** `QuestionM30`\n  * **Code Snippet:**\n    ```java\n    for (int row = 1; row <= 3; row++) {\n        for (int col = 1; col <= 4; col++) {\n            if (col <= row) {\n                System.out.print(row);\n            } else {\n                System.out.print(".");\n            }\n        }\n        System.out.println();\n    }\n    ```\n  * **Execution Trace:**\n    * `row = 1`: `col = 1` prints `1`, `col = 2,3,4` prints `.` -> `1...`\n    * `row = 2`: `col = 1,2` prints `2`, `col = 3,4` prints `.` -> `22..`\n    * `row = 3`: `col = 1,2,3` prints `3`, `col = 4` prints `.` -> `333.`\n  * **Answer:** A ("1...", then "22..", then "333.")\n\n# Section B: Short Answer Questions and Trace Analyses\n\n* **S01: Trace a changing remainder**\n  * **Project:** `IT168_SA2_S01` | **Package:** `it168.sa2.s01` | **Class:** `QuestionS01`\n  * **Snippet:**\n    ```java\n    int n = 10;\n    int total = 0;\n    while (n > 1) {\n        total += n % 3;\n        System.out.println(n + " " + total);\n        n -= 3;\n    }\n    System.out.println("End:" + n);\n    ```\n  * **(a) Trace Table:**\n\n| Iteration | `n` at entry | `n % 3` | `total` after addition | `n` after update |\n| :--- | :--- | :--- | :--- | :--- |\n| 1 | 10 | 1 | 1 | 7 |\n| 2 | 7 | 1 | 2 | 4 |\n| 3 | 4 | 1 | 3 | 1 |\n\n  * **(b) Output and Body Executions:**\n    * Output:\n      ```text\n      10 1\n      7 2\n      4 3\n      End:1\n      ```\n    * Loop-body executions: 3 times.\n\n* **S02: Body executions versus condition checks**\n  * **Project:** `IT168_SA2_S02` | **Package:** `it168.sa2.s02` | **Class:** `QuestionS02`\n  * **Snippet:**\n    ```java\n    int n = 1;\n    int total = 0;\n    do {\n        total += n;\n        System.out.println(n + ":" + total);\n        n *= 2;\n    } while (total < 10);\n    ```\n  * **(a) Output and Final Values:**\n    * Output:\n      ```text\n      1:1\n      2:3\n      4:7\n      8:15\n      ```\n    * Final values: `n = 16`, `total = 15`.\n  * **(b) Execution and Evaluation Counts:**\n    * Loop body executions: 4 times.\n    * Condition evaluations (`total < 10`): 4 times.\n    * Explanation: In a post-test `do-while` loop, the body executes unconditionally on the first pass before evaluating the condition. Each subsequent pass executes the body and tests the condition at the end. Thus, body executions match condition evaluations.\n\n* **S03: Translate for into while**\n  * **Project:** `IT168_SA2_S03` | **Package:** `it168.sa2.s03` | **Class:** `QuestionS03`\n  * **Snippet:**\n    ```java\n    int i;\n    int sum = 0;\n    for (i = 2; i <= 11; i += 3) {\n        sum += i;\n        System.out.println(i + ":" + sum);\n    }\n    System.out.println("Final:" + i);\n    ```\n  * **(a) Complete Output:**\n    ```text\n    2:2\n    5:7\n    8:15\n    11:26\n    Final:14\n    ```\n  * **(b) Equivalent while loop:**\n    ```java\n    int i = 2;\n    int sum = 0;\n    while (i <= 11) {\n        sum += i;\n        System.out.println(i + ":" + sum);\n        i += 3;\n    }\n    System.out.println("Final:" + i);\n    ```\n\n* **S04: Preserve a zero-iteration case**\n  * **Project:** `IT168_SA2_S04` | **Package:** `it168.sa2.s04` | **Class:** `QuestionS04`\n  * **Snippet:**\n    ```java\n    while (n > 0) {\n        System.out.println(n);\n        n--;\n    }\n    ```\n  * **(a) Equivalent Guarded do-while Structure:**\n    ```java\n    if (n > 0) {\n        do {\n            System.out.println(n);\n            n--;\n        } while (n > 0);\n    }\n    ```\n  * **(b) Evaluation for Initial Values 0 and 3:**\n    * Initial `n = 0`: Produces no output; final `n = 0`.\n    * Initial `n = 3`: Prints `3`, `2`, `1` on separate lines; final `n = 0`.\n    * Explanation: An unguarded `do-while` loop executes its body at least once before testing the loop condition. If `n = 0`, an unguarded `do-while` would execute the body, printing `0` and reducing `n` to `-1`, violating original behavior.\n\n* **S05: Sentinel, rejected input, and unread input**\n  * **Project:** `IT168_SA2_S05` | **Package:** `it168.sa2.s05` | **Class:** `QuestionS05`\n  * **Input Tokens:** `8 -4 0 5 -99 7`\n  * **Snippet:**\n    ```java\n    Scanner input = new Scanner(System.in);\n    int count = 0;\n    int total = 0;\n    while (true) {\n        int value = input.nextInt();\n        if (value == -99) {\n            break;\n        }\n        if (value < 0) {\n            continue;\n        }\n        count++;\n        total += value;\n    }\n    System.out.println(count + ":" + total);\n    ```\n  * **(a) Output and Categorization:**\n    * Output: `3:13`\n    * Accepted values: `8`, `0`, `5`\n    * Rejected value: `-4`\n    * Sentinel value: `-99`\n  * **(b) Loop Entries and Unread Tokens:**\n    * Loop body entries: 5 times (for tokens `8`, `-4`, `0`, `5`, `-99`).\n    * Unread token: `7`.\n\n* **S06: Preserve continue during loop conversion**\n  * **Project:** `IT168_SA2_S06` | **Package:** `it168.sa2.s06` | **Class:** `QuestionS06`\n  * **(a) Output:** `1 3 5 `\n  * **(b) Equivalent while loop:**\n    ```java\n    int i = 1;\n    while (i <= 6) {\n        if (i % 2 == 0) {\n            i++;\n            continue;\n        }\n        System.out.print(i + " ");\n        i++;\n    }\n    ```\n    * Explanation: In a `for` loop, the update statement (`i++`) executes automatically after `continue`. In a `while` loop, the increment must explicitly precede `continue` inside the conditional block to prevent an infinite loop.\n\n* **S07: continue and the bottom test**\n  * **Project:** `IT168_SA2_S07` | **Package:** `it168.sa2.s07` | **Class:** `QuestionS07`\n  * **Snippet:**\n    ```java\n    int n = 5;\n    do {\n        n--;\n        if (n == 3) {\n            continue;\n        }\n        System.out.println(n);\n    } while (n > 1);\n    ```\n  * **(a) Complete Output and Final `n`:**\n    * Output:\n      ```text\n      4\n      2\n      1\n      ```\n    * Final `n = 1`.\n  * **(b) Iteration Breakdown:**\n    * Iteration 1: `n` decreases to `4`. Prints `4`. Test 4 > 1 is true.\n    * Iteration 2: `n` decreases to `3`. `continue` triggers. Printing is skipped. Test 3 > 1 is true.\n    * Iteration 3: `n` decreases to `2`. Prints `2`. Test 2 > 1 is true.\n    * Iteration 4: `n` decreases to `1`. Prints `1`. Test 1 > 1 is false. Loop ends.\n    * Answer: `continue` does **not** skip the bottom condition test; it jumps directly to evaluating `while (n > 1)`.\n\n* **S08: Repair a sentinel average**\n  * **Project:** `IT168_SA2_S08` | **Package:** `it168.sa2.s08` | **Class:** `QuestionS08`\n  * **(a) Flaw Analysis:**\n    * For input `6 10 -1`: The loop adds `-1` to `total` (6 + 10 - 1 = 15)andincrements‘count‘to‘3‘.Itcomputes) and increments `count` to `3`. It computes15 / 3 = 5.0insteadofinstead of(6+10)/2 = 8.0.\n    * For input `-1`: The loop adds `-1` to `total` and sets `count` to `1`, computing -1.0$$ instead of detecting empty input.
    * **(b) Repaired Implementation:**
    

    java_standalone Scanner input = new Scanner(System.in); int value = input.nextInt(); int total = 0; int count = 0; while (value != -1) { total += value; count++; value = input.nextInt(); } if (count == 0) { System.out.println("No data"); } else { System.out.println((double) total / count); }     ```

  • S09: Separate compilation and boundary errors

    • Project: IT168_SA2_S09 | Package: it168.sa2.s09 | Class: QuestionS09

    • (a) Analysis:

    • Does not compile. i is declared in the for loop header, making it out of scope in `System.out.println(