Array Questions

These questions are generally asked in university exam

Indexed based Array question from topic DSA IN C

1. Find the address of LA[25], if the base address of a one-dimensional linear array (LA) is 5000, and each element of the array occupies 4 bytes.

Answer:

To find the address of LA[25], we use the formula for calculating the address of an element in a one-dimensional array:

Address of LA[i] = Base Address + (i * Size of each element)

Here, the base address = 5000, i = 25, and the size of each element = 4 bytes.

Address of LA[25] = 5000 + (25 * 4) = 5000 + 100 = 5100

So, the address of LA[25] is 5100.

2. Given an array of size 10, what would be the index of the 7th element in 0-based indexing?

Answer:

In 0-based indexing, the first element has index 0. Therefore, the 7th element in the array would have index 6.

3. Given a 1D array of size 10, what will be the address of the 5th element if the base address is 2000 and each element occupies 2 bytes?

Answer:

To find the address , we use the formula for calculating the address of an element in a one-dimensional array:

Address of LA[i] = Base Address + (i * Size of each element)

Here, base address = 2000, i = 5, and size of each element = 2 bytes.

Address of LA[5] = 2000 + (5 * 2) = 2000 + 10 = 2010

4. What is the difference between row-major and column-major order in the context of arrays?

Answer:

In row-major order, elements of a multi-dimensional array are stored row by row, meaning elements of the first row are stored first, followed by the second row, and so on.

In column-major order, elements are stored column by column, meaning elements of the first column are stored first, followed by the second column, and so on.

5. Find the address of LA[12] if the base address of a one-dimensional array LA is 1200, and each element occupies 8 bytes. Assume that array indexing starts from 5 instead of 0.

Answer:

To find the address of LA[12], we use the formula for calculating the address of an element in a one-dimensional array:

Address of LA[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 1200, starting index = 5, i = 12, and size of each element = 8 bytes.

Address of LA[12] = 1200 + ((12 - 5) * 8) = 1200 + (7 * 8) = 1200 + 56 = 1256

So, the address of LA[12] is 1256.

6. What will be the index of the 100th element in a 1D array of size 200, given that the array uses negative indexing starting from -50?

Answer:

To find the index of the 100th element, we add 99 to the starting index:

Index of the 100th element = -50 + 99 = 49

So, the index of the 100th element is 49.

7. Find the address of A[20] if the base address of a one-dimensional array A is 3000, each element occupies 4 bytes, and the array uses 1-based indexing.

Answer:

Using 1-based indexing, the address is calculated using:

Address of A[i] = Base Address + ((i - 1) * Size of each element)

Here, base address = 3000, i = 20, and size of each element = 4 bytes.

Address of A[20] = 3000 + ((20 - 1) * 4) = 3000 + (19 * 4) = 3000 + 76 = 3076

So, the address of A[20] is 3076.

8. In an array of size 50, what will be the index of the element in the 30th position if the array indexing starts at 10 (0-based)?

Answer:

To find the index of the 30th element, we add 29 to the starting index:

Index of the 30th element = 10 + 29 = 39

So, the index of the element in the 30th position is 39.

9. Find the address of B[50] if the base address of a one-dimensional array B is 4000, each element occupies 2 bytes, and the array is indexed from -10.

Answer:

To find the address of, we use the formula for calculating the address of an element in a one-dimensional array:

Address of B[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 4000, starting index = -10, i = 50, and size of each element = 2 bytes.

Address of B[50] = 4000 + ((50 - (-10)) * 2) = 4000 + (60 * 2) = 4000 + 120 = 4120

So, the address of B[50] is 4120.