Practice questions on Arrays
Last Updated :
28 Dec, 2024
In this article, we will discuss some important concepts related to arrays and problems based on that. Before understanding this, you should have basic idea about Arrays.
Type 1. Based on array declaration -
These are few key points on array declaration:
- A single dimensional array can be declared as int a[10] or int a[] = {1, 2, 3, 4}. It means specifying the number of elements is optional in 1-D array.
- A two dimensional array can be declared as int a[2][4] or int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8}. It means specifying the number of rows is optional but columns are mandatory.
- The declaration of int a[4] will give the values as garbage if printed. However, int a[4] = {1,1} will initialize remaining two elements as 0.
Que - 1.
Predict output of following program
int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
(A) 1 followed by four garbage values: (B) 1 0 0 0 0 (C) 1 1 1 1 1 (D) 0 0 0 0 0
Solution:
As discussed, if array is initialized with few elements, remaining elements will be initialized to 0. Therefore, 1 followed by 0, 0, 0, 0 will be printed.
Que - 2.
Predict output of the following program:
int main()
{
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
printf("%d ", a[i][j]);
return 0;
}
(A) 1 2 3 4 (B) Compiler Error in line ” int a[][] = {{1,2},{3,4}};” (C) 4 garbage values (D) 4 3 2 1
Solution:
As discussed, specifying the number of columns in 2-D array is mandatory, so, it will give compile time error.
Type 2. Finding address of an element with given base address -
When an array is declared, a contiguous block of memory is assigned to it which helps in finding address of elements from base address. For a single dimensional array a[100], address of ith element can be found as:
addr(a[i]) = BA+ i*SIZE
Where BA represents base address (address of 0th element) and SIZE represents size of each element in the array. For a two dimensional array x[3][3], the elements can be represented as:

