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] + " ");
    }
}

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

Choose the Best Option

Click any option to instantly check if you're correct.

  • A a   b   c
  • B a   p   x
  • C p   q   r
  • D b   q   y
Correct Answer: Option D

Explanation

[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] + " ");
    }
}
(a) a   b   c
(b) a   p   x
(c) p   q   r
(d) b   q   y
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

Share This Question

Challenge a friend or share with your study group.