250+ TOP MCQs on STL Container Any – 1 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “STL Container Any – 1”.

1. What is any in C++?
a) STL container used to store a single value of any type
b) Exception class in C++
c) Fundamental type provided by C++
d) Template data type
Answer: a
Clarification: Any is an STL container provided by C++ to store value or objects of any type.

2. In how many different ways any-container can be constructed?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three basic ways of constructing any variable. They are done using copy initialization, using the constructor or using an assignment operator.

3. What is the correct syntax of constructing any using copy initialization?
a) any variable_name = value;
b) any variable_name(value);
c)

any variable_name;
variable_name = value;

d) any variable_name = value;
Answer: a
Clarification: To initialize an any variable using copy initialization we use the following syntax:

any variable_name = value;

4. What is the correct syntax of constructing any using parameterized constructor?
a) any variable_name = value;
b) any variable_name(value);
c)

any variable_name;
variable_name = value;

d) any variable_name = value;
Answer: b
Clarification: To initialize an any variable using parameterized constructor we use the following syntax:

any variable_name(value);

5. What is the correct syntax of constructing any using assignment operator?
a) any variable_name = value;
b) any variable_name(value);
c)

any variable_name;
variable_name = value;

d) any variable_name = value;
Answer: b
Clarification: To initialize an any variable using assignment operator we use the following syntax:

any variable_name;
variable_name = value;

6. Which of the following syntax is used to convert any variable to its original type?
a) any_cast();
b) any_cast(variable_name);
c) (variable_name);
d) any_cast(variable_name);
Answer: d
Clarification: The syntax used to convert the any variable to its original type is as follows:

any_cast(variable_name);

7. Which header file is required to use any container?
a)
b)
c)
d)
Answer: a
Clarification: header file is required to use any container and its realted functions.

8. What will be the output of the following C++ code?

#include
#include
using namespace std;
int main()
{
	int a = 5;
	any var = a;
	cout<<var<<endl;
	return 0;
}

a) 5
b) Compile-time error
c) Run-time error
d) Nothing is printed
Answer: b
Clarification: C++ does not allow programmer to directly print the value of any container variable. One need type cast the any variable before printing.

9. What will be the output of the following C++ code?

#include
#include
#include
using namespace std;
int main()
{
	string s = "Hello World";
	any var(s);
	cout<<any_cast<string>(var)<<endl;
	return 0;
}

a) Run-time error
b) Compile-time error
c) Hello World
d) Nothing is printed
Answer: d
Clarification: In the above program as we have converted the value to its original type before printing therefore the program runs perfectly and outputs “Hello World”.

10. What will be the output of the following C++ code?

#include
#include
#include
using namespace std;
int main()
{
	string s = "Hello World";
	any var(s);
	cout<<any_cast<char*>(var)<<endl;
	return 0;
}

a) Hello World
b) Compile-time error
c) Run-time error
d) Nothing is printed
Answer: c
Clarification: In this program as we are trying to convert an string into char* which is not same therefore the program gives run-time error saying bad_any_cast.

Leave a Reply

Your email address will not be published. Required fields are marked *