mcq on arrays C language,single dimensional array mcq,multidimensional arrays mcq, and more

Q5.What is the size of the following array declaration in C? int numbers[5];
a) 5
b) 10
c) 20
d) The size is not defined
Answer: a) 5


Q6.How do you initialize an array in C with values {10, 20, 30, 40, 50}?
a) int arr[5] = {10, 20, 30, 40, 50};
b) arr_int[] = {10, 20, 30, 40, 50};
c) arr{10, 20, 30, 40, 50};
d) int arr= {10, 20, 30, 40, 50};
Answer: a) int arr[5] = {10, 20, 30, 40, 50};


Q7.Which of the following statements is true for arrays in C?
a) Arrays can be resized at runtime
b) Arrays in c are only passed by reference to functions
c) Arrays store elements in a contiguous memory location
d) Arrays can store elements of different data types
Answer: c) Arrays store elements in a contiguous memory location


Q8.What is the value of numbers[2] in the following array initialization? int numbers[5] = {0};
a) 0
b) Garbage value
c) Error
d) 2
Answer: a) 0


Q9.What is the correct way to pass an array to a function in C?
a) Passing the array name
b) Passing each element of the array
c) Passing the array size
d) Arrays cannot be passed to functions
Answer: a) Passing the array name


Q10.Which keyword is used to skip the current iteration in a loop in C?
A. break;
B. continue;
C. pass;
D. skip;
Answer: B.continue;


Q11.Which of the following is true about the size of an array in C?
a) The size of an array must be specified at compile time
b) The size of an array can be changed at runtime
c) The size of an array is decided by the number of elements stored in it
d) The size of an array can be dynamically allocated
Answer: a) The size of an array must be specified at compile time


Q12.How do you find the length of an array in C?
a) Using the sizeof() operator
b) Using the strlen() function
c) Using the length() method
d) None of the above
Answer: a) Using the sizeof() operator


Q13.choose the the correct output of the following code ?
int numbers[3] = {1, 2, 3};
printf("%d", numbers[3]);
a) 0
b) 3
c) Garbage value
d) Error
Answer: c) Garbage value


Q14.How do you assign a value of 42 to the second element of an array named 'ages' in C?
a) ages(1) = 42;
b) ages[2] = 42;
c) ages.2 = 42;
d) ages = {42};
Answer: b) ages[2] = 42;


Q15.What is the correct syntax to declare a 2D array in C with 3 rows and 4 columns?
a) int matrix[3][4];
b) array_2d[3,4];
c) int[4][3] matrix;
d) int matrix = [3][4];
Answer: a) int matrix[3][4];


Q16.How do you access the element in row 2, column 3 of a 2D array named 'matrix' in C?
a) matrix(2, 3)
b) matrix[2][3]
c) matrix{2, 3}
d) matrix.2.3
Answer: b) matrix[2][3]


Q17.What is the correct way to initialize a 2D array in C with values {{1, 2, 3}, {4, 5, 6}}?
a) int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
b) array_2d = {{1, 2, 3}, {4, 5, 6}};
c) matrix{1, 2, 3, 4, 5, 6}
d) int matrix = {{1, 2, 3}, {4, 5, 6}};
Answer: a) int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};


Q18.Output of the following code is_______.
int matrix[2][2] = {{10, 20}, {30, 40}};
printf("%d", matrix[1][1]);
a) 10
b) 20
c) 30
d) 40
Answer: d) 4


Q19.Which of the following statements is true for 2D arrays in C?
a) 2D arrays are arrays of pointers
b) 2D arrays store elements in a non-contiguous memory location
c) 2D arrays can have rows of different lengths
d) 2D arrays cannot be passed to functions
Answer: c) 2D arrays can have rows of different lengths


Q20.How do you pass a 2D array to a function in C?
a) Passed as a single pointer
b) Passed as multiple pointers
c) Passed as a single array
d) Passed as multiple arrays
Answer: a) Passed as a single pointer


Q21.What is the correct way to declare a 3D array in C with dimensions 2x3x4?
a) int cube[2][3][4];
b) array_3d[2, 3, 4];
c) int[4][2][3] cube;
d) int cube = [2][3][4];
Answer: a) int cube[2][3][4];


Q22.How do you access the element in the second row, third column, and fourth depth of a 3D array named 'box' in C?
a) box[2][3][4]
b) box(2, 3, 4)
c) box{2, 3, 4}
d) box.2.3.4
Answer: a) box[2][3][4]


Q23.What is the correct way to initialize a 3D array in C with values {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}}?
a) int box[3][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}};
b) array_3d = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}};
c) box{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
d) int box = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}};
Answer: a) int box[3][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}};


