C Programming Arrays MCQs
1D Arrays in C (30 Questions)
1. How do you declare a 1D array of 10 integers in C?
A) int array(10);
B) int array[10];
C) array int[10];
D) int array{10};
Answer: B) int array[10];
In C, 1D arrays are declared with square brackets []. The correct syntax is type name[size].
2. What is the index of the first element in a C array?
A) -1
B) 0
C) 1
D) It depends on declaration
Answer: B) 0
C arrays are zero-indexed, meaning the first element is at index 0.
3. What happens if you access an array element with an index equal to the array size?
A) Returns the last element
B) Returns a garbage value
C) Causes a compilation error
D) Causes undefined behavior
Answer: D) Causes undefined behavior
Accessing beyond array bounds leads to undefined behavior in C, which might crash or return garbage values.
4. How do you initialize all elements of a 1D array to 0?
A) int arr[5] = {0, 0, 0, 0, 0};
B) int arr[5] = {0};
C) int arr[5] = {};
D) Both A and B
Answer: D) Both A and B
In C, if you provide at least one initializer, all remaining elements are automatically initialized to 0.
5. What is the correct way to pass a 1D array to a function?
A) func(int arr[])
B) func(int arr[size])
C) func(int *arr)
D) All of the above
Answer: D) All of the above
In C, arrays decay to pointers when passed to functions, so all these forms are equivalent.
6. What is the output of: int arr[5] = {1}; printf("%d", arr[3]);
A) 1
B) 0
C) Garbage value
D) Compilation error
Answer: B) 0
When partially initialized, remaining elements are set to 0.
7. Which operation is NOT valid on arrays in C?
A) arr1 = arr2
B) sizeof(arr)
C) &arr[0]
D) arr + 1
Answer: A) arr1 = arr2
Arrays cannot be assigned directly using the = operator.
8. What does sizeof(arr) return if arr is declared as int arr[10]?
A) 10
B) 40 (assuming 4-byte integers)
C) Size of pointer
D) Depends on elements stored
Answer: B) 40 (assuming 4-byte integers)
sizeof returns total bytes: 10 elements * 4 bytes each = 40 bytes.
9. How can you find the number of elements in a 1D array?
A) sizeof(array)/sizeof(array[0])
B) length(array)
C) array.length
D) count(array)
Answer: A) sizeof(array)/sizeof(array[0])
This divides total array size by size of one element.
10. What is the relationship between arrays and pointers in C?
A) Arrays are pointers
B) Arrays decay to pointers
C) Pointers are arrays
D) No relationship
Answer: B) Arrays decay to pointers
Array names convert to pointers in most expressions (array decay).
11. What is the correct way to declare and initialize an array with values 1, 2, 3?
A) int arr[] = {1, 2, 3};
B) int arr[3] = {1; 2; 3};
C) int arr[] = (1, 2, 3);
D) int arr[3] = {1:2:3};
Answer: A) int arr[] = {1, 2, 3};
Curly braces {} are used for array initialization with comma-separated values.
12. How do you modify the third element of an array 'nums'?
A) nums(2) = value;
B) nums[3] = value;
C) nums[2] = value;
D) nums.2 = value;
Answer: C) nums[2] = value;
Array indices start at 0, so the third element is at index 2.
13. What is the output of: int arr[] = {1,2,3}; printf("%d", arr[arr[1]]);
A) 1
B) 2
C) 3
D) Compilation error
Answer: C) 3
arr[1] is 2, so arr[arr[1]] becomes arr[2] which is 3.
14. Which of these correctly declares an array of pointers to integers?
A) int *arr[10];
B) int (*arr)[10];
C) int arr*[10];
D) int arr[10]*;
Answer: A) int *arr[10];
This declares an array of 10 pointers to integers.
15. What is the output of: char str[] = "Hello"; printf("%d", sizeof(str));
A) 5
B) 6
C) 4
D) Compilation error
Answer: B) 6
String literals include a null terminator, so "Hello" occupies 6 bytes.
16. How do you declare a constant array that cannot be modified?
A) const int arr[] = {1,2,3};
B) int const arr[] = {1,2,3};
C) static int arr[] = {1,2,3};
D) Both A and B
Answer: D) Both A and B
const can appear before or after the type, both forms are equivalent.
17. What is the output of: int arr[3] = {1}; arr[1] = arr[0]++; printf("%d", arr[1]);
A) 0
B) 1
C) 2
D) Undefined
Answer: B) 1
The post-increment returns the original value (1) before incrementing.
18. Which of these correctly declares an array of function pointers?
A) int (*arr[10])();
B) int *arr[10]();
C) (int *)arr[10]();
D) int arr[10]*();
Answer: A) int (*arr[10])();
This declares an array of 10 pointers to functions returning int.
19. What is the output of: int arr[] = {1,2,3,4,5}; printf("%d", *(arr+2));
A) 1
B) 2
C) 3
D) Address of arr[2]
Answer: C) 3
Pointer arithmetic: *(arr+2) is equivalent to arr[2].
20. How do you declare an array that can be modified in another file?
A) extern int arr[];
B) global int arr[];
C) public int arr[];
D) shared int arr[];
Answer: A) extern int arr[];
The extern keyword declares an array defined in another file.
21. What is the output of: int arr[5] = {0}; printf("%d", arr[10]);
A) 0
B) Garbage value
C) Compilation error
D) Runtime error
Answer: B) Garbage value
Accessing beyond array bounds may return garbage values (undefined behavior).
22. Which of these correctly declares a pointer to an array of 5 integers?
A) int (*ptr)[5];
B) int *ptr[5];
C) int *(ptr[5]);
D) int ptr*[5];
Answer: A) int (*ptr)[5];
This declares a pointer to an array of 5 integers.
23. What is the output of: int arr[] = {1,2,3}; int *p = arr; printf("%d", *++p);
A) 1
B) 2
C) Address of arr[1]
D) 3
Answer: B) 2
++p increments the pointer before dereferencing, so it points to arr[1].
24. How do you declare an array that can hold 10 strings of max length 20?
A) char arr[10][20];
B) char arr[20][10];
C) char *arr[10];
D) string arr[10][20];
Answer: A) char arr[10][20];
This creates a 2D array of 10 rows (strings) each with 20 columns (chars).
25. What is the output of: int arr[3] = {5}; printf("%d, %d", arr[0], arr[1]);
A) 5, 0
B) 5, 5
C) 5, garbage
D) Compilation error
Answer: A) 5, 0
Partial initialization sets remaining elements to 0.
26. Which of these correctly copies one array to another?
A) arr2 = arr1;
B) memcpy(arr2, arr1, sizeof(arr1));
C) copy(arr1, arr2);
D) arr2[] = arr1[];
Answer: B) memcpy(arr2, arr1, sizeof(arr1));
memcpy() is the standard way to copy array contents in C.
27. What is the output of: int arr[] = {1,2,3}; printf("%d", *arr + 1);
A) 1
B) 2
C) Address of arr[1]
D) 3
Answer: B) 2
*arr gives 1 (value at arr[0]), then adding 1 gives 2.
28. How do you declare an array that persists between function calls?
A) auto int arr[10];
B) static int arr[10];
C) const int arr[10];
D) register int arr[10];
Answer: B) static int arr[10];
static variables retain their values between function calls.
29. What is the output of: int arr[3] = {1,2,3}; printf("%d", arr[arr[0]]);
A) 1
B) 2
C) 3
D) Undefined
Answer: B) 2
arr[0] is 1, so arr[arr[0]] becomes arr[1] which is 2.
30. Which of these correctly declares an array of 10 function pointers returning int?
A) int (*arr[10])();
B) int *arr[10]();
C) int arr[10]*();
D) int (*arr)(10);
Answer: A) int (*arr[10])();
This declares an array of 10 function pointers returning int.
2D Arrays in C (20 Questions)
1. How do you declare a 2D array of 3 rows and 4 columns in C?
A) int array[3,4];
B) int array[3][4];
C) int array(3)(4);
D) int array{3,4};
Answer: B) int array[3][4];
2D arrays in C are declared with consecutive pairs of square brackets [][].
2. How is a 2D array stored in memory in C?
A) Column-major order
B) Row-major order
C) Depends on compiler
D) Random order
Answer: B) Row-major order
C stores 2D arrays in row-major order - all elements of a row are stored contiguously.
3. What is the correct way to initialize a 2D array?
A) int arr[2][3] = {{1,2,3}, {4,5,6}};
B) int arr[][3] = {1,2,3,4,5,6};
C) int arr[2][3] = {1,2,3,4,5,6};
D) All of the above
Answer: D) All of the above
All these methods are valid ways to initialize a 2D array in C.
4. When passing a 2D array to a function, which dimension can be omitted?
A) First dimension
B) Second dimension
C) Both dimensions
D) Neither dimension
Answer: A) First dimension
Only the first dimension can be omitted when declaring a 2D array parameter.
5. How do you access the element in 2nd row and 3rd column of array 'mat'?
A) mat[2][3]
B) mat[1][2]
C) mat(2,3)
D) mat[3][2]
Answer: B) mat[1][2]
Array indices start at 0, so 2nd row is index 1 and 3rd column is index 2.
6. What is the output of: int arr[2][2] = {{1,2}, {3,4}}; printf("%d", arr[0][1] + arr[1][0]);
A) 3
B) 5
C) 7
D) Compilation error
Answer: B) 5
arr[0][1] is 2 and arr[1][0] is 3, so 2 + 3 = 5.
7. How do you declare a pointer to a 2D array of 3 columns?
A) int (*ptr)[3];
B) int *ptr[3];
C) int **ptr;
D) int ptr[][3];
Answer: A) int (*ptr)[3];
This declares a pointer to an array of 3 integers, which can point to rows of a 2D array.
8. What is the output of: int arr[][3] = {1,2,3,4}; printf("%d", arr[1][1]);
A) 1
B) 2
C) 4
D) 0
Answer: D) 0
The array is initialized as {{1,2,3},{4,0,0}}, so arr[1][1] is 0.
9. How many elements are in: char grid[5][10];?
A) 5
B) 10
C) 15
D) 50
Answer: D) 50
5 rows × 10 columns = 50 elements total.
10. What is the correct function prototype to accept a 2D array with 4 columns?
A) void func(int arr[][]);
B) void func(int arr[][4]);
C) void func(int *arr[4]);
D) void func(int **arr);
Answer: B) void func(int arr[][4]);
When passing 2D arrays, column size must be specified.
11. What does this initialize: int matrix[2][3] = {0};?
A) Only matrix[0][0] to 0
B) First row to 0
C) All elements to 0
D) Causes compilation error
Answer: C) All elements to 0
Partial initialization sets all remaining elements to 0.
12. How to dynamically allocate a 2D array with 3 rows and 5 columns?
A) int **arr = malloc(3 * 5 * sizeof(int));
B) int **arr = malloc(3 * sizeof(int*)); for(int i=0; i<3; i++) arr[i] = malloc(5 * sizeof(int));
C) int arr = malloc(3,5);
D) int arr[3][5] = malloc(15 * sizeof(int));
Answer: B) int **arr = malloc(3 * sizeof(int*)); for(int i=0; i<3; i++) arr[i] = malloc(5 * sizeof(int));
This correctly allocates an array of pointers, then allocates each row.
13. What is the output of: int arr[2][2] = {1}; printf("%d", arr[1][1]);
A) 1
B) 0
C) Garbage value
D) Compilation error
Answer: B) 0
Only arr[0][0] is initialized to 1, all others are 0.
14. Which expression is equivalent to arr[i][j]?
A) *(arr + i + j)
B) *(*(arr + i) + j)
C) (arr + i)[j]
D) *arr[i] + j
Answer: B) *(*(arr + i) + j)
Pointer arithmetic for 2D arrays: arr[i][j] ≡ *(*(arr + i) + j).
15. What is the correct way to free a dynamically allocated 2D array?
A) free(arr);
B) free(arr[i]);
C) free(arr[0]); free(arr);
D) delete arr;
Answer: B) for(int i=0; i
Must free each row first, then the array of pointers.
16. What is the output of: int arr[2][3] = {{1,2,3},{4,5,6}}; printf("%d", *(arr[1] + 1));
A) 2
B) 4
C) 5
D) 6
Answer: C) 5
arr[1] points to second row, +1 moves to second element (5).
17. How to declare a 2D array where each row can have different lengths?
A) int arr[][];
B) int *arr[];
C) int **arr;
D) Both B and C
Answer: D) Both B and C
Both array of pointers and pointer-to-pointer can create jagged arrays.
18. What is the output of: int arr[2][2] = {{1,2},{3,4}}; printf("%d", *(arr + 1));
A) 1
B) Address of second row
C) 3
D) Compilation error
Answer: B) Address of second row
Pointer arithmetic on array name gives address of rows.
19. How to declare a 3D array of integers with dimensions 2x3x4?
A) int arr[2,3,4];
B) int arr[2][3][4];
C) int arr[2](3)(4);
D) int arr[2,3][4];
Answer: B) int arr[2][3][4];
3D arrays use three sets of square brackets.
20. What is the output of: int arr[2][3] = {1,2,3,4,5,6}; printf("%d", sizeof(arr)/sizeof(arr[0][0]));
A) 2
B) 3
C) 6
D) 24
Answer: C) 6
Total elements (2×3=6) = total size / element size.
Related Array Resources