C++ final exam questions and answers

By John

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?
#include
#include
using namespace std;
int main()
{
float val = 5.5;
any var(val);
cout<(c);
cout<
#include
using namespace std;
int main()
{
float val = 5.5;
any var(val);
cout<