JavaScript is required

What will be output of the program?
Media VietJack

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

Đáp án đúng: D


The code initializes an array `arr` of size 5. The loop calculates each element based on the condition `i > 2`. If `i` is greater than 2, `arr[i]` is `i + 1`; otherwise, it's `i`. The program then prints `arr[1]`, `arr[2]`, and `arr[3]`. Let's trace the execution: - i = 0: arr[0] = 0 + (0 > 2 ? 1 : 0) = 0 - i = 1: arr[1] = 1 + (1 > 2 ? 1 : 0) = 1 - i = 2: arr[2] = 2 + (2 > 2 ? 1 : 0) = 2 - i = 3: arr[3] = 3 + (3 > 2 ? 1 : 0) = 3 + 1 = 4 - i = 4: arr[4] = 4 + (4 > 2 ? 1 : 0) = 4 + 1 = 5 Therefore, arr[1] = 1, arr[2] = 2, and arr[3] = 4. The output will be 1 2 4.

Câu hỏi liên quan