# Array and Array Operations

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Array and Array Operations”.

1. Which of these best describes an array?
A) A data structure that shows a hierarchical behavior
B) Container of objects of similar types
C) Arrays are immutable once initialised
D) Array is not a data structure

Explanation: Only elements of the same form can be found in an array.

2. How do you instantiate an array in Java?
A) int arr[] = new int(3);
B) int arr[];
C) int arr[] = new int;
D) int arr() = new int(3);

Explanation: It’s worth noting that int arr[]; declares an array, while int arr[] = new int; instantiates one.

3. How do you initialize an array in C?
A) int arr = (1,2,3);
B) int arr(3) = {1,2,3};
C) int arr = {1,2,3};
D) int arr(3) = (1,2,3);

Explanation: In C, this is how you create an array.

4. What is the output of the following Java code?

```public class array
{
public static void main(String args[])
{
int []arr = {1,2,3,4,5};
System.out.println(arr);
System.out.println(arr);
}
}```

A) 3 and 5
B) 5 and 3
C) 2 and 4
D) 4 and 2

Explanation: The indexing of an array begins at zero.

5. Which of the following is the correct way to declare a multidimensional array in Java?
A) int[] arr;
B) int arr[[]];
C) int[][]arr;
D) int[[]] arr;

Explanation: In Java, you can declare a multidimensional array using the int[][] arr; or int arr[][]; syntax.

6. What is the output of the following Java code?

```public class array
{
public static void main(String args[])
{
int []arr = {1,2,3,4,5};
System.out.println(arr);
}
}```

A) 4
B) 5
C) ArrayIndexOutOfBoundsException
D) InavlidInputException

Explanation: ArrayIndexOutOfBoundsException is thrown when an element is accessed outside of an array’s bounds.

7. Which of the following concepts make extensive use of arrays?
A) Binary trees
B) Scheduling of processes
C) Caching
D) Spatial locality

Explanation: When a memory location is referred to, it is likely that the locations around are also referred to. Since arrays are stored as contiguous blocks of memory, spatial locality makes it easier to access array elements.

8. When does the ArrayIndexOutOfBoundsException occur?
A) Compile-time
B) Run-time
C) Not an error
D) Not an exception at all

Explanation: The compilation is error-free and the ArrayIndexOutOfBoundsException is a run-time exception.

9. What are the disadvantages of arrays?
A) Data structure like queue or stack cannot be implemented
B) There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
C) Index value of an array can be negative
D) Elements are sequentially accessed

Explanation: Arrays have a set size. Unoccupied positions cannot be used again if we insert elements that are smaller than the allocated size. In memory, there would be wastage.

10. What are the advantages of arrays?
A) Objects of mixed data types can be stored
B) Elements in an array cannot be sorted
C) Index of first element of an array is 1
D) Easier to store elements of same data type

Explanation: Arrays are types of items of the same data form that are stored in a continuous memory location.

11. In general, the index of the first element in an array is __________
A) 0
B) -1
C) 2
D) 1

Explanation: Array indexing, in general, begins at zero. As a consequence, the first element in an array has an index of 0.

12. Elements in an array are accessed _____________
A) randomly
B) sequentially
C) exponentially
D) logarithmically