This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Consider 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]?
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: 2Which option correctly define the advantages of array?
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: 3Which of the following is an advantage of using arrays?
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: 4Consider 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?
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: 5How can we describe an array in the best possible way?
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: 6Given 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.
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: 7What are the advantages of array?
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: 8The highest or largest valid index of an array is called its _________.
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: 9Which of the following highly uses the concept of an array?
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: 10Consider 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:
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
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.