JavaScript is required

Cho đoạn chương trình. Giá trị của biến n là:
int n = 0;
for (int i = 0; i < 10; i += 4) n += i;

A.
8
B.
12
C.
16
D.
20
Trả lời:

Đáp án đúng: B


The given program will execute a `for` loop with the variable `i` initialized to 0, the loop condition `i < 10`, and the increment step `i += 4`. Inside the loop, the value of `i` will be added to the variable `n`. Let's trace the values of `i` in each iteration: - Iteration 1: `i = 0`, `n = n + i = 0 + 0 = 0` - Iteration 2: `i = 4`, `n = n + i = 0 + 4 = 4` - Iteration 3: `i = 8`, `n = n + i = 4 + 8 = 12` After the 3rd iteration, `i = 8 + 4 = 12`, the condition `i < 10` is no longer true, and the loop terminates. Therefore, the final value of `n` is 12. So the correct answer is B. 12

Tổng hợp 600+ câu hỏi trắc nghiệm lập trình C 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