# Array and Array Operations – 1

Subject:  Data Structure

Topic: Array and Array Operations

Difficulty Level: Moderate

No. of Questions: 10

#### 1. What is the time complexity of accessing an element in an array?

a) O(n)

b) O(log n)

c) O(1)

d) O(n log n)

Answer Explanation: Accessing an element in an array takes constant time, O(1), because arrays allow direct access to any element using its index.

#### 2. Which of the following is not a valid array declaration in C?

a) int arr[10];

b) float arr[5];

c) int arr[] = {1, 2, 3};

d) int arr[3] = {1, 2, 3, 4};

Answer: d) int arr[3] = {1, 2, 3, 4};

Answer Explanation: The array declaration in option d) is invalid because the number of initializers exceeds the specified size of the array.

#### 3. What will be the output of the following C code snippet?``````int arr[] = {10, 20, 30, 40}; printf("%d", arr[2]);``````

a) 10

b) 20

c) 30

d) 40

Answer Explanation: The array element at index 2 is 30.

#### 4. Which of the following operations is the most time-consuming in a linear array?

a) Accessing an element

b) Inserting an element at the beginning

c) Inserting an element at the end

d) Modifying an element

Answer: b) Inserting an element at the beginning

Answer Explanation: Inserting an element at the beginning of a linear array requires shifting all the elements to the right, making it the most time-consuming operation.

#### 5. How can you initialize an array in C during declaration?

a) int arr = {1, 2, 3};

b) int arr[3] = {1, 2, 3};

c) int arr[3]; arr = {1, 2, 3};

d) int arr(3) = {1, 2, 3};

Answer: b) int arr[3] = {1, 2, 3};

Answer Explanation: The correct syntax for initializing an array during declaration is shown in option b).

#### 6. What does the following code snippet do?``````int arr[5] = {1, 2, 3, 4, 5}; for(int i = 0; i < 5; i++) { arr[i] = arr[i] * 2; }``````

a) Doubles the value of each element in the array

b) Squares the value of each element in the array

c) Halves the value of each element in the array

d) Adds 2 to each element in the array

Answer: a) Doubles the value of each element in the array

Answer Explanation: The loop multiplies each element of the array by 2, effectively doubling their values.

#### 7. How do you find the length of an array in C?

a) sizeof(array) / sizeof(array[0])

b) sizeof(array)

c) length(array)

d) array.length

Answer Explanation: The correct way to find the length of an array in C is by dividing the total size of the array by the size of one element.

#### 8. Which of the following is a disadvantage of arrays?

a) Random access is not allowed

b) Wastage of memory space

c) Elements are stored in contiguous memory locations

d) Index value of the first element is 1

Answer: b) Wastage of memory space

Answer Explanation: Arrays can lead to wastage of memory space if the number of elements inserted into the array is less than the size of the array.

#### 9. What is the output of the following code snippet?``````int arr[] = {1, 3, 5, 7, 9}; printf("%d", arr[4]);``````

a) 1

b) 5

c) 7

d) 9

Answer Explanation: The array element at index 4 is 9.

a) malloc()

b) alloc()

c) calloc()

d) realloc()