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

C++ Programming Questions & Answers for Exams 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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?

#include
#include
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

d) Error
Answer: c
Clarification: 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
Answer: c
Clarification: type() function is used to check the type of data/value the container object is holding.

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

#include
#include
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
Answer: a
Clarification: 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.

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
Answer: d
Clarification: 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
#include
using namespace std;
int main()
{
	float val = 5.5;
	any var(val);
	if(var.has_value())
        {
            cout<<"Var is not Emptyn";
        }
        else
        {
            cout<<"Var is Emptyn";
        }
	return 0;
}

a) Var is Empty
b) Var is not Empty
c) Error
d) Segmentation fault
Answer: b
Clarification: 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
Answer: a
Clarification: 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
#include
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 emptyn";
	}
	else{
		cout<<"var is not emptyn";	
	}
	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
Clarification: 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
Answer: b
Clarification: There are two ways of handling errors in any container first by using exceptions like bad_any_cast and second by returning the pointer.

  250+ TOP MCQs on Value Return and Answers

Leave a Comment

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

Scroll to Top