127k views
1 vote
Consider the following code segment, where num is an integer variable.int[][] arr = {{11, 13, 14 ,15},{12, 18, 17, 26},{13, 21, 26, 29},{14, 17, 22, 28}};for (int j = 0; j < arr.length; j++){for (int k = 0; k < arr[0].length; k++){if (arr[j][k] == num){System.out.print(j + k + arr[j][k] + " ");}}}What is printed when num has the value 14 ?

a. 14 14
b. 18 19
c. 16 17
d. 17 16
e. 19 181/1

User Predactor
by
3.0k points

1 Answer

6 votes

Answer:

c. 16 17

Step-by-step explanation:

Given


num = 14

The above code segment

Required

The output

In the code segment, we have the following iterations:

for (int j = 0; j < arr.length; j++) and for (int k = 0; k < arr[0].length; k++)

The above iterates through all the elements of the array.

The if condition is true, only on two occasions

(i) When i = 0; j = 2

(ii) When i = 3; j = 0

i.e.

arr[0][2] = 14

arr[3[0] = 14

So, the result of the print statement is: j + k + arr[j][k]

(i) When i = 0; j = 2

j + k + arr[j][k] = 0 + 2 + 14 = 16

(ii) When i = 3; j = 0

j + k + arr[j][k] = 3 + 0 + 14 = 17

Hence, (c) 16 17 is true

User Noah Wilder
by
3.3k points