Array and Array Operations | Data Structure MCQs

  • 1. 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.

  • 2. 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.

  • 3. 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.

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

Answer: Option 'D'

All the options are syntactically correct.

  • 5. 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.

  • 6. 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[5]);
    	}
    }
   A.) 5
   B.) 4
   C.) ArrayIndexOutOfBoundsException
   D.) InavlidInputException

Answer: Option 'C'

Trying to access an element beyond the limits of an array gives ArrayIndexOutOfBoundsException.

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

Answer: Option 'C'

Whenever a particular memory location is referred, it is likely that the locations nearby are also referred, arrays are stored as contigous blocks in memory, so if you want to access array elements, spatial locality makes it to access quickly.

  • 8. 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.

  • 9. What are the advantages of arrays?
   A.) Easier to store elements of same data type
   B.) Used to implement other data structures like stack and queue
   C.) Convenient way to represent matrices as a 2D array
   D.) All of the mentioned

Answer: Option 'D'

Arrays are simple to implement when it comes to matrices of fixed size and type, or to implement other data structures.

  • 10. What are the disadvantages of arrays?
   A.) We must know before hand how many elements will be there in the array
   B.) There are chances of wastage of memory space if elements inserted in an array are lesser than than the allocated size
   C.) Insertion and deletion becomes tedious
   D.) All of the mentioned

Answer: Option 'D'

All of the mentioned
Arrays are of fixed size, hence during the compile time we should know its size and type, since arrays are stored in contigous locations, insertion and deletion becomes time consuming.