176k 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?

User RTOSkit
by
5.4k points

1 Answer

3 votes

Answer:

Following are the complete code to the given question:

public class Main//main class

{

public static void main(String[] args) //main method

{

int [][] arr = {{11, 13, 14 ,15},{12, 18, 17, 26},{13, 21, 26, 29},{14, 17, 22, 28}};//defining a 2D array

int num=14;//defining an integer variable that holds a value 14

for (int j = 0; j < arr.length; j++)//defining for loop to hold row value

{

for (int k = 0; k < arr[0].length; k++)//defining for loop to hold column value

{

if (arr[j][k] == num)//defining if block that checks num value

{

System.out.print(j + k + arr[j][k] + " ");//print value

}

}

}

}

}

Output:

16 17

Step-by-step explanation:

  • In the question, we use the "length" function that is used to finds the number of rows in the array.
  • In this, the array has the 4 rows when j=0 and k=2 it found the value and add with its index value that is 0+2+14= 16.
  • similarly when j=3 and k=0 then it found and adds the value which is equal to 3+0+14=17.
  • So, the output is "16,17".
User Hookenz
by
5.2k points