Q: 1 Which one of the following is the correct definition of “is_array();” function in C++?
It checks that the specified variable is of the array or not
It checks that the specified array is of single dimension or not
It checks that the specified array is of multi-dimension or not
More than one of the above
[ Option A ]
In C++, is_array<T>::value is a type trait provided in the <type_traits> header. Its purpose is only to determine whether the given type T is an array type or not.
It does NOT check whether an array is 1D or multi-dimensional. It only checks “Is this type an array?”
Q: 2 Which of the following correctly declares an array in C++?
array{10};
int array;
int array[10];
More than one of the above
[ Option C ]
An array in C++ is a collection of similar type (homogeneous) items a single variable, stored in unique and successive (contiguous) memory location. An array items are accessed by index (starting from 0). To declare an array, specify the data type, array name, and size inside square brackets [].
Q: 3 Which of the following gives the nth element of the Array?
array[n];
array[n-1];
array[n+1];
None of the above
[ Option B ]
In most programming languages, arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on. Therefore, to access the nth element of an array, you need to use array[n-1].
Q: 4 What will be the output of the following C++ code?
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
char s1[6]=”Hello”;
char s2[6]=”World”;
char s3[12]=s1+” “+s2;
cout<<s3;
return 0;
}
Hello
World
Error
None of the above
[ Option C ]
In C++, you cannot directly add C-style character arrays using the + operator. In the given code, s1 and s2 are character arrays, and the statement char s3[12] = s1 + " " + s2; is invalid. C++ does not allow concatenation of arrays or char* using +.
Q: 5 In C++, for what purpose the “rank()” is used?
It returns the size of each dimension
It returns the maximum number of elements that can be stored in the array
It returns the dimension of the specified array
More than one of the above
[ Option C ]
In C++, the rank() function (std::rank<T>() from <type_traits>) is used with arrays to determine the number of dimensions of the array, not the size of each dimension or the total number of elements. For example, a one-dimensional array has a rank of 1, a two-dimensional array has a rank of 2, and so on.
This is useful when working with multidimensional arrays to programmatically determine their structure.
#include <iostream>
#include <type_traits>
using namespace std;
int main()
{
int arr1[6];
int arr2[2][3];
int arr3[2][3][4];
cout << "Rank (Dimension) of arr1: " << std::rank<decltype(arr1)>::value << endl;
cout << "Rank (Dimension) of arr2: " << std::rank<decltype(arr2)>::value << endl;
cout << "Rank (Dimension) of arr3: " << std::rank<decltype(arr3)>::value << endl;
return 0;
}
OUTPUT
Rank (Dimension) of arr1: 1
Rank (Dimension) of arr2: 2
Rank (Dimension) of arr3: 3Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.