JavaScript is required

Theo dõi đoạn code dưới đây và chọn đáp án đúng nhất:
i = 0
x = 0 while i < 10:
if i % 2 == 0:
x += 1
i += 1
x = _____.

A.
3
B.
4
C.
5
D.
0
Trả lời:

Đáp án đúng: C


The code iterates through a `while` loop from `i = 0` to `i < 10`. Inside the loop, an `if` statement checks if `i` is even (`i % 2 == 0`). If `i` is even, `x` is incremented by 1 (`x += 1`). Then, `i` is incremented by 1 (`i += 1`). Let's trace the values of `i` and `x`: - i = 0: `i % 2 == 0` is true, `x` increments to 1. `x = 1`, `i = 1` - i = 1: `i % 2 == 0` is false, `i = 2` - i = 2: `i % 2 == 0` is true, `x` increments to 2. `x = 2`, `i = 3` - i = 3: `i % 2 == 0` is false, `i = 4` - i = 4: `i % 2 == 0` is true, `x` increments to 3. `x = 3`, `i = 5` - i = 5: `i % 2 == 0` is false, `i = 6` - i = 6: `i % 2 == 0` is true, `x` increments to 4. `x = 4`, `i = 7` - i = 7: `i % 2 == 0` is false, `i = 8` - i = 8: `i % 2 == 0` is true, `x` increments to 5. `x = 5`, `i = 9` - i = 9: `i % 2 == 0` is false, `i = 10` The loop terminates when `i = 10`. The final value of `x` is 5.

Tổng hợp 200 câu hỏi trắc nghiệm lập trình Python có đáp án đầy đủ nhằm giúp các bạn dễ dàng ôn tập lại toàn bộ các kiến thức.


50 câu hỏi 60 phút

Câu hỏi liên quan