Skip to main content

array-and-strings

1).Which of the following is the correct syntax to declare an array in C++?

1) int arr[];
2) int[] arr;
3) array<int> arr;
4) None of the above

2).What is the index of the first element of an array in C++?

1) 1
2) 0
3) -1
4) None of the above

3).What will be the output of the following code?

#include <iostream>
using namespace std;

int main() {
int arr[] = {10, 20, 30, 40};
cout << arr[1];
return 0;
}
1) 10
2) 20
3) 30
4) 40

4).Which function is used to find the length of a string in C++?

1) length()
2) size()
3) str_length()
4) None of the above

5).Which of the following is the correct way to initialize a string in C++?

1) string str = "Hello";
2) char str[] = "Hello";
3) char[] str = "Hello";
4) All of the above

6).Which of the following functions is used to copy one string to another in C++?

1) strcpy()
2) string_copy()
3) copy_string()
4) None of the above

7).What will be the output of the following code?

#include <iostream>
#include <cstring>
using namespace std;

int main() {
char str1[] = "Hello";
char str2[] = "Hello";
cout << (strcmp(str1, str2) == 0 ? "Equal" : "Not Equal");
return 0;
}
1) Equal
2) Not Equal
3) Error
4) None of the above

8).What is the size of the array `int arr[5]` in C++?

1) 5
2) 10
3) 20
4) Depends on the compiler

9).How do you access the third element of an array `arr[]` in C++?

1) arr[2]
2) arr[3]
3) arr(2)
4) arr(3)

10).What is the correct way to declare a multidimensional array in C++?

1) int arr[3][3];
2) int arr[3,3];
3) int arr[3, 3] = {};
4) None of the above