Data Structure Questions and Answers - Wisdom Lighter

Tuesday, 22 May 2018

Data Structure Questions and Answers


                                                       
C++

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) Container of objects of mixed types
d) All of the mentioned
View Answer
Answer: b
Explanation: Array contains elements only of the same type.
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);
View Answer
Answer: c
Explanation: This is the syntax to initialize an array in C.
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);
View Answer
Answer: c
Explanation: 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: 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) 4
b) 5
c) ArrayIndexOutOfBoundsException
d) InavlidInputException
View Answer
Answer: c
Explanation: Trying to access an element beyond the limits of an array gives ArrayIndexOutOfBoundsException.
7. When does the ArrayIndexOutOfBoundsException occur?
a) Compile-time
b) Run-time
c) Not an error
d) None of the mentioned
View Answer
Answer: b
Explanation: ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free.
8. Which of the following concepts make extensive use of arrays?
a) Binary trees
b) Scheduling of processes
c) Caching
d) Spatial locality
View Answer
Answer: d
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.
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: d
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.
11. Assuming int is of 4bytes, what is the size of int arr[15];?
a) 15
b) 19
c) 11
d) 60
View Answer
Answer: d
Explanation: Since there are 15 int elements and each int is of 4bytes, we get 15*4 = 60bytes.

No comments:
Write comments