JavaScript is required

What will be the output of the program?
Media VietJack

A.
4 2 -1 1
B.
4 3 0 1
C.
3 2 -1 1
Trả lời:

Đáp án đúng: A


The code initializes an integer array `arr` with values {5, 8, 2, 10}. It then iterates through the array using a `for` loop, applying two operations to each element: first, it calculates the modulo 5 of the element (`arr[i] % 5`), and then it subtracts 1 from the result. Finally, it prints the modified element followed by a space. Let's trace the execution: - i = 0: `arr[0] = 5 % 5 = 0`; `arr[0] = 0 - 1 = -1`. Output: -1 - i = 1: `arr[1] = 8 % 5 = 3`; `arr[1] = 3 - 1 = 2`. Output: 2 - i = 2: `arr[2] = 2 % 5 = 2`; `arr[2] = 2 - 1 = 1`. Output: 1 - i = 3: `arr[3] = 10 % 5 = 0`; `arr[3] = 0 - 1 = -1`. Output: -1 Therefore, the output of the program is -1 2 1 -1.

Câu hỏi liên quan