Docsity

Data Structure Questions and Answers – Array and Array Operations, Summaries of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures MCQs – Arrays 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: 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);
Answer: c
Explanation: Correct syntax uses curly braces {}.
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);
Answer: c
Explanation: Declaration vs instantiation difference.
4. Correct way to declare a multidimensional array in Java?
a) int[] arr;
b) int arr[[]];
c) int[][] arr;
d) int[[]] arr;
Answer: c
5. Output of arr[2] and arr[4] for {1,2,3,4,5}?
a) 3 and 5
b) 5 and 3
c) 2 and 4
d) 4 and 2
Answer: a
Explanation: Index starts from 0.
6. Output of accessing arr[5]?
a) 4
b) 5
c) ArrayIndexOutOfBoundsException
d) InvalidInputException
Answer: c
7. When does ArrayIndexOutOfBoundsException occur?
a) Compile-time
b) Run-time
c) Not an error
d) Not an exception
pf2

Partial preview of the text

Download Data Structure Questions and Answers – Array and Array Operations and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Data Structures MCQs – Arrays 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: 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); Answer: c Explanation: Correct syntax uses curly braces {}.
  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); Answer: c Explanation: Declaration vs instantiation difference.
  4. Correct way to declare a multidimensional array in Java? a) int[] arr; b) int arr[[]]; c) int[][] arr; d) int[[]] arr; Answer: c
  5. Output of arr[2] and arr[4] for {1,2,3,4,5}? a) 3 and 5 b) 5 and 3 c) 2 and 4 d) 4 and 2 Answer: a Explanation: Index starts from 0.
  6. Output of accessing arr[5]? a) 4 b) 5 c) ArrayIndexOutOfBoundsException d) InvalidInputException Answer: c
  7. When does ArrayIndexOutOfBoundsException occur? a) Compile-time b) Run-time c) Not an error d) Not an exception

Answer: b

  1. Which concept uses arrays extensively? a) Binary trees b) Scheduling of processes c) Caching d) Spatial locality Answer: d
  2. Advantage of arrays? a) Mixed data types b) Cannot be sorted c) Index starts at 1 d) Easy storage of same type elements Answer: d
  3. Disadvantage of arrays? a) Cannot implement stack/queue b) Memory wastage c) Negative index allowed d) Sequential access only Answer: b
  4. Size of int arr[15] if int = 4 bytes? a) 15 b) 19 c) 11 d) 60 Answer: d
  5. Index of first element in array? a) 0 b) - c) 2 d) 1 Answer: a
  6. Elements in array are accessed? a) randomly b) sequentially c) exponentially d) logarithmically Answer: a