As 2-D array is stored in row major order in C language, row 0 will be stored first followed by row 1 and row 2. For finding the address of x[2][2], we need to go to 2nd row (each row having 3 elements). After reaching 2nd row, it can be accessed as single dimensional array. Therefore, we need to go to 2nd element of the array. Assuming BA as 0 and size as 1, the address of x[2][2] will be 0 + (2 * 3 + 2) * 1 = 8. For a given array with m rows and n columns, the address can be calculated as:
add(a[i][j]) = BA + (i*n + j) * SIZE
Where BA represents base address (address of 0th element), n represents number of columns in 2-D array and SIZE represents size of each element in the array.
Que - 3.
Consider the following declaration of a ‘two-dimensional array in C:
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[40][50] is: (GATE CS 2002) (A) 4040 (B) 4050 (C) 5040 (C) 5050
Solution:
Using the formula discussed,
addr[40][50] = 0 + (40*100 + 50) * 1 = 4050
Que - 4.
For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits. (GATE-CS-2014)
t0 = i * 1024
t1= j * 32
t2 = k * 4
t3 =t1 + t0
t4 = t3 + t2
t5 = X[t4]
Which one of the following statement about the source code of C program is correct? (A) X is declared as “int X[32][32][8]” (B) X is declared as “int X[4][1024][32]” (C) X is declared as “char X[4][32][8]” (D) X is declared as “char X[32][16][2]”
Solution:
For a three dimensional array X[10][20][30], we have 10 two dimensional matrices of size [20]*[30]. Therefore, for a 3 D array X[M][N][O], the address of X[i][j][k] can be calculated as:
BA + (i*N*O+j*O+k)*SIZE
Given different expressions, the final value of t5 can be calculated as:
t5 = X[t4] = X[t3+t2] = X[t1+t0+t2] = X[i*1024+j*32+k*4]
By equating addresses,
(i*N*O+j*O+k)SIZE = i*1024+j*32+k*4 = (i*256+j*8+k)4
Comparing the values of i, j and SIZE, we get
SIZE = 4, N*O = 256 and O = 8, hence, N = 32
As size is 4, array will be integer. The option which matches value of N and O and array as integer is (A).
Type 3. Accessing array elements using pointers -
- In a single dimensional array a[100], the element a[i] can be accessed as a[i] or *(a+i) or *(i+a)
- Address of a[i] can be accessed as &a[i] or (a+i) or (i+a)
- In two dimensional array a[100][100], the element a[i][j] can be accessed as a[i][j] or *(*(a+i)+j) or *(a[i]+j)
- Address of a[i][j] can be accessed as &a[i][j] or a[i]+j or *(a+i)+j
- In two dimensional array, address of ith row can be accessed as a[i] or *(a+i)
Que - 5.
Assume the following C variable declaration
int *A [10], B[10][10];
Of the following expressions
I. A[2]
II. A[2][3]
III. B[1]
IV. B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program (GATE CS 2003)? (A) I, II, and IV only (B) II, III, and IV only (C) II and IV only (D) IV only
Solution:
As given in the question, A is an array of 10 pointers and B is a two dimensional array. Considering this, we take an example as:
int *A[10], B[10][10];
int C[] ={1, 2, 3, 4, 5};
As A[2] represents an integer pointer, it can store the address of integer array as: A[2] = C; therefore, I is valid. As A[2] represents base address of C, A[2][3] can be modified as: A[2][3] = *(C +3) = 0; it will change the value of C[3] to 0. Hence, II is also valid. As B is 2D array, B[2][3] can be modified as: B[2][3] = 5; it will change the value of B[2][3] to 5. Hence, IV is also valid. As B is 2D array, B[2] represent address of 2nd row which can’t be used at LHS of statement as it is invalid to modify the address. Hence III is invalid.
Similar Reads
PreComputation Technique on Arrays Precomputation refers to the process of pre-calculating and storing the results of certain computations or data structures(array in this case) in advance, in order to speed up the execution time of a program. This can be useful in situations where the same calculations are needed multiple times, as
15 min read
array::at() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par
2 min read
Commonly Asked Data Structure Interview Questions on Array Arrays are one of the most fundamental data structures in computer science.It allows for efficient access to elements using an index, which is particularly useful for applications that involve large amounts of data or require quick retrieval of items.Array elements (in C, C++ and Java Primitives) or
7 min read
Types of Arrays There are majorly 4 types of arrays1. Fixed Size Array2. Dynamic Sized Array3. 1-Dimensional Array4. Multi-Dimensional ArrayClassification of Types of ArraysHowever, these array types can be broadly classified in two ways:On the basis of SizeOn the basis of DimensionsTypes of ArraysTable of ContentC
4 min read
Top Conceptual Questions and Answers on Array for Interviews An Array is a fundamental concept in computer science that stores a collection of elements in a contiguous block of memory. It allows for efficient access to elements using indices and is widely used in programming for organizing and manipulating data.In our article âTop Array Interview Questions an
9 min read
Commonly Asked Data Structure Interview Questions To excel in a Data Structure interview, a strong grasp of fundamental concepts is crucial. Data structures provide efficient ways to store, organize, and manipulate data, making them essential for solving complex problems in software development.Interviewers often test candidates on various data str
6 min read
Searching in Array Searching is one of the most common operations performed in an array. Array searching can be defined as the operation of finding a particular element or a group of elements in the array. There are several searching algorithms. The most commonly used among them are: Linear Search Binary Search Ternar
4 min read
QuickSort Based Practice Problems Problems based on Partitioning AlgorithmSorting an Array of Two Types.Stable Binary SortingStable Binary Sorting with O(1) SpaceThree Way Partitioning Around a RangeThree Way Partitioning Around a ValueSorting an Array of Three TypesProblems based on Quick SortKth Smallest ElementK Smallest Elements
1 min read
Top 50 Array Coding Problems for Interviews Array is one of the most widely used data structure and is frequently asked in coding interviews to the problem solving skills. The following list of 50 array coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.Easy ProblemsSecond Largest
2 min read
Array Definition & Meaning in DSA An array is a collection of items of same data type stored at contiguous memory locations Array exampleTypes of Arrays:One-dimensional Array: It is the simplest kind of array where all the elements are stored linearly in a single row. Two-dimensional Array: A two-dimensional array is an array with 2
4 min read