177k views
4 votes
What is the output of the program below? public class Java1234 { public static void main(String args[ ]) { int matrix[ ][ ]; matrix = new int[3][4]; for(int p = 0; p < 3; p++) { for(int q = 0; q < 4; q++) System.out.prin

2 Answers

3 votes

Actual Question:

What is the output of the program below?

public class Java1234 {

public static void main(String args[ ]) {

int matrix[ ][ ];

matrix = new int[3][4];

for(int p = 0; p < 3; p++) {

for(int q = 0; q < 4; q++)

System.out.print(matrix[p][q]);

}

}

Answer:

000000000000

Explanation:

Going through the code line after line;

=> Line 1

public class Java1234 {

This is just a class declaration where the class name is Java1234. Every program in Java is run in a class

=> Line 2

public static void main(String args [ ] ) {

This is the main method declaration. Execution in Java starts at the main method.

=> Line 3

int matrix [ ][ ];

This is a declaration of a 2 dimensional array matrix to hold integer variables.

=> Line 4

matrix = new int[3][4];

Here the array variable matrix has been initialized to be an array of 3 rows and 4 columns. Therefore, it can hold 12 integer variables.

In Java, when an array is initialized this way, its values assume their default values. And since it is an array of integers, integer variables have a default value of zero(0). Therefore, the array matrix contains 12 integer variables with each value set to 0.

=> Lines 5 through 8

for (int p = 0; p < 3; p++) {

for (int q = 0; q < 4; q++)

System.out.print(matrix[p][q]);

}

This is a nested for loop that cycles through each of the element in the array.

The outer loop goes from 0 to 2 (less than 3). And at each cycle, the inner loop goes from 0 to 3 (less than 4) and prints the array element at the index determined by p and q. i.e

when p = 0, q will go from 0 to 3

=> at p = 0, q = 0, 1, 2, 3

=> for each value of q, the matrix[p][q] will be calculated as follows

matrix[0][0] = 0

matrix[0][1] = 0

matrix[0][2] = 0

matrix[0][3] = 0

when p = 1, q will go from 0 to 3

=> at p = 1, q = 0, 1, 2, 3

=> for each value of q, the matrix[p][q] will be calculated as follows

matrix[1][0] = 0

matrix[1][1] = 0

matrix[1][2] = 0

matrix[1][3] = 0

when p = 2, q will go from 0 to 3

=> at p = 2, q = 0, 1, 2, 3

=> for each value of q, the matrix[p][q] will be calculated as follows

matrix[2][0] = 0

matrix[2][1] = 0

matrix[2][2] = 0

matrix[2][3] = 0

Therefore, the loop will run 12 times altogether and at each cycle, it prints 0 since the array has not been actually initialized with any value and so it assumes its default value which is 0 for an integer.

Output:

000000000000

User Zedfoxus
by
4.5k points
2 votes

Given the complete question to be:

What is the output of the program below?

public class Java1234 {

public static void main(String args[ ]) {

int matrix[ ][ ];

matrix = new int[3][4];

for(int p = 0; p < 3; p++) {

for(int q = 0; q < 4; q++)

System.out.print(matrix[p][q]);

}

}

}

Answer:

000000000000

Step-by-step explanation:

The output of program will be 0's in twelve places since the line " int matrix[ ][ ];" declared an array of unknown size which will consist of zeros as it's element.

The line "matrix = new int[3][4];" declared the size of the matrix to be 3 by 4 while the for loop printed the elements of the arrays by looping through it.

User Elhoej
by
4.3k points