Given the code fragment:

int row = 10;
for (; row > 0;) {
    int col = row;
    while (col >= 0) {
        System.out.print(col + " ");
        col -= 2;
    }
    row = row / col;
}

What is the result?
A.

10 8 6 4 2 0

B.

10 8 6 4 2

C. AnArithmeticException is thrown at runtime
D. The program goes into an infinite loop outputting: 10 8 6 4 2 0. . .
E. Compilation fails

題解

第 13 行的 for 迴圈會在 row 變數的值大於 0 的時候執行。

第一次執行 for 迴圈,row = 10、col = 10,所以 while 迴圈會重複執行,依序印出「10 8 6 4 」……,直到 col 變數的值小於 0。最後一次執行 while 迴圈時,col 的值為 -2,小於 0 了,所以跳出 while 迴圈。

接著改變 row 的值為 10/-2=-5,然後準備進入下一次的 for 迴圈。然而,此時 row 小於等於 0 了,因此會直接跳出 for 迴圈。