Q: 1 Consider an array A[15,13], assume 2 words per memory cell and elements are stored in row-major order. The base address of array A is 150. What is the address of A[13,12]?
334
484
512
362
[ Option C ]
The array A[15][13] is stored in row-major order, which means elements are stored row by row. The formula to calculate the address of an element A[i][j] in row-major order is:
LOC(A[I][J])=BASE(A)+[I×N+J]×W
Where,
When the index starts from 0, the number of columns is specified in the array declaration. For example, in A[15][13], the total number of columns is 13.
So,
Base Address = 150
Number of Columns (N) = 13
Size of each element (W) = 2
Element to find = A[13][12] i.e., I = 13 and J = 12
LOC(A[13][12]) = 150+[13*13+12]*2
LOC(A[13][12]) = 150+[169+12]*2
LOC(A[13][12]) = 150+[181]*2
LOC(A[13][12]) = 150+362
LOC(A[13][12]) = 512
NOTE:
Q: 2 Which option correctly define the advantages of array?
Arrays allow efficient insertions and deletions in the middle.
Arrays provide low memory usage by storing references instead of actual data.
Arrays allow random access and cache friendliness due to contiguous memory allocation.
Arrays are slower than other data structures in accessing elements.
[ Option C ]
Arrays allow direct access to any element in constant time O(1) using its index, which is efficient because elements are stored at contiguous memory locations due to which they also benefit from locality of reference.
Q: 3 Which of the following is an advantage of using arrays?
Constant time insertion and deletion
Ability to store elements of different data types
Random access to elements using an index
More than one of the above
[ Option C ]
An array is a linear data structure in which elements are stored in contiguous memory locations. This layout allows for random access, meaning any element can be directly accessed using its index in constant time O(1).
Q: 4 Consider an array consisting of –ve and +ve numbers. What would be the worst case time complexity of an algorithm to segregate or separate the numbers having same sign altogether i.e all +ve on one side and then all -ve on the other?
O(n)
O(log n)
O(n2)
O(n log n)
[ Option A ]
To segregate positive and negative numbers in an array, we can scan the array only once and rearrange elements using a two-pointer or partition-based approach. In two pointers approach, one at the beginning (left) and one at the end (right) of the array.
The left pointer moves from the beginning towards the right, looking for a positive number. The right pointer moves from the end towards the left, looking for a negative number.
When the left pointer finds a positive number and the right pointer finds a negative number, they are swapped.
If left points to a negative number, or right points to a positive number, the respective pointers are adjusted accordingly (i.e., increment left or decrement right).
Even in the worst case, each element is examined only once, which is why the overall time complexity of the algorithm is O(n).
#include<stdio.h>
void main()
{
int arr[5]={-3,5,-10,-7,9},i;
int left=0;
int right=4;
printf("Array Before Segregated
");
for(i=0;i<5;i++)
printf("%d ",arr[i]);
while (left<right)
{
// Move left if the current element is negative
while(arr[left]<0 && left<right)
left++;
// Move right if the current element is positive
while(arr[right]>0 && left<right)
right--;
// Swap if left is positive and right is negative
if (left<right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
printf("
Array After Segregated
");
for(i=0;i<5;i++)
printf("%d ",arr[i]);
}
OUTPUT
Array Before Segregated
-3 5 -10 -7 9
Array After Segregated
-3 -7 -10 5 9
Q: 5 How can we describe an array in the best possible way?
Arrays are immutable
Container that stores the elements of similar types
The array is not a data structure
None of the above
[ Option B ]
An Array is a linear data structure that holds multiple elements of the same data type (homogeneous) in contiguous memory locations. This allows easy and efficient access to elements using their index. The index of an array usually starts from 0.
Q: 6 Given an array A[-8…2][-12…-2] with a base value of 5894 and the size of each element is 4 Byte in memory. Find the address of A[0][-4]. The elements of array stored in column-major order.
5994
6278
6482
6332
[ Option B ]
In column-major implementation, address of an element A[I][J] is calculated using the formula below:
Where,
Here the array is defined as A[-8…2][-12…-2]. So, we calculate the memory location using the custom index formula.
LOC(A[I][J])=BASE(A)+[(J-L2)×M+(I-L1)]W
Find location of A[0][-4]= ?
Given,
BASE (A) = 5894
W = 4 Bytes
L1 = -8
L2 = -12
U1 = 2
I = 0
J = -4
M = U1-L1 = 2-(-8)+1 = 11
LOC(A[0][-4]) = 5894+[(-4-(-12))*11+(0-(-8))]*4
LOC(A[0][-4]) = 5894+[8*11+8]*4
LOC(A[0][-4]) = 5894+[88+8]*4
LOC(A[0][-4]) = 5894+96*4
LOC(A[0][-4]) = 5894+384
LOC(A[0][-4]) = 6278
Q: 7 What are the advantages of array?
Easier to store elements of same data type
Convenient way to represent matrices as a 2-D array
Used to implement other data structure like stack and queue
All of the above
[ Option D ]
Arrays provide several advantages in programming. They offer an easy and organized way to store elements of the same data type, allow convenient representation of matrices using 2-D arrays, and are commonly used to implement other data structures such as stacks and queues.
Q: 8 The highest or largest valid index of an array is called its _________.
Length
Upper Bound
Capacity
Both (A) and (B)
[ Option B ]
The Upper Bound of an array refers to the largest valid index that can be accessed in the array. If an array has n elements and uses zero-based indexing, the lower bound is 0 and upper bound is n−1.
Q: 9 Which of the following highly uses the concept of an array?
Binary Search Tree
Scheduling of Processes
Spatial Locality
None of the above
[ Option C ]
Spatial Locality is a principle of memory access that states if a memory location is accessed, nearby memory locations are likely to be accessed soon. Since arrays store data in contiguous memory locations, accessing elements sequentially benefits heavily from spatial locality. Thus, arrays highly utilize this concept.
Q: 10 Consider a linear array A which records the number of cars sold every year from 1932. Let base address of A is 200 and each memory cell of A occupies 4 words. Then address of array element for the year 1965 is:
233
332
1965
2165
[ Option B ]
Array A stores number of cars sold each year starting from 1932 So, A[0] corresponds to year 1932 or lower bound of array is 1932.
Base address = 200
Each element occupies 4 words i.e., 4 Bytes.
We are asked to find the address of the element corresponding to year 1965 or find the address of index position 1965. So,
LOC (A[I])=Base(A) + (I-Lower Bound) * W
LOC (A[1965]) = 200 + (1965-1932)*4
LOC (A[1965]) = 200 + 33 * 4
LOC (A[1965]) = 200 + 132
LOC (A[1965]) = 332
Q: 11 How many subarrays are possible for an array with n elements.
2n-1
2n
n(n+1)/2
n(n-1)/2
[ Option C ]
A subarray is a contiguous part of an array. For an array of size n, the subarrays can range in size from 1 element up to n elements.
Suppose an array of 5 elements is as:
int arr[5]={11,22,33,44,55};
arr1[1]={11};
arr2[1]={22};
arr3[1]={33};
arr4[1]={44};
arr5[1]={55};
arr6[2]={11,22};
arr7[2]={22,33};
arr8[2]={33,44};
arr9[2]={44,55};
Remember, its array so consecutive sequence is required.
arr10[3]={11,22,33};
arr11[3]={22,33,44};
arr12[3]={33,44,55};
arr13[4]={11,22,33,44};
arr14[4]={22,33,44,55};
arr15[5]={11,22,33,44,55}; and so on.
If the size of the array is n = 5, then the total number of possible subarrays can be calculated as follows.
From the first element, we can form 5 subarrays, from the second element 4 subarrays, from the third element 3 subarrays, from the fourth element 2 subarrays, and from the last element 1 subarray.
So, the total number of subarrays is:
5+4+3+2+1 = 15
In general, for an array of size n, the total number of subarrays is:
n+(n−1)+(n−2)+ …+1
This is the sum of the first n natural numbers, which forms an arithmetic series. The sum of this series is given by the formula: n*(n+1)/2.
Therefore, an array of size n has exactly n(n+1)/2 subarrays.
Q: 12 Which of the following are the subarrays which can be formed from the following Array: arr[ ]={1,2,3};
{1} {2} {3} {1,2} {2,3} {3,1} {1,2,3}
{1,2} {2,3} {1,2,3} {2,3,1}
{1} {2} {3} {1,2} {1,3} {2,3} {1,2,3}
{1} {2} {3} {1,2} {2,3} {1,2,3}
[ Option D ]
A subarray is a contiguous part of an array, meaning the elements must be taken in order and without skipping any element in between.
For the array arr[] = {1,2,3}, the possible subarrays are {1}, {2}, {3}, {1,2}, {2,3}, and {1,2,3}. All these subarrays maintain the original order and continuity of elements.
Q: 13 Which of the following languages stores 2-D arrays in row-major order?
Fortran
C
MATLAB
R
[ Option B ]
In row-major order, elements of a 2-D array are stored row by row in memory. This means the elements in the first row are stored first, followed by the second row, and so on.
(1) C uses row-major storage → rows stored one after another in memory.
(2) Fortran, MATLAB, R use column-major → columns stored one after another.
Q: 14 Consider a two-dimensional array A[20][10]. Assume 4 words per memory cell, the base address of array A is 100, elements are stored in row-major order and first element is A[0][0]. What is the address of A[11][5]?
560
570
460
575
[ Option A ]
The array A[20][10] is stored in row-major order, which means elements are stored row by row. The formula to calculate the address of an element A[i][j] in row-major order is:
LOC(A[I][J])=BASE(A)+[I×N+J]×W
Where,
When the index starts from 0, the number of columns is specified in the array declaration. For example, in A[20][10], the total number of columns is 10.
So,
Base Address = 100
Number of Columns (N) = 10
Size of each element (W) = 4
Element to find = A[11][5] i.e., I = 11 and J = 5
LOC(A[11][5]) = 100+[11*10+5]*4
LOC(A[11][5]) = 100+[110+5]*4
LOC(A[11][5]) = 100+[115]*4
LOC(A[11][5]) = 100+460
LOC(A[11][5]) = 560
NOTE:
Q: 15 Which of the following is the advantage of the array data structure?
Elements of mixed data types can be stored
Easier to access the elements in an array
Index of the first element starts from 1
More than one of the above
[ Option B ]
Arrays are a data structure used to store multiple values of the homogeneous type (same data type) under single name in continuous memory locations. Because of this continuous storage, arrays provide a major advantage, the elements can be accessed very quickly using their index.
Q: 16 Which of the following best represents the time complexity of accessing an element in an array by its index if array contains n elements.
O(n)
O(1)
O(n2)
O(log n)
[ Option B ]
Accessing an element in an array by its index takes constant time, regardless of the number of elements in the array. This is because arrays store elements in contiguous memory locations, and the address of any element can be calculated directly using its index.
If the array contains n elements, accessing arr[i] requires the same amount of time for any valid index. Hence, the time complexity of accessing an array element by index is O(1).
| ARRAY OPERATION | DESCRIPTION | TIME COMPLEXITY |
|---|---|---|
| Access | Accessing an element using index. | O(1) |
| Update | Modifying an element using index. | O(1) |
| Traversal | Visiting all elements one by one. | O(n) |
| Linear Search | Linear search in an unsorted array. | O(n) |
| Binary Search | Binary search in a sorted array. | O(log n) |
| Insertion (End) | Insert element at the end. | O(1) |
| Insertion (Beginning) | Insert element at the beginning. | O(n) |
| Insertion (Middle) | Insert element at any position. | O(n) |
| Deletion (End) | Delete last element. | O(1) |
| Deletion (Beginning) | Delete first element. | O(n) |
| Deletion (Middle) | Delete element from any position. | O(n) |
Q: 17 LB are UB are lower bound and upper bound of a linear array LA. Consider following algorithm-
1. Repeat for K=LB to UB apply PROCESS to LA[K]
2. Exit.
The algorithm ____________ the array LA.
Sorts
Searches
Traverses
Merges
[ Option C ]
Q: 18 What is the time complexity for inserting and deleting at the beginning of the array?
O(1)
O(log n)
O(n log n)
O(n)
[ Option D ]
In an array, elements are stored in contiguous memory locations. When an element is inserted or deleted at the beginning, all the existing elements must be shifted one position to maintain the order of the array.
This shifting operation requires traversing almost all n elements, which takes linear time. Therefore, the time complexity for both insertion and deletion at the beginning of an array is O(n).
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.