Multiple-Choice Questions
1. What is an array?
A) A single value variable
B) A data structure to store multiple values of similar data types
C) A function
D) A type of loop
2. Which format is correct for declaring an array?
A) int array;
B) array int[5];
C) int array[5];
D) int array[5] = {};
3. What does int arr[5] = {}; do?
A) Initializes the array with zeros
B) Leaves the array uninitialized
C) Causes a compilation error
D) Initializes the array with ones
4. How can you access the third element of an array named arr?
A) arr[3]
B) arr[2]
C) arr[0]
D) arr[1]
5. What is the correct way to initialize an array with specific values?
A) int arr[5] = {1, 2, 3, 4, 5};
B) int arr = {1, 2, 3, 4, 5};
C) int arr[] = {1, 2, 3, 4, 5};
D) Both A and C
6. Which statement is true about array subscripts?
A) Subscripts start at 1
B) Subscripts start at 0
C) Subscripts can be negative
D) Subscripts can be floats
7. What does the following code do? int arr[5]; arr[0]=10; arr[1]=5; arr[2]=arr[0]+arr[1];
A) Initializes the first element of arr with 10
B) Initializes the second element of arr with 5
C) Initializes the third element of arr with the sum of the first and second elements
D) All of the above
8. How do you access all elements of an array using a loop?
A) for(int i=0; i<size; i++) { cout << arr[i]; }
B) for(int i=1; i<=size; i++) { cout << arr[i]; }
C) while(size < 5) { cout << arr[size]; }
D) None of the above
9. How do you input values into an array using cin?
A) cin >> arr;
B) cin >> arr[i];
C) for(int i=0; i<size; i++) { cin >> arr[i]; }
D) Both B and C
10. What is wrong with newValues = oldValues; in copying arrays?
A) Nothing, it's correct
B) Arrays cannot be copied this way
C) Arrays must be copied element by element
D) Both B and C
11. What happens if you access an array out of its bounds?
A) Nothing
B) The program will crash
C) It may overwrite other memory
D) The compiler will catch the error
12. What is the result of this code: for(int i=0; i<5; i++) { arr[i]=i*2; }?
A) Initializes each element to 2
B) Initializes elements to 0, 2, 4, 6, 8
C) Initializes elements to 1, 2, 3, 4, 5
D) Causes a compilation error
13. What does the tests array contain after this code: int tests[5]={79,82,91,77,84};?
A) {0, 0, 0, 0, 0}
B) {79, 82, 91, 77, 84}
C) {79, 82, 0, 0, 0}
D) {84, 77, 91, 82, 79}
14. How do you find the sum of array elements?
A) Using the + operator on the array
B) Using a loop to add each element
C) Using a built-in function
D) All of the above
15. How can you find the largest element in an array?
A) Using a loop to compare each element
B) Using a built-in function
C) Sorting the array first
D) Both A and C
16. What does a parallel array mean?
A) Arrays with the same data type
B) Arrays with related data at the same subscript
C) Arrays with unrelated data
D) Arrays with different sizes
17. How do you process a two-dimensional array?
A) Using nested loops
B) Using a single loop
C) Using no loops
D) Both A and B
18. What is the correct syntax for declaring a two-dimensional array?
A) int arr[2][3];
B) int arr(2)(3);
C) int arr[2,3];
D) int arr{2}{3};
19. How do you initialize a two-dimensional array?
A) int arr[2][2] = { {1, 2}, {3, 4} };
B) int arr[2][2] = {1, 2, 3, 4};
C) int arr[2][2] = {{1, 2}, {3, 4}};
D) Both A and C
20. How can you access elements in a two-dimensional array?
A) Using single index
B) Using double index
C) Using triple index
D) None of the above
21. What does this code do: int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};?
A) Initializes a 2D array with 1-9
B) Initializes a 3D array with 1-9
C) Initializes a 1D array with 1-9
D) Causes a compilation error
22. How do you copy elements from one array to another?
A) newArr = oldArr;
B) Using a loop to copy each element
C) Using a built-in function
D) Both A and B
23. What will cout << tests[i]; do in a loop accessing array elements?
A) Print the array element at position i
B) Print the address of the array element
C) Cause a compilation error
D) Print the entire array
24. What is the output of this code: int tests[5] = {1, 2, 3, 4, 5}; for(int i=0; i<5; i++) { cout << tests[i] << "
"; }?
A) 12345
B) 1 2 3 4 5
C) 54321
D) 5 4 3 2 1
25. What is a common error when working with arrays?
A) Off-by-one errors
B) Out-of-bounds access
C) Incorrect initialization
D) All of the above
26. What does const int SIZE = 5; int arr[SIZE]; do?
A) Declares an array of size 5
B) Initializes the array with 5
C) Declares a constant array
D) Both A and B
27. How do you find the average of array elements?
A) By summing elements and dividing by the number of elements
B) Using a built-in function
C) By accessing the middle element
D) By sorting the array
28. What does the code int largest = arr[0]; for(int i=1; i<size; i++) { if(arr[i] > largest) largest = arr[i]; }
do?
A) Finds the smallest element
B) Finds the largest element
C) Finds the average element
D) Finds the first element
29. What is int tests[ISIZE] = {79, 82}; an example of?
A) Complete initialization
B) Partial initialization
C) Dynamic initialization
D) None of the above
30. How can arrays be used in functions?
A) By passing the array as an argument
B) By returning an array from the function
C) Both A and B
D) Arrays cannot be used in functions
31. What does this code do: for(int i=0; i<SIZE; i++) { tests2[i] = tests[i]; }?
A) Copies elements from tests to tests2
B) Compares elements of tests and tests2
C) Initializes tests2 with zeros
D) Causes a compilation error
32. How do you sort an array?
A) Using the sort function
B) By comparing and swapping elements
C) Both A and B
D) Arrays cannot be sorted
33. What is the size of int arr[10]; in bytes if int is 4 bytes?
A) 10 bytes
B) 40 bytes
C) 4 bytes
D) 14 bytes
34. What does the ++ operator do when used with array elements?
A) Increments the array size
B) Increments the value of the element
C) Causes a compilation error
D) None of the above
35. What is a two-dimensional array similar to?
A) A list
B) A matrix
C) A single variable
D) A loop
36. How do you access the element in the second row and third column of a 2D array arr?
A) arr[3][2]
B) arr[2][3]
C) arr[1][2]
D) arr[2][1]
37. What will cout << arr[i][j]; do in a nested loop accessing a 2D array?
A) Print the element at row i and column j
B) Print the address of the element
C) Cause a compilation error
D) Print the entire array
38. How do you declare a three-dimensional array?
A) int arr[3][3];
B) int arr[3][3][3];
C) int arr(3)(3)(3);
D) int arr[3,3,3];
39. How do you initialize a three-dimensional array?
A) int arr[2][2][2] = {1, 2, 3, 4, 5, 6, 7, 8};
B) int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
C) int arr[2][2][2] = {1, 2, 3, 4, 5, 6, 7, 8};
D) Both A and B
40. What is the output of this code: int arr[2][2] = { {1, 2}, {3, 4} }; cout << arr[1][1];?
A) 1
B) 2
C) 3
D) 4
Essay Questions
1. Identify the Error in the Code
1.
int arr[5];
arr[5] = 10;
2.
int arr[10];
for (int i = 0; i <= 10; i++) {
arr[i] = i;
3.
int tests[5] = {1, 2, 3, 4, 5};
cout << tests;
4.
int x[3] = {1, 2, 3};
x[4] = 5;
5.
int y;
int arr[3] = {0, 1, 2};
y = arr[3];
2. Determine the Output of the Code
1.
int x = 2;
int y = 4;
cout << x++ << --y;
2.
int count = 0, number = 0, limit = 4;
do {
number += 2;
count++;
} while (count < limit);
cout << number << " " << count << endl;
3.
int x;
for (x = 5; x <= 14; x += 3)
cout << x << endl;
cout << x << endl;
4.
int a, x, y = 0;
for (x = 0; x < 10; x++) {
cout << "Enter the number: ";
cin >> a;
y += a;
cout << "The sum of those numbers is " << y << endl;
5.
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";