Site icon T4Tutorials.com

One-dimensional and multi-dimensional arrays — C++ MCQs

1. What is a one-dimensional array in C++?

(A) An array of arrays


(B) A variable holding multiple data types


(C) An array stored in heap only


(D) A linear list of elements stored in contiguous memory



2. How do you declare an array of 5 integers in C++?

(A) int arr(5);


(B) int arr[5];


(C) int arr{5};


(D) int arr = 5;



3. What is the index of the first element in a C++ array?

(A) 1


(B) 1


(C) 0


(D) Depends on compiler



4. How do you access the 3rd element of an array named arr?

(A) arr(2)


(B) arr[2]


(C) arr[3]


(D) arr{3}



5. Which of the following is correct initialization of a one-dimensional array?

(A) int arr[5] = {1, 2, 3, 4, 5};


(B) int arr[5] = (1, 2, 3, 4, 5);


(C) int arr[5] = [1, 2, 3, 4, 5];


(D) int arr[5] = <1, 2, 3, 4, 5>;



6. What will be the output of this code?
int arr[3] = {5, 10, 15};
cout << arr[1];

(A) 5


(B) 10


(C) 15


(D) Error



7. What is a two-dimensional array?

(A) An array of integers


(B) A single variable with multiple values


(C) A dynamically allocated array


(D) An array of arrays



8. How do you declare a 2×3 integer array?

(A) int arr[2][3];


(B) int arr[3][2];


(C) int arr(2,3);


(D) int arr{2,3};



9. How do you access the element at 2nd row and 3rd column of arr?

(A) arr[2][3]


(B) arr[1][2]


(C) arr[3][2]


(D) arr(1,2)



10. What is the output?
int arr[2][2] = {{1,2},{3,4}};
cout << arr[1][0];

(A) 1


(B) 2


(C) 3


(D) 4



11. What is the default value of an uninitialized global array?

(A) Garbage


(B) 0


(C) NULL


(D) Compiler dependent



12. What is the output of this code?
int arr[5];
cout << arr[0];

(A) 0


(B) Error


(C) 1


(D) Garbage value



13. Can arrays in C++ have variable size?

(A) Yes, always


(B) No, size must be constant for static arrays


(C) Only global arrays


(D) Only inside functions



14. How is memory allocated for a 2D array?

(A) Contiguous memory block row-wise


(B) Separate blocks for each row


(C) Heap only


(D) Stack only



15. How do you initialize a 2×2 array with zeros?

(A) int arr[2][2] = {0};


(B) int arr[2][2] = {{0,0},{0,0}};


(C) Both A and B


(D) None



16. What will be the output?
int arr[3] = {1,2};
cout << arr[2];

(A) 0


(B) 2


(C) Garbage


(D) Error



17. Which of the following is illegal?

(A) int arr[5] = {1,2,3,4,5};


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


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


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



18. How do you declare a 3D array?

(A) int arr(2,2,2);


(B) int arr[2,2,2];


(C) int arr[2][2][2];


(D) int arr{2,2,2};



19. What is the output of this code?
int arr[2][3] = {{1,2,3},{4,5,6}};
cout << arr[1][2];

(A) 2


(B) 3


(C) 5


(D) 6



20. How do you calculate the number of elements in a 1D array arr?

(A) sizeof(arr)/sizeof(int)


(B) sizeof(arr)/sizeof(arr[0])


(C) length(arr)


(D) arr.size()



21. Arrays in C++ are:

(A) Dynamically allocated by default


(B) Zero-based indexed


(C) One-based indexed


(D) Can hold different data types



22. What is wrong with the declaration?
int arr[];

(A) Nothing


(B) Array name is missing


(C) Size must be specified or initialized


(D) Type missing



23. Which of the following is correct for multi-dimensional array initialization?

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


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


(C) Both A and B


(D) None



24. What is the output of this code?
int arr[3] = {5};
cout << arr[1];

(A) 5


(B) 0


(C) Garbage


(D) Error



25. How do you access elements in a multi-dimensional array using loops?

(A) Nested loops


(B) Single loop only


(C) Recursion only


(D) Cannot access



26. Which of the following is true about arrays in C++?

(A) Size can be changed after declaration


(B) Arrays are continuous memory blocks


(C) Can store multiple types


(D) Can store strings only



27. What will the output?
int arr[2][3] = {{1,2,3},{4,5,6}};
cout << arr[0][2];

(A) 2


(B) 3


(C) 5


(D) 6



28. Can you partially initialize a 2D array?

(A) Yes, uninitialized elements are zero


(B) No


(C) Only for 1D arrays


(D) Only for global arrays



29. Which of the following is invalid?

(A) int arr[5]; arr[5] = 10;


(B) int arr[5] = {1,2,3};


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


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



30. What is the output of this code?
int arr[2][2] = {{1,2},{3,4}};
cout << sizeof(arr)/sizeof(arr[0]);

(A) Error


(B) 4


(C) 8


(D) 2



31. Which of the following is true about multi-dimensional arrays?

(A) Memory is not contiguous


(B) Rows can have different lengths


(C) They are arrays of arrays


(D) Cannot store integers



Exit mobile version