C++ Programming Questions and Answers – STL Container Any – 1

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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;
advertisement
advertisement

d) any <type>variable_name = value;
View Answer

Answer: a
Explanation: 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;
Note: Join free Sanfoundry classes at Telegram or Youtube

d) any <type>variable_name = value;
View Answer

Answer: b
Explanation: 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;
advertisement

d) any <type>variable_name = value;
View Answer

Answer: b
Explanation: 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<variable_name>();
b) any_cast(variable_name);
c) <original_type>(variable_name);
d) any_cast<original_type>(variable_name);
View Answer

Answer: d
Explanation: The syntax used to convert the any variable to its original type is as follows:

any_cast(variable_name);
advertisement

7. Which header file is required to use any container?
a) <any>
b) <stl>
c) <container-any>
d) <containers>
View Answer

Answer: a
Explanation: <any> header file is required to use any container and its realted functions.

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

#include<iostream>
#include<any>
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
View Answer

Answer: b
Explanation: 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<iostream>
#include<any>
#include<string>
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
View Answer

Answer: d
Explanation: 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<iostream>
#include<any>
#include<string>
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
View Answer

Answer: c
Explanation: 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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.