Arrays MCQs

By: Prof. Dr. Fazal Rehman Shamil | Last updated: September 20, 2024

What is an array?
A) A collection of elements of the same type stored in contiguous memory locations
B) A collection of elements of different types
C) A data structure that stores elements in a non-linear fashion
D) A dynamic data structure
Answer: A) A collection of elements of the same type stored in contiguous memory locations

What is the index of the first element in a typical zero-indexed array?
A) 0
B) 1
C) -1
D) Depends on the programming language
Answer: A) 0

What is the time complexity for accessing an element in an array?
A) O(1)
B) O(n)
C) O(log n)
D) O(n log n)
Answer: A) O(1)

Which of the following is NOT a valid array declaration in C++?
A) int arr[10];
B) int arr(){};
C) int* arr = new int[10];
D) int arr[5] = {1, 2, 3, 4, 5};
Answer: B) int arr(){};

How do you find the length of an array in C++?
A) Using sizeof(arr)
B) Using length(arr)
C) Using count(arr)
D) Length is not defined for arrays
Answer: A) Using sizeof(arr)

What will happen if you try to access an index that is out of bounds in an array?
A) It will return zero
B) It will cause a runtime error
C) It will return a garbage value
D) It will create a new element
Answer: B) It will cause a runtime error

What is the time complexity of inserting an element at the end of a dynamic array?
A) O(1)
B) O(n)
C) O(log n)
D) O(n log n)
Answer: A) O(1)

Which of the following is an example of a multi-dimensional array?
A) int arr[5];
B) int arr[3][4];
C) int* arr;
D) int arr[] = {1, 2, 3};
Answer: B) int arr[3][4];

What does it mean if an array is sparse?
A) Most elements are the same
B) Most elements are zero or empty
C) The array is sorted
D) The array contains duplicate values
Answer: B) Most elements are zero or empty

What is the primary advantage of using an array over linked lists?
A) Dynamic sizing
B) Random access
C) Better memory management
D) Easier to implement
Answer: B) Random access

Which of the following operations on arrays is NOT possible?
A) Insertion
B) Deletion
C) Dynamic resizing
D) Accessing elements
Answer: C) Dynamic resizing

What will be the output of the following code? int arr[5] = {1, 2, 3, 4, 5}; cout << arr[2];
A) 1
B) 2
C) 3
D) 4
Answer: C) 3

How do you declare a character array in C++?
A) char arr[10];
B) char arr(10);
C) char arr{10};
D) char arr;
Answer: A) char arr[10];

What is the process of placing an element in the correct position of a sorted array called?
A) Insertion
B) Sorting
C) Searching
D) Merging
Answer: A) Insertion

In which scenario would you use a 2D array?
A) To represent a list of numbers
B) To represent a grid or matrix
C) To store elements of different types
D) To manage memory more efficiently
Answer: B) To represent a grid or matrix

What is the output of the following code? int arr[] = {5, 10, 15}; cout << arr[1] + arr[2];
A) 5
B) 10
C) 15
D) 25
Answer: D) 25

What is the purpose of the sizeof operator in relation to arrays?
A) To get the length of the array
B) To get the size of each element
C) To get the total size of the array in bytes
D) All of the above
Answer: C) To get the total size of the array in bytes

Which of the following is true about static arrays?
A) They can grow in size during runtime
B) They require less memory than dynamic arrays
C) Their size must be defined at compile time
D) They are more flexible than dynamic arrays
Answer: C) Their size must be defined at compile time

What happens if you pass an array to a function in C++?
A) The entire array is copied
B) A pointer to the first element is passed
C) The function cannot access the array
D) It causes a compilation error
Answer: B) A pointer to the first element is passed

What is the main disadvantage of arrays?
A) Random access
B) Fixed size
C) Easy implementation
D) Fast access time
Answer: B) Fixed size

Which of the following can be used to traverse an array?
A) For loop
B) While loop
C) Do-while loop
D) All of the above
Answer: D) All of the above

What is the time complexity of searching for an element in a sorted array using binary search?
A) O(n)
B) O(log n)
C) O(n log n)
D) O(1)
Answer: B) O(log n)

