Array and Array Operations

Uncategorized

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

Answer: B
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[3];
D) int arr() = new int(3);

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

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

Answer: C
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[2]);
		System.out.println(arr[4]);
	}
}

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

Answer: A
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;

Answer: C
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[5]);
	}
}

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

Answer: C
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

Answer: D
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

Answer: B
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

Answer: B
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

Answer: D
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

Answer: A
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

Answer: A
Explanation: An array’s elements are accessed at random. Elements in linked lists are accessed in a sequential order.

13. Assuming int is of 4bytes, what is the size of int arr[15];?
A) 15
B) 19
C) 11
D) 60

Answer: D
Explanation: We get 15*4 = 60bytes since there are 15 int elements and each int is 4bytes.

A data structure that comprises a set of elements is known as an array. These components are usually all of the same data form, such as an integer or a string. A search engine, for example, might use an array to store Web pages found during a user’s search. An array is a linear data structure that consists of a set of data items of identical data types that are stored in constant time. Below is a list of arrays and their representations. Array Index: Each element in an array has a unique index that is used to identify it. The index of the array starts at 0.

Leave a Reply

Your email address will not be published. Required fields are marked *