MCQs on Arrays & Java Collections: Declaring, Initializing, Operations, and Multi-Dimensional Arrays This set of multiple-choice questions (MCQs) covers essential topics in Java arrays and collections, including declaring and initializing arrays, multi-dimensional arrays, array operations, and using arrays within Java collections. These topics are fundamental for mastering Java programming and enhancing your problem-solving skills.
Topic 1: Declaring and Initializing Arrays
Which of the following is the correct way to declare an array in Java?
A) int[] arr;
B) int arr[];
C) Both A and B
D) None of the above
How do you initialize an array with 5 elements in Java?
A) int[] arr = new int[5];
B) int arr[] = {1, 2, 3, 4, 5};
C) Both A and B
D) None of the above
Which of the following is true about array initialization in Java?
A) Arrays cannot be initialized with a fixed size
B) Array initialization must always be done dynamically
C) Arrays can be initialized with both dynamic and static values
D) Arrays can only be initialized with default values
What is the default value of an int array element in Java?
A) 0
B) null
C) undefined
D) 1
Which statement correctly initializes a String array in Java with 3 elements?
A) String[] arr = {“apple”, “banana”, “cherry”};
B) String[] arr = new String[3];
C) String arr[] = {“apple”, “banana”, “cherry”};
D) All of the above
How do you declare a 2D array of integers in Java?
A) int[][] arr = new int[2][3];
B) int arr[][] = new int[2][3];
C) Both A and B
D) None of the above
Which of the following is the correct way to initialize an array of 5 integers with values 1 to 5 in Java?
A) int[] arr = {1, 2, 3, 4, 5};
B) int arr[] = new int[5]{1, 2, 3, 4, 5};
C) int arr[] = new int[5]; arr[0] = 1; arr[1] = 2;
D) None of the above
How do you initialize an array of double values in Java?
A) double[] arr = new double[3];
B) double arr[] = {1.1, 2.2, 3.3};
C) Both A and B
D) None of the above
Which of the following is the correct declaration and initialization of a boolean array with 2 elements?
A) boolean[] arr = {true, false};
B) boolean arr[] = {false, true};
C) Both A and B
D) None of the above
In Java, what is the length of an array named arr?
A) arr.length
B) arr.size()
C) arr.length()
D) arr.size
Topic 2: Multi-Dimensional Arrays
How many dimensions does a 2D array have in Java?
A) 1
B) 2
C) 3
D) Depends on initialization
Which of the following correctly initializes a 3×3 integer 2D array in Java?
A) int[][] arr = new int[3][3];
B) int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
C) Both A and B
D) None of the above
In a 2D array, which statement correctly accesses the element in the second row and third column?
A) arr[1][2]
B) arr[2][3]
C) arr[3][2]
D) arr[1][3]
How do you declare a 3-dimensional array in Java?
A) int[][][] arr = new int[3][3][3];
B) int arr[][][] = new int[3][3][3];
C) Both A and B
D) None of the above
How can you initialize a jagged array in Java?
A) int[][] arr = new int[2][];
B) int arr[][] = {{1, 2, 3}, {4, 5}};
C) Both A and B
D) None of the above
Which statement is used to get the length of a specific dimension of a multi-dimensional array?
A) arr.length
B) arr[0].length
C) arr[1].length()
D) arr[1].length
What is the correct way to print a 2D array in Java?
A) System.out.println(Arrays.toString(arr));
B) System.out.println(Arrays.deepToString(arr));
C) System.out.println(arr);
D) None of the above
In a multi-dimensional array, which statement correctly accesses the element in the third dimension?
A) arr[0][1][2]
B) arr[1][0][2]
C) arr[2][1][0]
D) All of the above
Which of the following is true about a jagged array in Java?
A) It is a multi-dimensional array with each row having the same number of columns
B) It is a multi-dimensional array with rows of varying lengths
C) It is not a type of array
D) It can only be used with primitive types
What happens if you try to access an out-of-bounds index in a multi-dimensional array?
A) It will return null
B) It will cause a runtime error
C) It will return 0
D) It will throw an ArrayIndexOutOfBoundsException
Topic 3: Array Operations
Which of the following methods is used to copy one array to another in Java?
A) Arrays.copyOf()
B) System.arraycopy()
C) Both A and B
D) None of the above
How do you sort an array in Java?
A) Arrays.sort()
B) Collections.sort()
C) Both A and B
D) None of the above
Which method is used to fill an array with a specific value in Java?
A) Arrays.fill()
B) Arrays.set()
C) Array.fill()
D) None of the above
What does the Arrays.equals() method do?
A) It compares the size of two arrays
B) It checks if two arrays have the same reference
C) It checks if two arrays have the same elements
D) None of the above
Which method is used to search for an element in a sorted array?
A) Arrays.find()
B) Arrays.search()
C) Arrays.binarySearch()
D) Arrays.lookup()
What happens when you try to modify the length of an array after it is initialized?
A) The array size is automatically increased
B) A SizeOverflowException is thrown
C) The array length cannot be modified
D) The array is re-initialized with a new size
Which statement is used to remove an element from an array in Java?
A) Arrays.remove()
B) Arrays.delete()
C) Arrays.deleteElement()
D) There is no direct method for removing an element
What is the time complexity of searching an element in a sorted array using binarySearch() in Java?
A) O(n)
B) O(log n)
C) O(n^2)
D) O(1)
Which method would you use to convert an array to a List in Java?
A) Arrays.asList()
B) Array.toList()
C) List.add()
D) None of the above
How do you determine if two arrays are identical in Java?
A) Arrays.equals(arr1, arr2)
B) arr1 == arr2
C) Arrays.same(arr1, arr2)
D) None of the above
Answers
Qno
Answer
1
C) Both A and B
2
C) Both A and B
3
C) Arrays can be initialized with both dynamic and static values
4
A) 0
5
D) All of the above
6
C) Both A and B
7
A) int[] arr = {1, 2, 3, 4, 5};
8
C) Both A and B
9
C) Both A and B
10
A) arr.length
11
B) 2
12
C) Both A and B
13
A) arr[1][2]
14
C) Both A and B
15
C) Both A and B
16
B) arr[0].length
17
B) System.out.println(Arrays.deepToString(arr));
18
D) All of the above
19
B) It is a multi-dimensional array with rows of varying lengths
20
D) It will throw an ArrayIndexOutOfBoundsException
21
C) Both A and B
22
A) Arrays.sort()
23
A) Arrays.fill()
24
C) It checks if two arrays have the same elements
25
C) Arrays.binarySearch()
26
C) The array length cannot be modified
27
D) There is no direct method for removing an element