Q24.What will be the output of the following program after successful execution?
int box[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
printf("%d", box[1][1][1]);
a) 5
b) 6
c) 7
d) 8
Answer: d) 8


Q25.What is the correct way to declare a jagged array in C?
a) int** jagged_arr;
b) int jagged_arr[][];
c) int* jagged_arr[];
d) int** jagged_arr[];
Answer: c) int* jagged_arr[];


Q26.How do you access the element in the second row and first column of a jagged array named 'table' in C?
a) table{2}{1}
b) table(2, 1)
c) table[2][1]
d) table.2.1
Answer: c) table[2][1]


Q27.What is the correct way to initialize a jagged array in C with values {{1}, {2, 3}, {4, 5, 6}}?
a) int* table[] = {1, 2, 3, 4, 5, 6};
b) int table[][] = {{1}, {2, 3}, {4, 5, 6}};
c) int table[3][] = {{1}, {2, 3}, {4, 5, 6}};
d) int* table[] = {{1}, {2, 3}, {4, 5, 6}};
Answer: d) int* table[] = {{1}, {2, 3}, {4, 5, 6}};


Q28.What is the correct output of the following code?
int* table[] = {1, 2, 3, 4, 5, 6};
printf("%d", table[2][1]);
a) 1
b) 2
c) 3
d) 4
Answer: b) 2


Q29.How do you access the element in the second row, third column, and fourth depth of a jagged 3D array named 'pyramid' in C?
a) pyramid[2][3][4]
b) pyramid(2, 3, 4)
c) pyramid{2, 3, 4}
d) pyramid.2.3.4
Answer: a) pyramid[2][3][4]


Q30.What is the correct way to initialize a jagged 3D array in C with values {{{1}, {2, 3}, {4, 5, 6}}, {{7}, {8, 9}, {10, 11, 12}}}?
a) int* pyramid[][] = {{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}};
b) int pyramid[2][3][] = {{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}};
c) int* pyramid[][] = {{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}};
d) int pyramid[] = {{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}};
Answer: a) int* pyramid[][] = {{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}};


Q31.Find the correct output of following program code?
int* pyramid[][] = {{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}};
printf("%d", pyramid[1][1][1]);
a) 1
b) 2
c) 3
d)4
Answer: c) 3


Q32.Which of the following is not true about arrays in C?
a) Arrays is used to store elements of different types of data
b) Arrays are only passed by reference to a functions
c) Arrays store elements in a contiguous memory location
d) Arrays can be resized at runtime
Answer: d) Arrays can be resized at runtime


Q33.How do you declare a dynamic array in C?
a) int array[];
b) int* array;
c) array_int[];
d) int array = dynamic;
Answer: b) int* array;


Q34.What is the output of the following code snippet?
int* array = malloc(5 * sizeof(int));
printf("%d", sizeof(array));
a) 5
b) 20
c) Garbage value
d) Error
Answer: b) 20


Q35.How do you access the third element of a dynamic array named 'arr' in C?
a) arr[3]
b) arr(2)
c) arr{2}
d) arr.3
Answer: a) arr[3]


Q36.What is the correct way to initialize a dynamic array in C with values {10, 20, 30, 40, 50}?
a) int* arr = malloc(5 * sizeof(int));
arr = {10, 20, 30, 40, 50};
b) int* arr = {10, 20, 30, 40, 50}?
c) int* arr = malloc(5 * sizeof(int));
arr[5] = {10, 20, 30, 40, 50};
d) int* arr = malloc(5 * sizeof(int));
arr = malloc(10, 20, 30, 40, 50);
Answer: c) int* arr = malloc(5 * sizeof(int));
arr[5] = {10, 20, 30, 40, 50};


Q37.What is the correct way to pass a dynamic array to a function in C?
a) Passed as a single pointer
b) Passed as multiple pointers
c) Passed as a single array
d) Passed as multiple arrays
Answer: a) Passed as a single pointer


Q38.Which of the following statements is true for dynamic arrays in C?
a) Dynamic arrays cannot be resized
b) Dynamic arrays store elements in non-contiguous memory locations
c) Dynamic arrays must be explicitly deallocated after use
d) Dynamic arrays are limited to a fixed size
Answer: c) Dynamic arrays must be explicitly deallocated after use


Q39.What is the value of arr[2] in the following dynamic array initialization?
int* arr = malloc(3 * sizeof(int)); arr[1] = 2;
a) 2
b) 0
c) Garbage value
d) Error
Answer: a) 2


Q40.How do you deallocate memory allocated for a dynamic array named 'data' in C?
a) free(data);
b) destroy(data);
c) del(data);
d) None of the above
Answer: a) free(data);


Previous Topic:-->>Loops MCQ. || Next topic:-->>Functions MCQ.