What will the following code print? int arr[5] = {1, 2, 3}; cout << arr[5];
A) 1
B) 0
C) Undefined behavior
D) Compilation error
Answer: C) Undefined behavior

How do you initialize all elements of an array to zero in C++?
A) int arr[5] = {0};
B) int arr[5] = {1, 1, 1, 1, 1};
C) int arr[5] = {};
D) Both A and C
Answer: D) Both A and C

What is the best data structure to represent a collection of elements that require fast random access?
A) Linked List
B) Stack
C) Array
D) Queue
Answer: C) Array

What type of sorting algorithm can be used to sort an array?
A) Bubble Sort
B) Merge Sort
C) Quick Sort
D) All of the above
Answer: D) All of the above

How do you find the maximum element in an array?
A) By sorting the array
B) By iterating through the array
C) By using a binary search
D) None of the above
Answer: B) By iterating through the array

What is the effect of using delete on an array allocated with new?
A) It will cause a memory leak
B) It will free the memory allocated for the array
C) It will only free the first element
D) It causes undefined behavior
Answer: B) It will free the memory allocated for the array

How are elements stored in a one-dimensional array?
A) In a linked fashion
B) Randomly
C) Contiguously in memory
D) Based on their type
Answer: C) Contiguously in memory

Which operation is used to combine two arrays into one?
A) Merging
B) Sorting
C) Splitting
D) Cloning
Answer: A) Merging

What is a common application of arrays in programming?
A) To implement dynamic data structures
B) To represent data in tables
C) To handle complex data types
D) To improve memory efficiency
Answer: B) To represent data in tables

What is the output of the following code? int arr[3] = {1, 2, 3}; cout << arr[3];
A) 1
B) 3
C) Undefined behavior
D) 0
Answer: C) Undefined behavior

What is the difference between a static array and a dynamic array?
A) Static arrays can change size
B) Dynamic arrays are allocated at compile time
C) Static arrays are allocated at compile time
D) There is no difference
Answer: C) Static arrays are allocated at compile time

Which of the following statements is true about arrays?
A) They can only store primitive data types
B) They can store elements of different data types
C) They allow for random access
D) Both A and C
Answer: C) They allow for random access

What is a common method to sort an array?
A) Insertion Sort
B) Selection Sort
C) Bubble Sort
D) All of the above
Answer: D) All of the above

Which of the following methods can be used to reverse an array?
A) Using a loop
B) Using recursion
C) Using a built-in function
D) All of the above
Answer: D) All of the above

What is the purpose of the memset function in C++?
A) To set a specific value in an array
B) To initialize an array to zero
C) To allocate memory
D) To copy memory
Answer: B) To initialize an array to zero

How do you access the last element of an array named arr with size n?
A) arr[n]
B) arr[n-1]
C) arr[n+1]
D) arr[n-2]
Answer: B) arr[n-1]

What is the best way to copy an array in C++?
A) Using a loop
B) Using memcpy
C) Using the assignment operator
D) Both A and B
Answer: D) Both A and B

 

Data Structures MCQs

Basic Concepts

  1. Introduction to Data Structures
  2. Complexity Analysis MCQs

Linear Data Structures MCQs

  1. Arrays MCQs
  2. Linked Lists MCQs
  3. Stacks MCQs
  4. Queues MCQs

Non-Linear Data Structures MCQs

  1. Trees MCQs
  2. Heaps MCQs
  3. Graphs MCQs

Hashing MCQs MCQs

  1. Hash Tables

Sorting and Searching Algorithms MCQs 

  1. Sorting Algorithms MCQs
  2. Searching Algorithms MCQs

Miscellaneous

  1. Memory Management in data structures MCQs
  2. String Manipulation Algorithms MCQs
  1. Data Structures MCQs 1
  2. Data Structures MCQs 2
  3. Data Structures MCQs 3
  4. Data Structures MCQs 4
  5. Data Structures MCQs 5
  6. Stacks Solved MCQs
  7. Queues MCQs
  8. pointer mcqs
  9. Array MCQs