MCQs On Array Data Structure With Answers
MCQs On Array Data Structure With Answers
Answers
Question 1
C++Java
#include <iostream>
using namespace std;
int main()
{
int arr[2] = { 1, 2 };
cout << 0 [arr] << ", " << 1 [arr] << endl;
return 0;
}
1, 2
Syntax error
Run time error
None
Discuss it
Question 1 ‒ Explanation
Question 2
The minimum number of comparisons required to determine if an integer
appears more than n/2 times in a sorted array of n integers is
Θ(n)
Θ(logn)
Θ(n*logn)
Θ(1)
Discuss it
Question 2 ‒ Explanation
Question 3
Unsorted array
Min-heap
Sorted array
Sorted doubly linked list
Discuss it
Question 3 ‒ Explanation
Question 4
Consider an array consisting of –ve and +ve numbers. What would be the
worst case time complexity of an algorithm to segregate the numbers
having same sign altogether i.e all +ve on one side and then all -ve on the
other ?
O(N)
O(N Log N)
O(N * N)
O(N Log Log N)
Discuss it
Question 4 ‒ Explanation
Here we can use the partition algorithm of quick sort for segregation and
answer will be O(N).
Question 5
Let A[1...n] be an array of n distinct numbers. If i < j and A[i] > A[j], then
the pair (i, j) is called an inversion of A. What is the expected number of
inversions in any permutation on n elements ?
n(n-1)/2
n(n-1)/4
n(n+1)/4
2n[logn]
Discuss it
Question 5 ‒ Explanation
,a
Question 6
560
460
570
575
Discuss it
Question 6 ‒ Explanation
Question 7
An array A consists of n integers in locations A[0], A[1] ....A[n-1]. It is
required to shift the elements of the array cyclically to the left by k places,
where 1 <= k <= (n-1). An incomplete algorithm for doing this in linear
time, without using another array is given below. Complete the algorithm
by filling in the blanks. Assume alt the variables are suitably declared.
C++
min = n; i = 0;
while (___________) {
temp = A[i]; j = i;
while (________) {
A[j] = ________
j= (j + k) mod n ;
min = j;
i = __________
i > min; j!= (n+i)mod n; A[j + k]; temp; i + 1 ;
i < min; j!= (n+i)mod n; A[j + k]; temp; i + 1;
i > min; j!= (n+i+k)mod n; A[(j + k)]; temp; i + 1;
i < min; j!= (n+i-k)mod n; A[(j + k)mod n]; temp; i + 1;
Discuss it
Question 7 ‒ Explanation
In the five blanks given in the question, the last two blanks must be temp
and i+1 because all the given options for the fourth and fifth blanks have
temp and i+1. Now, for the first blank, it must be imin then the control
goes out of the while loop in the initial case when i=0 and min=n So, the
first blank is i < min which implies either option (B) or option (D) is
correct. Assume option (B) is correct then in the bracket of while we
have j!=(n+i)modn That means whenever j becomes equal to (n+i)modn
then control goes out of the while loop. Now (n+i)modn=i and j is always
equal to i because in line 3 of the code we are assigning the value of i to
j. So, if option (B) is true control never enters the second while loop but it
has to enter the second while loop to shift the nos. K places left. Hence,
option (D) is correct.
Question 8
int geeks[20];
int geeks;
geeks{20};
array geeks[20];
Discuss it
Question 8 ‒ Explanation
Option A is correct. Int is the data type used, geeks is the name of the
array and 20 is the size of the array.
Question 9
&A[0][0][0] + w(y * z * p + z*q + r)
&A[0][0][0] + w(x * y * p + z * q+ r)
&A[0][0][0] + w(x * y * q + z * p + r)
Discuss it
Question 9 ‒ Explanation
Question 10
C = 100
for i = 1 to n do
for j = 1 to n do
{
Temp = A[i][j] + C
A[i][j] = A[j][i]
A[j][i] = Temp - C
}
for i = 1 to n do
for j = 1 to n do
Output(A[i][j]);
The matrix A itself
Transpose of matrix A
Adding 100 to the upper diagonal elements and subtracting 100 from
diagonal elements of A
None of the above
Discuss it
Question 10 ‒ Explanation
If we take look at the inner statements of first loops, we can notice that
the statements swap A[i][j] and A[j][i] for all i and j. Since the loop runs
for all elements, every element A[l][m] would be swapped twice, once for
i = l and j = m and then for i = m and j = l. Swapping twice means the
matrix doesn’t change.
Question 11
An array of 50 numbers
An array of 100 numbers
An array of 500 numbers
A dynamically allocated array of 550 numbers
Discuss it
Question 11 ‒ Explanation
Question 12
What will the output of the below code, be if the base address of the array
is 1200?
C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
cout << arr << ", " << &arr << ", " << &arr[0] << endl;
return 0;
}
1200, 1202, 1204
1200 1200 1200
1200, 1204, 1208
1200, 1204, 1208
Discuss it
Question 12 ‒ Explanation
Given that, the base address of the array is 1200.
Question 13
What is the correct way to call the function (fun) in the below program?
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Driver program
int main()
{
char arr[]
= { 'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z' };
// How to call the above function here to print the char
// elements?
return 0;
}
fun(&arr);
fun(*arr);
fun(arr)
None
Discuss it
Question 13 ‒ Explanation
fun(arr) is the correct syntax to call the function having array (arr) as a
Parameter.
Question 14
C++
void fun(int A[][N])
{
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
swap(A[i][j], A[j][i]);
}
Matrix A
Diagonal Of matrix A
Transpose of matrix A
None
Discuss it
Question 14 ‒ Explanation
In the above program, we are just swapping(arr[i][j]) to arr[j][i]). That
means row will become column and column will become row.
That means the Transpose of the graph.
Question 15
C++
void fun(int arr[], int n)
{
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = i; k <= j; k++)
cout << arr[k] << " ";
Prints the subsequence of the article.
Prints the elements of the array
Prints the subarray of the element.
None
Discuss it
Question 15 ‒ Explanation
Question 16
elements can be accessed from anywhere.
The size of the array is fixed.
Indexing is started from Zero.
Memory waste if an array's elements are smaller than the size
allotted to them
Discuss it
Question 16 ‒ Explanation
Here we have declared the size of the array as 5, but we have initialized
only 3 values to it. So it leads to memory wastage.
Hence Option (D) is correct.
Question 17
Consider the below program, and what is doing this program basically?
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char a[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
int n = sizeof(a) / sizeof(a[0]);
print(a, n, 3);
return 0;
}
It is printing the normal array
It is printing circular array rotated by 3
Syntax error
None
Discuss it
Question 17 ‒ Explanation
Question 18
C++
/*Function to left rotate arr[] of size n by d*/
void Rotate(int arr[], int d, int n)
{
int p = 1;
while (_______) {
int last = arr[0];
for (int i = 0; ______ i++) {
arr[i] = arr[i + 1];
}
__________
p++;
}
}
p <= d , i < n - 1 , arr[n - 1] = last;
p < d, i < n, arr[n] = last;
p >=d, i >n , arr[n] = last
None
Discuss it
Question 18 ‒ Explanation
Question 19
Row-wise traversal of the matrix.
Column-wise traversal of the matrix.
spiral traversal of the matrix.
Discuss it
Question 19 ‒ Explanation
Question 20
C++
void fun(int arr[], int start, int end)
{
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
swapping the elements pairwise
sorting the elements
Reverse an array
None
Discuss it
Question 20 ‒ Explanation