Sunday, July 28, 2024

2 Dimensional array MCQ

 What does a two-dimensional array represent in Java?

a) A single row of elements

b) Multiple rows and columns of elements

c) Elements stored in a linear fashion

d) Elements of different data types


What is the declaration syntax for a two-dimensional integer array named matrix in Java?

a) int matrix()[];

b) matrix int[][];

c) int[][] matrix;

d) matrix[][] int;


How are two-dimensional arrays initialized in Java?

a) matrix[][] = new int[][];

b) int matrix()[][] = new int();

c) int[][] matrix = new int[3][3];

d) int matrix[][]();


What is the index range for accessing elements in a two-dimensional array of size m x n in Java?

a) 0 to n-1

b) 1 to m x n

c) 0 to m-1 for rows and 0 to n-1 for columns

d) -m to m-1 for rows and -n to n-1 for columns


How are individual elements accessed in a two-dimensional array in Java?

a) matrix.get(2, 3);

b) matrix[2, 3];

c) matrix[2][3];

d) matrix.getElement(2, 3);


In a two-dimensional array arr[][], how do you access the element in the third row and fourth column?

a) arr[3][4];

b) arr[2][3];

c) arr[4][3];

d) arr[3][5];


What is the number of dimensions in a two-dimensional array?

a) One

b) Two

c) Three

d) It varies


How do you assign values to a two-dimensional array in Java after its declaration?

a) matrix = {{1, 2}, {3, 4}};

b) matrix = new int[][] {1, 2, 3, 4};

c) matrix = new int[][] {{1, 2}, {3, 4}};

d) All of the above


What happens if you try to access an index beyond the bounds of a two-dimensional array in Java?

a) It results in a compilation error.

b) It throws an ArrayIndexOutOfBoundsException.

c) It returns the default value of the array's data type.

d) It returns null.


What is the primary difference between a one-dimensional array and a two-dimensional array in Java?

a) One-dimensional arrays store elements in a linear sequence, while two-dimensional arrays store elements in rows and columns.

b) One-dimensional arrays can store multiple data types, while two-dimensional arrays can only store integers.

c) One-dimensional arrays have fixed sizes, while two-dimensional arrays can dynamically resize.

d) There is no difference between them.


Answers:


b) Multiple rows and columns of elements

c) int[][] matrix;

c) int[][] matrix = new int[3][3];

c) 0 to m-1 for rows and 0 to n-1 for columns

c) matrix[2][3];

a) arr[3][4];

b) Two

c) matrix = new int[][] {{1, 2}, {3, 4}};

b) It throws an ArrayIndexOutOfBoundsException.

a) One-dimensional arrays store elements in a linear sequence, while two-dimensional arrays store elements in rows and columns.

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...