MCQs on Arrays | Java Collections

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

  1. 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
  2. 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
  3. 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
  4. What is the default value of an int array element in Java?
    • A) 0
    • B) null
    • C) undefined
    • D) 1
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. How many dimensions does a 2D array have in Java?
  • A) 1
  • B) 2
  • C) 3
  • D) Depends on initialization
  1. 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
  1. 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]
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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

  1. 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
  1. How do you sort an array in Java?
  • A) Arrays.sort()
  • B) Collections.sort()
  • C) Both A and B
  • D) None of the above
  1. 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
  1. 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
  1. 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()
  1. 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
  1. 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
  1. 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)
  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
  1. 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

QnoAnswer
1C) Both A and B
2C) Both A and B
3C) Arrays can be initialized with both dynamic and static values
4A) 0
5D) All of the above
6C) Both A and B
7A) int[] arr = {1, 2, 3, 4, 5};
8C) Both A and B
9C) Both A and B
10A) arr.length
11B) 2
12C) Both A and B
13A) arr[1][2]
14C) Both A and B
15C) Both A and B
16B) arr[0].length
17B) System.out.println(Arrays.deepToString(arr));
18D) All of the above
19B) It is a multi-dimensional array with rows of varying lengths
20D) It will throw an ArrayIndexOutOfBoundsException
21C) Both A and B
22A) Arrays.sort()
23A) Arrays.fill()
24C) It checks if two arrays have the same elements
25C) Arrays.binarySearch()
26C) The array length cannot be modified
27D) There is no direct method for removing an element
28B) O(log n)
29A) Arrays.asList()
30A) Arrays.equals(arr1, arr2)

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top