NEW
Font size
WorksheetsSession 5 Array fundamentals
Total questions: 10
Worksheet time: 10mins
How do we declare array of 10 integers?
int arr;
string arr[10];
int arr[9];
int arr[10];
Which index that array in c++ start from?
1
0
-1
How can you access the 5th element of an array named arr?
arr[4];
arr[5]
arr(5);
arr{4}
What happens if you try to access an index outside the array bounds?
Compilation error always
Undefined behavior
The program automatically resizes the array
Nothing happens, safe access
C++ allows dynamically changing the size of a fixed-size array once declared.
True
False
The size of an array must be a constant expression in C++.
True
False
Arrays in C++ can store elements of different data types?
True
False
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
sum=100
sum=150
sum=70
sum=0
int arr[4] = {3, 6, 9, 12};
int total = 0;
for(int i = 0; i < 4; i++) {
total += arr[i];
cout << "Step " << i+1 << ": total = " << total << endl;
}
=> what will be the total value after 2 iterations (cycles)
3
6
9
18
#include <iostream>
using namespace std;
int main() {
int numbers[3] = {5, 10, 15};
cout << numbers[1] << endl;
return 0;
}
output= 5
output= 10
output= 15
output= 5+10
