C array MCQs
In this article, we will discuss the C array MCQs.
- How do you initialize an array at the time of declaration?
a.int arr[] = {1, 2, 3};
b.int arr() = {1, 2, 3};
c.int arr{} = {1, 2, 3};
d.int arr = {1, 2, 3};
Answer: (a) int arr[] = {1, 2, 3};
2. What is the correct syntax to access the element in the 2nd row and 3rd column of a 2D array named "matrix"?
a.matrix[2][3]
b.matrix(2, 3)
c.matrix(3, 2)
d.matrix[3][2]
Answer: (a) matrix[2][3]
3. What is the index of the last element in a C array with ten elements?
a. 9
b. 10
c. 11
d. 0
Answer: (a) 9
4. What is the correct way to access the third element in a C array named "numbers"?
a. numbers(2)
b. numbers[2]
c. numbers{3}
d. numbers.3
Answer: (b) numbers[2]
5. What is an array in C?
a. A sequence of variables that are all the same data type
b. A sequence of variables that can be of different data types
c. A collection of unrelated variables
d. A single variable that holds multiple data types
Answer: (a) A sequence of variables that are all the same data type
6. What will be the output of the following code snippet?
int arr[5] = {1, 2, 3};
printf("%d", arr[3]);
- 0
- 1
- 3
- Garbage value
Answer: (d) Garbage value
7. Can the size of an array be changed after its declaration in C?
a. Yes
b. No
Answer: (b) No
8. How do you pass an array as an argument to a function in C?
a. By Value
b. By reference
c. By pointer
d. Arrays cannot be passed to functions
Answer: (c) By pointer
9. How do you declare a one-dimensional array in C?
a. int arr[];
b. int[] arr;
c. int arr[];
d. int arr[10];
Answer: (d) int arr[10];
10. What is the purpose of the "sizeof" operator in C when used with an array?
a. To get the size of an individual element in the array
b. To get the number of elements in the array
c. To get the total memory size occupied by the array
d. To get the first element of the array
Answer: (c) To get the total memory size occupied by the array
11. In C, arrays are always passed to functions:
a. By Value
b. By reference
c. By pointer
d. By their names
Answer: (d) By their names
12. How do you find the length of an array in C?
a. sizeof(arr)
b. length(arr)
c. len(arr)
d. arr. length
Answer: (c) len(arr)
13.What will be the value of arr[5] if arr is an integer array declared as follows:
int arr[5] = {1, 2, 3};
- 1
- 2
- 3
- Garbage value
Answer: (d) Garbage value
14. What is the correct syntax to initialize a 2D array in C?
a. int arr[2, 2] = {{1, 2}, {3, 4}};
b. int arr[2][2] = {1, 2, 3, 4};
c. int arr[][] = {{1, 2}, {3, 4}};
d. int arr[2][2] = {{1, 2}, {3, 4}};
Answer: (d) int arr[2][2] = {{1, 2}, {3, 4}};
15. What will be the output of the following code snippet?
int arr[5] = {0};
printf("%d", arr[4]);
- 0
- 4
- Garbage value
- Compiler error
Answer: (a) 0
16. Which of the following is the correct way to initialize a character array with the string "Hello" in C?
a. char str[] = "Hello";
b. char str[5] = "Hello";
c. char str[] = {'H', 'e', 'l', 'l', 'o'};
d. char str[6] = “Hello";
Answer: (a) char str[] = "Hello";
17. What is the size of the array "numbers" if declared as int numbers[10];?
a. 10
b. 40 (assuming each int takes 4 bytes)
c. 8 (assuming each int takes 4 bytes)
d. 0
Answer: (b) 40 (assuming each int takes 4 bytes)
18. What will be the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[10]);
- 10
- 0
- Garbage value
- Runtime error
Answer: (c) Garbage Value
19.Can a C array have negative indices?
a.Yes
b.No
Answer: (b) No
20.How do you access the address of the first element of an array named "data"?
a. &data
b. data
c. *data
d. *(data + 1)
Answer: (b) data
21. What is the correct syntax to declare a 2D array with 3 rows and 4 columns in C?
a. int arr[3, 4];
b. int arr[3][4];
c. int arr(3, 4);
d. int arr{3, 4};
Answer: (b) int arr[3][4];
22. Which of the following is an invalid array declaration in C?
a. double values[5];
b. char names[
c. int numbers[0];
d. float scores[];
Answer: (c) int numbers[0];
23. What is the output of the following code snippet?
int arr[5] = {1, 2, 3};
arr[3] = 4;
printf("%d", arr[3]);
- 1
- 2
- 3
- 4
Answer: (d) 4
24. What will be the output of the following code snippet?
int arr[3] = {10, 20, 30};
printf("%p", arr);
- 10
- 20
- 30
- The memory address of the first element of the array
Answer: (d)The memory address of the first element of the array
25. What is the maximum number of dimensions an array can have in C?
a. 1
b. 2
c. 3
d. It depends on the compiler
Answer: (d) It depends on the Compiler
26. What is the purpose of the "memset" function in C?
a. To allocate memory for an array
b. To initialize an array with a specific value
c. To resize an array
d. To copy one array to another
Answer: (b) To initialize an array with a specific value
27. What will be the output of the following code snippet?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", ptr[2]);
- 0
- 3
- Garbage value
- Compiler error
Answer: (c) Garbage Value
28. What is the correct way to represent a 3x3 2D array in C?
a. int arr[3][3];
b. int arr[3, 3];
c. int arr[9];
d. int arr[3][];
Answer: (a) int arr[3][3];
29. What is the output of the following code snippet?
int arr[3] = {1, 2, 3};
int *ptr = arr;
printf("%d", *ptr);
- 0
- 1
- 2
- Garbage value
Answer: (b) 1
30. How do you find the number of elements in a 2D array with m rows and n columns?
a. sizeof(array)
b. m * n
c. sizeof(array) / sizeof(array[0])
d. m + n
Answer: (b) m * n
31. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
printf("%d\n", *ary);
}
- 1
- Compile time error
- Some garbage value
- Undefined variable
Answer: (a) 1
32. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int p[4];
p = ary;
printf("%d\n", p[1]);
}
- a)1
- Compile time error
- Undefined behaviour
- 2
Answer: (b) Compile time error
33. What elements are present in the array of the following C code?
int array[5] = {5};
- 5, 5, 5, 5, 5
- 5, 0, 0, 0, 0
- 5, (garbage), (garbage), (garbage), (garbage)
- (garbage), (garbage), (garbage), (garbage), 5
Answer: (b) 5, 0, 0, 0, 0
34. What will be the output of the following C code?
#include <stdio.h>
int main()
{
const int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5;
printf("%d\n", ary[3]);
}
- 4
- 5
- Compile time error
- 3
Answer: (b) 5
35. Which of the following declaration is illegal?
a. int a = 0, b = 1, c = 2;
int array[3] = {a, b, c};
b. int size = 3;
int array[size];
c. int size = 3;
int array[size] = {1, 2, 3};
d. All of the mentioned
Answer: (c)
int size = 3;
int array[size] = {1, 2, 3};
36.What is the maximum number of dimensions an array in C may have?
a. Two
b. eight
c. sixteen
d. Theoretically no limit. The only practical limits are memory size and compilers
Answer: (d) Theoretically no limit. The only practical limits are memory size and compilers
37. What will be the address of the arr[2][3] if arr is a 2-D long array of 4 rows and five columns and starting address of the array is 2000?
a. 2048
b. 2056
c. 2052
d. 2042
Answer: (c) 2052
38.A one-dimensional array A has indices 1....75. Each element is a string and takes up three memory words. The array is stored at location 1120 decimal. The starting address of A[49] is:
a. 1264
b. 1164
c. 1167
d. 1267
Answer: (a) 1264