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
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
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?
#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
d) Error
View Answer
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
Explanation: 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<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
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.
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
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
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
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 emptyView Answer
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
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.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books
- Check C++ Books