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

This set of C++ Programming Questions & Answers for Exams focuses on “STL Container Any – 2”.

1. Which exception is thrown if the typecasting is not done properly?
a) bad_type_cast
b) bad_any_cast
c) type_mismatched
d) bad_cast_mismatched
View Answer

Answer: b
Explanation: bad_any_cast exception is thrown when typecasting is not done properly by the user i.e. if any is storing int value and we are trying to cast it into a string then the program will throw bad_any_cast exception.

2. What is the use of emplace() function?
a) Used to change the object any container is holding
b) Used to add more item to the any list
c) Used to empty any container value
d) Used to check the type of any variable
View Answer

Answer: a
Explanation: emplace() function is used to change the object contained in any container i.e destroying the present object and creating the new object for the value given by the user.

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

advertisement
advertisement
#include<iostream>
#include<any>
using namespace std;
int main()
{
	float val = 5.5;
	any var(val);
	cout<<var<<endl;
	char c = 'a';
	var.emplace<char>(c);
	cout<<var<<endl;
	return 0;
}

a) 5.5
b) a
c)

5.5
a
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

d) Error
View Answer

Answer: c
Explanation: In this program we are using emplace() function to change the any variable contents and this is allowed in C++ therefore the program runs fine.

4. What is the use of type() function in any container?
a) Used to destroys the contained object in any variable
b) Used to change the object any container is holding
c) Used to return the type information about the any container
d) Used to check whether a container is empty or not
View Answer

Answer: c
Explanation: type() function is used to check the type of data/value the container object is holding.
advertisement

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

#include<iostream>
#include<any>
using namespace std;
int main()
{
	float val = 5.5;
	any var(val);
	cout<<var.type().name()<<endl;
	return 0;
}

a) f
b) d
c) Pkc
d) u
View Answer

Answer: a
Explanation: The type function is used to get information about the data stored in the any container variable. name() attribute is used to print the type id of the data. Now as the data stored in any variable is float therefore the program outputs f as f is the type id for float.
advertisement

6. What is the use of has_value() function in any container?
a) Used to destroys the contained object in any variable
b) Used to change the object any container is holding
c) Used to return the type information about the any container
d) Used to check whether any container is empty or not
View Answer

Answer: d
Explanation: has_value() function is provided to check whether a given any container is empty or not.

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

#include<iostream>
#include<any>
using namespace std;
int main()
{
	float val = 5.5;
	any var(val);
	if(var.has_value())
        {
            cout<<"Var is not Empty\n";
        }
        else
        {
            cout<<"Var is Empty\n";
        }
	return 0;
}

a) Var is Empty
b) Var is not Empty
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: As the variable is containing the information about the float value val = 5.5 therefore the container is not empty therefore the program outputs “Var is not Empty”.

8. What is the use of reset() function?
a) Used to destroys the contained object in any variable
b) Used to change the object any container is holding
c) Used to empty any container value
d) Used to check the type of any variable
View Answer

Answer: a
Explanation: reset() function is provided with any to destroy an object contained in any variable in case it is not needed.

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

#include<iostream>
#include<any>
using namespace std;
int main()
{
	float val = 5.5;
	any var(val);
    cout<<any_cast<float>(var)<<endl;
	var.reset();
	if(!var.has_value())
        {
		cout<<"var is empty\n";
	}
	else{
		cout<<"var is not empty\n";	
	}
	return 0;
}

a) var is empty
b)

5.5
var is not empty

c) 5.5
d)

5.5
var is empty
View Answer
Answer: d
Explanation: As the program uses reset() function which resets/destroys an object contained inside the any container therefore var becomes empty hence the program outputs “var is empty”.
 
 

10. In how many ways we can handle errors in any class?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two ways of handling errors in any container first by using exceptions like bad_any_cast and second by returning the pointer.

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.