MCQ
Single Best Answer
Moderate
Q
[Arrays]
What is the outcome of the following code snippet?
public class Test
{
public static void main()
{
char[][] matrix = {{'a','b','c'},{'p','q','r'},{'x','y','z'}};
for(int i=0; i<3; i++)
System.out.print(matrix[i][1] + " ");
}
}
What is the outcome of the following code snippet?
{
public static void main()
{
char[][] matrix = {{'a','b','c'},{'p','q','r'},{'x','y','z'}};
for(int i=0; i<3; i++)
System.out.print(matrix[i][1] + " ");
}
}
ID: #24941
Competency focused Practice Questions ISC Class XII Computer Science
2 views
Question Info
#24941Q ID
ModerateDifficulty
Competency focused Practice Questions ISC Class XII Computer ScienceTopic
Your Answer
Choose the Best Option
Click any option to instantly check if you're correct.
Correct Answer: Option D
Explanation
[Arrays]
What is the outcome of the following code snippet?
What is the outcome of the following code snippet?
public class Test
{
public static void main()
{
char[][] matrix = {{'a','b','c'},{'p','q','r'},{'x','y','z'}};
for(int i=0; i<3; i++)
System.out.print(matrix[i][1] + " ");
}
}
{
public static void main()
{
char[][] matrix = {{'a','b','c'},{'p','q','r'},{'x','y','z'}};
for(int i=0; i<3; i++)
System.out.print(matrix[i][1] + " ");
}
}
Correct Answer: (d) b q y
Explanation:
The program uses a two-dimensional character array named matrix.
char[][] matrix = {
{'a','b','c'},
{'p','q','r'},
{'x','y','z'}
};
Array Structure:
| Row Index | Column 0 | Column 1 | Column 2 |
|---|---|---|---|
| 0 | a | b | c |
| 1 | p | q | r |
| 2 | x | y | z |
Understanding the Loop:
for(int i=0; i<3; i++)
System.out.print(matrix[i][1] + " ");
The loop runs from:
- i = 0
- i = 1
- i = 2
In every iteration, the program prints:
matrix[i][1]
Here:
- i → row number
- 1 → second column (because indexing starts from 0)
Step-by-Step Execution:
| i Value | matrix[i][1] | Output |
|---|---|---|
| 0 | matrix[0][1] | b |
| 1 | matrix[1][1] | q |
| 2 | matrix[2][1] | y |
Final Output:
b q y
Conclusion:
Therefore, the correct answer is: (d) b q y
Continue Practice
Share
Share This Question
Challenge a friend or share with your study group.
More from This Topic