Explanation: An array in C is a data structure that can store a fixed-size sequence of elements of the same data type.
Explanation: The correct syntax to declare an integer array of size 10 in C is `int array[10];`.
Explanation: In C, an array can be initialized to zero using any of these methods. If only one zero is provided, the remaining elements are automatically initialized to zero.
Explanation: In C, the indexing of array elements starts from 0, so the first element is at index 0.
Explanation: Arrays in C are zero-indexed, so the third element is accessed with index 2.
int array[5] = {1, 2, 3, 4, 5};
printf("%d", array[2]);
Explanation: The array `array` has its elements indexed from 0 to 4. `array[2]` refers to the third element, which is 3.
Explanation: The correct syntax to define a two-dimensional array of integers in C is `int array[3][4];`.
Explanation: In a two-dimensional array, the first index is the row, and the second index is the column. Since indexing starts from 0, `matrix[1][2]` accesses the second row and third column.
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
printf("%d", sum);
Explanation: The code sums up all elements of the array `arr`, resulting in 10 + 20 + 30 + 40 = 100.
int arr[][3] = {{1, 2, 3}, {4, 5, 6}};
Explanation: This declaration defines a two-dimensional array with 2 rows and 3 columns, where the elements are specified in the initialization list.
int arr[10];
Explanation: The size of the array `arr` is 10 integers, and each integer is 4 bytes. Therefore, the total size is 10 * 4 = 40 bytes.
Explanation: All the given options correctly initialize an array. The first and second explicitly specify the size, while the third initializes the first three elements and the rest are initialized to zero.
Explanation: The `sizeof()` operator in C is used to determine the size of a variable or an array in bytes.
int arr[3] = {10, 20, 30};
printf("%d", arr[3]);
Explanation: Accessing `arr[3]` is out of bounds since the valid indices for `arr` are 0, 1, and 2. This results in undefined behavior.
Explanation: In a zero-indexed array of size 5, the last element is at index 4. Thus, `arr[4] = 50;` correctly assigns the value 50 to the last element.
Explanation: All these methods correctly initialize an array of size 5 with all elements set to zero.
int arr[] = {5, 10, 15, 20};
printf("%d", arr[1] + arr[3]);
Explanation: `arr[1]` is 10 and `arr[3]` is 20. The sum is 10 + 20 = 30.
Explanation: In C, arrays are passed to functions by reference, meaning the address of the first element is passed, not the entire array.
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d", *(p + 2));
Explanation: `p` points to the first element of `arr`. `*(p + 2)` dereferences the pointer to the third element, which is 3.
int arr[5] = {0};
Explanation: The declaration initializes an array of 5 integers, where all elements are set to zero because only the first element is explicitly initialized, and the rest are implicitly set to zero.
Explanation: The correct syntax to declare a two-dimensional array of integers with 3 rows and 4 columns in C is `int arr[3][4];`.
Explanation: Each of these statements initializes all elements of a 3x3 two-dimensional array to zero.
Explanation: Since array indexing starts at 0, `arr[1][2]` accesses the element in the second row and third column.
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d", arr[1][1]);
Explanation: `arr[1][1]` accesses the element in the second row and second column, which is 5.
Explanation: The correct way to initialize a 2x2 array with these elements is to use nested curly braces to separate the rows.
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += arr[i][i];
}
printf("%d", sum);
Explanation: The code sums the diagonal elements of the array: 1 + 5 + 9 = 15.
Explanation: In a zero-indexed two-dimensional array, `arr[0][1]` refers to the first row and second column.
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
// Access arr[i][j]
for(int i = 0; i < 2; i++)
// Access arr[i][i]
for(int j = 0; j < 2; j++)
// Access arr[j][j]
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
// Access arr[j][i]
Explanation: To iterate through all elements of a 2x2 array, you need nested loops where the outer loop iterates over rows and the inner loop iterates over columns.
int arr[2][2] = {{1, 2}, {3, 4}};
printf("%d", arr[0][0] + arr[1][1]);
Explanation: `arr[0][0]` is 1 and `arr[1][1]` is 4. The sum is 1 + 4 = 5.
Explanation: In C, a two-dimensional array is stored in a single contiguous block of memory, with elements laid out row-wise.
Explanation: The correct syntax to declare a three-dimensional array of integers with dimensions 2x3x4 in C is `int arr[2][3][4];`.
Explanation: The correct way to initialize a 2x2x2 three-dimensional array with all elements set to 1 is to use nested curly braces for each dimension.
Explanation: In a zero-indexed three-dimensional array, `arr[0][1][2]` accesses the element in the first row, second column, and third depth.
int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
printf("%d", arr[1][0][1]);
Explanation: `arr[1][0][1]` accesses the element in the second row, first column, and second depth, which is 6.
int sum = 0;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
sum += arr[i][j][k];
int sum = 0;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
sum += arr[i][j];
int sum = 0;
for(int i = 0; i < 2; i++)
sum += arr[i];
int sum = 0;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
sum += arr[j][i][k];
Explanation: To sum all elements in a 2x2x2 three-dimensional array, nested loops are needed for each dimension.
Explanation: In C, multidimensional arrays are stored in a single contiguous block of memory in row-major order, meaning the rightmost index varies the fastest.
Explanation: The total number of elements in the array is the total size divided by the size of one element. `sizeof(arr) / sizeof(arr[0][0][0])` gives the number of elements.
int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
printf("%d", arr[0][1][1] + arr[1][1][0]);
Explanation: `arr[0][1][1]` is 4 and `arr[1][1][0]` is 7. The sum is 4 + 7 = 11.
Explanation: In a zero-indexed three-dimensional array, `arr[1][0][0]` refers to the second row, first column, and first depth.
Explanation: The correct syntax to declare a four-dimensional array of integers with dimensions 2x2x2x2 in C is `int arr[2][2][2][2];`.
Explanation: The correct syntax to initialize an array of 5 integers with these values is `int arr[5] = {1, 2, 3, 4, 5};`.
int arr[5] = {1, 2};
Explanation: When an array is partially initialized, the remaining elements are set to zero.
Explanation: Both `int arr[10] = {0};` and `int arr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};` correctly initialize all elements to zero.
Explanation: The correct syntax to initialize a two-dimensional array with these values is `int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};`.
int arr[4] = {0, 1, [3] = 3};
Explanation: This code uses designated initializers. The value at index 3 is explicitly set to 3, and the unspecified element at index 2 is initialized to 0.
Explanation: All these methods correctly initialize a character array with the string "Hello". Option A uses a character array with a null terminator, while B and C use string literals which automatically add the null terminator.
Explanation: In C, if an array is partially initialized, the unspecified elements are automatically initialized to zero.
int arr[3] = {1, 2};
printf("%d", arr[2]);
Explanation: Since the array is partially initialized, the third element `arr[2]` is automatically set to 0.
Explanation: The correct syntax to initialize a float array with these values is `float arr[4] = {1.1, 2.2, 3.3, 4.4};`.
int arr[5] = {[2] = 5, [4] = 10};
printf("%d %d %d %d %d", arr[0], arr[1], arr[2], arr[3], arr[4]);
Explanation: The code uses designated initializers to set `arr[2]` to 5 and `arr[4]` to 10. The remaining elements are initialized to 0.
Explanation: To insert an element into an array at a specific position, you need to shift the elements from that position to the right to make space for the new element.
Explanation: You can traverse an array using any of these loops: while loop, do-while loop, or for loop.
Explanation: Inserting an element at the beginning of an array requires shifting all existing elements to the right, which takes O(n) time.
Explanation: To delete an element from an array at a specific position, you need to shift the elements from that position to the left to fill the gap.
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
Explanation: The code traverses the array and prints each element, resulting in the output "1 2 3 4 5".
Explanation: Deleting an element from the end of an array is a constant time operation, O(1), since no elements need to be shifted.
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 3; i < 5; i++) {
arr[i - 1] = arr[i];
}
Explanation: The code shifts elements starting from index 3 to the left, resulting in `arr[2] = 4` and `arr[3] = 5`.
for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = x;
for (int i = n - 1; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = x;
for (int i = n - 1; i >= pos; i--) {
arr[i + 1] = arr[i];
}
arr[pos] = x;
for (int i = pos; i < n; i++) {
arr[i + 1] = arr[i];
}
arr[pos] = x;
Explanation: This code shifts elements to the right starting from the end of the array, making space for the new element at position `pos`.
Explanation: Traversing an array requires visiting each element once, resulting in a time complexity of O(n).
Explanation: Array indexing starts at 0, so the third position corresponds to index 2.
int arr[6] = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < 6; i += 2) {
printf("%d ", arr[i]);
}
Explanation: The loop increments `i` by 2 each time, so it prints the elements at even indices: 1, 3, and 5.
int arr[6] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < 6; i++) {
arr[i - 1] = arr[i];
}
Explanation: The loop shifts elements to the left starting from index 1, resulting in `arr[5]` being duplicated.
for (int i = pos; i < n; i++) {
arr[i] = arr[i + 1];
}
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
for (int i = pos; i < n - 1; i++) {
arr[i + 1] = arr[i];
}
for (int i = pos; i < n; i++) {
arr[i + 1] = arr[i];
}
Explanation: This code shifts elements to the left starting from position `pos`, effectively removing the element at that position.
Explanation: Deleting an element from the beginning of an array requires shifting all subsequent elements to the left, which takes O(n) time.
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
}
Explanation: The loop traverses the array in reverse order, printing each element.
Explanation: Array traversal is used to access and process each element in the array.
int arr[6] = {1, 2, 3, 4, 5, 6};
int pos = 3;
int x = 10;
for (int i = 5; i >= pos; i--) {
arr[i + 1] = arr[i];
}
arr[pos] = x;
Explanation: The code shifts elements to the right starting from index 5 to make space for the new element `x` at position 3.
Explanation: Inserting an element into a sorted array requires finding the correct position (O(log n)) and then shifting elements to make space (O(n)), resulting in an overall complexity of O(n).
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[n - i - 1]);
}
Explanation: Both methods correctly traverse the array in reverse order and print each element.
int pos;
for (pos = 0; pos < n; pos++) {
if (arr[pos] == x) break;
}
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
int pos;
for (pos = 0; pos < n; pos++) {
if (arr[pos] == x) break;
}
for (int i = pos + 1; i < n; i++) {
arr[i - 1] = arr[i];
}
Explanation: Both methods correctly find the first occurrence of `x` and shift elements to the left to remove it.
int arr[5] = {1, 2, 3, 4, 5};
int x = 3;
int pos;
for (pos = 0; pos < 5; pos++) {
if (arr[pos] == x) break;
}
for (int i = pos; i < 4; i++) {
arr[i] = arr[i + 1];
}
Explanation: The code finds the first occurrence of `x` and shifts elements to the left, but doesn't handle the last element properly.
Explanation: Traversing a two-dimensional array requires visiting each element once, resulting in a time complexity of O(m * n).
int arr[4] = {1, 2, 3, 4};
for (int i = 0; i < 4; i++) {
printf("%d ", arr[3 - i]);
}
Explanation: The loop prints the elements in reverse order.
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
}
printf("%d", max);
int max = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] > max) max = arr[i];
}
printf("%d", max);
int max = arr[1];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
}
printf("%d", max);
int max = arr[0];
for (int i = 1; i <= n; i++) {
if (arr[i] > max) max = arr[i];
}
printf("%d", max);
Explanation: This code correctly initializes `max` to the first element and updates it by comparing with each element in the array.
int arr[4] = {1, 2, 3, 4};
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += arr[i];
}
printf("%d", sum);
Explanation: The code calculates the sum of the array elements, resulting in `1 + 2 + 3 + 4 = 10`.
int arr[6] = {1, 2, 3, 4, 5, 6};
int x = 3;
int pos;
for (pos = 0; pos < 6; pos++) {
if (arr[pos] == x) break;
}
for (int i = pos; i < 5; i++) {
arr[i] = arr[i + 1];
}
Explanation: The code finds the first occurrence of `x` and shifts elements to the left, but duplicates the last element.
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
for (int i = 0; i < n; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i];
arr[n - i] = temp;
}
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 2];
arr[n - i - 2] = temp;
}
Explanation: This code correctly swaps the elements from the beginning with the elements from the end up to the middle of the array.
Explanation: Array insertion can involve adding elements at the beginning, end, or any specific position in the array.
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
if (arr[i] == 3) continue;
printf("%d ", arr[i]);
}
Explanation: The `continue` statement skips the printing of the element `3`.
int arr[10] = {0};
int arr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Explanation: Both methods correctly initialize all elements of the array to zero.
for (int i = 0; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
for (int i = 0; i < n; i++) {
arr[i] = arr[i + 1];
}
for (int i = 1; i < n; i++) {
arr[i] = arr[i - 1];
}
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
Explanation: This code correctly shifts all elements one position to the left.
Explanation: In C, you pass an array to a function by passing a pointer to its first element.
void func(int arr[]);
void func(int arr[10]);
void func(int *arr);
Explanation: All these declarations are valid as they all indicate that the function takes a pointer to an integer.
printArray(arr, n);
printArray(arr);
printArray(&arr, n);
printArray(&arr);
Explanation: You call the function by passing the array and its size.
void modifyArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] += 1;
}
}
int main() {
int arr[3] = {1, 2, 3};
modifyArray(arr, 3);
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation: The function `modifyArray` increments each element of the array by 1.
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[4] = {4, 3, 2, 1};
printArray(arr, 4);
return 0;
}
Explanation: The function `printArray` prints each element of the array as it is.
int sumArray(int arr[], int n);
void sumArray(int arr[], int n);
int sumArray(int *arr, int n);
Explanation: Both declarations are correct since they both indicate a function that returns an integer and takes an array and its size as arguments.
int sumArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int arr[4] = {1, 2, 3, 4};
printf("%d", sumArray(arr, 4));
return 0;
}
Explanation: The function `sumArray` calculates the sum of the array elements, which is `1 + 2 + 3 + 4 = 10`.
Explanation: When an array is passed to a function, a pointer to the first element is passed, so the function can modify the original array.
Explanation: You need to specify the second dimension when passing a two-dimensional array to a function.
void func(int arr[][]);
void func(int arr[10][]);
void func(int arr[][10]);
void func(int *arr[10]);
Explanation: You need to specify at least the second dimension when defining a function that takes a two-dimensional array.
void modifyArray(int arr[][3], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] += 1;
}
}
}
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
modifyArray(arr, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
}
return 0;
}
Explanation: The function `modifyArray` increments each element of the two-dimensional array by 1.
void func(int *arr[]);
void func(int **arr);
void func(int arr[10]);
Explanation: Both declarations indicate that the function takes an array of pointers to integers.
void modify(int arr[], int n);
void modify(int *arr, int n);
Explanation: Both declarations are correct since they both indicate a function that takes a pointer to an array and its size as arguments.
void printEven(int arr[], int n) {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
printf("%d ", arr[i]);
}
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printEven(arr, 5);
return 0;
}
Explanation: The function `printEven` prints only the even elements of the array.
int maxElement(int arr[], int n);
void maxElement(int arr[], int n);
int maxElement(int *arr, int n);
Explanation: Both declarations are correct since they both indicate a function that returns an integer and takes an array and its size as arguments.
int maxElement(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int main() {
int arr[5] = {5, 8, 3, 9, 2};
printf("%d", maxElement(arr, 5));
return 0;
}
Explanation: The function `maxElement` finds and returns the maximum element in the array, which is 9.
Explanation: When passing an array to a function, the size of the array must be explicitly passed as a parameter.
void doubleArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] *= 2;
}
}
int main() {
int arr[4] = {1, 2, 3, 4};
doubleArray(arr, 4);
for (int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation: The function `doubleArray` doubles each element of the array.
void func(char *arr);
void func(char arr[]);
void func(char arr[10]);
Explanation: All these declarations are valid and indicate a function that takes an array of characters as an argument.
void printString(char str[]) {
printf("%s", str);
}
int main() {
char str[10] = "Hello";
printString(str);
return 0;
}
Explanation: The function `printString` prints the entire string stored in the character array.
void func(char *str[]);
void func(char **str);
void func(char str[][]);
void func(char str[][10]);
Explanation: When passing an array of strings to a function, it's typically represented as an array of pointers to characters.
void printStrings(char *str[]) {
for (int i = 0; str[i] != NULL; i++) {
printf("%s ", str[i]);
}
}
int main() {
char *arr[3] = {"One", "Two", "Three"};
printStrings(arr);
return 0;
}
Explanation: The function `printStrings` prints each string in the array until it encounters a NULL pointer.
Explanation: When passing a multidimensional array to a function, the size of each dimension must be specified except for the first dimension.
void printMatrix(int mat[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
printMatrix(arr, 2, 3);
return 0;
}
2 5
3 6
2
3
4
5
6
Explanation: The function `printMatrix` prints the elements of the 2D array row by row.