Question 3
How do you pass an array to a function in C++?
By copying
By value
By pointer
By name
Question 4
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int arr[3] = {1, 2};
for(int i = 0; i < 3; i++)
cout << arr[i] << " ";
return 0;
}
1 2 3
1 2 1
1 2 0
Compilation error
Question 5
What will be the output of the following code?
#include <iostream>
using namespace std;
void modifyArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
arr[i] += 5;
}
}
int main() {
int arr[3] = {1, 2, 3};
modifyArray(arr, 3);
for(int i = 0; i < 3; i++) {
cout << arr[i] << " ";
}
return 0;
}
1 2 3
6 7 8
1 7 3
Compilation error
Question 6
Which of the following are true about multidimensional arrays in C++?
They are arrays of arrays
The syntax to declare a 2D array is type arrayName[rows][columns];
They can have two or more dimensions
All of the above
Question 7
What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
cout << arr[1][1];
return 0;
}
1
2
5
6
Question 8
Which of the following is a correct way to dynamically allocate a 1D array of 20 integers in C++?
int* arr = new int[20];
int* arr = new int();
int* arr = new int[20]();
int* arr = new int* [20];
Question 9
Which of the following statements about arrays is/are incorrect?
Arrays are always passed by value to functions
The name of the array is a pointer to its first element
Arrays can be initialized at the time of their declaration
None of the above
Question 10
What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int* p = arr;
cout << *(p + 2);
return 0;
}
1
3
4
2
There are 23 questions to complete.