Array Questions and Answers (C++)
Part 1: Multiple Choice Questions
1. First element of the array: int nums[5] = {10, 20, 30, 40, 50}; → 10
2. Correct declaration of 10 integers → int arr[10];
3. Index of first element in C++ → 0
4. Accessing out of bounds → Undefined behavior
5. Function to get size of array → sizeof(arr)
Part 2: Fill in the Blanks
6. int marks[4];
7. for(int i = 0; i < 5; i++) cout << arr[i] << ' ';
8. for(int i = 0; i < 10; i++) cin >> nums[i];
Part 3: Coding Questions
9. Reverse array of 5 integers:
int arr[5]; for(int i=0;i<5;i++)cin>>arr[i]; for(int i=4;i>=0;i--)cout<<arr[i]<<" ";
10. Sum of 6 integers:
int arr[6], sum=0; for(int i=0;i<6;i++){cin>>arr[i]; sum+=arr[i];} cout<<sum;
11. Largest number in 10 elements:
int arr[10], max=arr[0]; for(int i=1;i<10;i++) if(arr[i]>max) max=arr[i]; cout<<max;
12. Swap first and last:
int temp=arr[0]; arr[0]=arr[n-1]; arr[n-1]=temp;
13. Check sorted ascending:
bool sorted=true; for(int i=0;i<n-1;i++) if(arr[i]>arr[i+1]) sorted=false;
14. Average of elements:
float avg=sum/n;
15. 2D array (3x3) sum of each row:
for(int i=0;i<3;i++){int sum=0; for(int j=0;j<3;j++) sum+=arr[i][j]; cout<<sum<<endl;}
Part 4: Challenge Question
16. Compare two arrays A and B:
bool equal=true; for(int i=0;i<n;i++) if(A[i]!=B[i]) equal=false;
if(equal) cout<<"Equal"; else cout<<"Not Equal";