This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which one of the following is the correct definition of “is_array();” function in C++?
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: 2Which of the following correctly declares an array in C++?
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: 3Which of the following gives the nth element of the Array?
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: 4What 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;
}
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: 5In C++, for what purpose the “rank()” is used?
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: 3You have reached the end of this topic. Continue learning with the next topic below.
Thank 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.