149k views
4 votes
Consider the following code segment.

int[][] mat = {{10, 15, 20, 25},
{30, 35, 40, 45},
{50, 55, 60, 65}};
for (int[] row : mat)
{
for (int j = 0; j < row.length; j += 2)
{
System.out.print(row[i] + " ");
}
System.out.println();
}
What, if anything, is printed as a result of executing the code segment?

Consider the following code segment. int[][] mat = {{10, 15, 20, 25}, {30, 35, 40, 45}, {50, 55, 60, 65}}; for-example-1
User Daniele B
by
5.2k points

1 Answer

3 votes

Answer:

10 20

30 40

50 60

Step-by-step explanation:

Given

The above code segment

Required

What is printed, if anything

To do this, we analyze the code line by line.

Line 1: The first line creates a 4 by 3 array named mat

Line 2: for (int[] row : mat) { -> This creates row[] array which represents each row of array mat

Line 3: for (int j = 0; j < row.length; j += 2){ -> This iterates through the even indexed elements of the row array i.e. 0 and 2

Line 4: System.out.print(row[i] + " "); -> This prints the even indexed elements of the row array.

The even indexed elements are: 10, 20, 30, 40, 50 and 60

Line 5: System.out.println(); --> This prints a new line

User Xersiee
by
6.3k points