4.5k views
4 votes
Question 3 Saved Please write the output of the following program: public static void main(String[] args){ String[] words = {"ONE", "TWO", "THREE"}; for(int i = 1; i < words.length; i++){ if(i % 2 == 0) words[i] = words[i] + i;

1 Answer

0 votes

The program will output: ONE, TWO2, THREE.

The program will output:

  • ONE
  • TWO2
  • THREE

The provided program is a loop that iterates over an array of strings called words. The loop starts at index 1 and goes up to the length of the array. Inside the loop, there is an if statement that checks if i is divisible by 2. If it is, the code appends the value of i to the corresponding element in the array.

In this case, when i = 1, the condition in the if statement is false, so the array element remains unchanged.

When i = 2, the condition is true, so the array element at index 2 (TWO) is concatenated with i, resulting in TWO2.

User Mark Skelton
by
8.4k points

Related questions