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.
advertisement

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;

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;
advertisement

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;

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);

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.
advertisement

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”.
advertisement

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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn