Array and Array Operations | Data Structure MCQs

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.) Container of objects of mixed types
   D.) All of the mentioned

Answer: Option 'B'

Container of objects of similar types
Array contains elements only of the same type.

2.

How do you instantiate an array in Java?

   A.) int arr() = new int(3);
   B.) int arr[] = new int[3];
   C.) int arr[] = new int(3);
   D.) int arr[];

Answer: Option 'B'

Note that option b is declaration whereas option c is to instantiate an array.

3.

What is the output of the following piece of 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: Option 'A'

Array indexing starts from 0.

4.

When does the ArrayIndexOutOfBoundsException occur?

   A.) Compile-time
   B.) Run-time
   C.) Not an error
   D.) None of the mentioned

Answer: Option 'B'

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

5.

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: Option 'C'

This is the syntax to initialize an array in C.

Array and Array Operations | Data Structure MCQs Download Pdf