MCQ on Algorithm and data structure

Data Structure Questions and Answers-Array and Array Operations

Question 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
Question 1 Explanation: 
Array contains elements only of the same type.
Question 2
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);
Question 2 Explanation: 
This is the syntax to initialize an array in C.
Question 3
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);
Question 3 Explanation: 
Note that option b is declaration whereas option c is to instantiate an array.

Question 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
Question 4 Explanation: 
All the options are syntactically correct.

Question 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
Question 5 Explanation: 
Array indexing starts from 0.
Question 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
4
B
5
C
ArrayIndexOutOfBoundsException
D
InavlidInputException
Question 6 Explanation: 
Trying to access an element beyond the limits of an array gives ArrayIndexOutOfBoundsException.

Question 7
When does the ArrayIndexOutOfBoundsException occur?
A
Compile-time
B
Run-time
C
Not an error
D
None of the mentioned
Question 7 Explanation: 
ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free.

Question 8
Which of the following concepts make extensive use of arrays?
A
Binary trees
B
Scheduling of processes
C
Caching
D
Spatial locality
Question 8 Explanation: 
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.

Question 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
Question 9 Explanation: 
Arrays are simple to implement when it comes to matrices of fixed size and type, or to implement other data structures.

Question 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
Question 10 Explanation: 
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.

Question 11
Assuming int is of 4bytes, what is the size of int arr[15];?
A
15
B
19
C
11
D
60
Question 11 Explanation: 
Since there are 15 int elements and each int is of 4bytes, we get 15*4 = 60bytes.

Question 12
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
Question 12 Explanation: 
Array contains elements only of the same type.

Question 13
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);
Question 13 Explanation: 
This is the syntax to initialize an array in C.

There are 13 questions to complete.