This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Standard Exceptions”.
1. Which header file is used to declare the standard exception?
a) #include<exception>
b) #include<except>
c) #include<error>
d) #include<exce>
View Answer
Explanation: #include<exception> is used to declare the standard exception.
2. Where are standard exception classes grouped?
a) namespace std
b) error
c) catch
d) final
View Answer
Explanation: As these are standard exceptions, they need to be defined in the standard block, So it is defined under namespace std.
3. How many types of standard exception are there in c++?
a) 9
b) 5
c) 6
d) 7
View Answer
Explanation: There are nine standard exceptions in c++. They are bad_alloc, bad_cast, bad_exception, bad_function_call, bad_typeid, bad_weak_ptr, ios_base::failure, logic_error and runtime_error.
4. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
class myexc: public exception
{
virtual const char* what() const throw()
{
return "My exception";
}
} myex;
int main ()
{
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
a) My
b) My exception
c) No exception
d) exception
View Answer
Explanation: This is a type of exception arising in the class. We can call this
also as a standard exception.
Output:
$ g++ std.cpp $ a.out My exception
5. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
int main ()
{
try
{
int* myarray= new int[1000];
cout << "Allocated";
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}
a) Allocated
b) Standard exception:
c) bad_alloc
d) Depends on memory
View Answer
Explanation: Variable will be allocated depends on the available space in the memory, If there is no space means, It will throw an exception.
Output:
$ g++ std1.cpp $ a.out Allocated
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char* ptr;
unsigned long int a = (size_t(0) / 3);
cout << a << endl;
try
{
ptr = new char[size_t(0) / 3];
delete[ ] ptr;
}
catch(bad_alloc &thebadallocation)
{
cout << thebadallocation.what() << endl;
};
return 0;
}
a) 0
b) 2
c) bad_alloc
d) depends on compiler
View Answer
Explanation: As we are dividing the zero by three, it is returning 0.
Output:
$ g++ std2.cpp $ a.out 0
7. What will be the output of the following C++ code?
#include <typeinfo>
#include <iostream>
using namespace std;
class shape
{
public:
virtual void myvirtualfunc() const {}
};
class mytriangle: public shape
{
public:
virtual void myvirtualfunc() const
{
};
};
int main()
{
shape shape_instance;
shape &ref_shape = shape_instance;
try
{
mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_shape);
}
catch (bad_cast)
{
cout << "Caught: bad_cast exception\n";
}
return 0;
}
a) Caught standard exception
b) No exception arises
c) Caught: bad_cast exception
d) Caught: cast
View Answer
Explanation: As we are not able to allocate the values by using dynamic cast,
So it is arising an exception.
Output:
$ g++ std3.cpp $ a.out Caught: bad_cast exception
8. What will be the output of the following C++ code?
#include <typeinfo>
#include <iostream>
using namespace std;
class Test
{
public:
Test();
virtual ~Test();
};
int main()
{
Test *ptrvar = NULL;
try
{
cout << typeid(*ptrvar).name() << endl;
}
catch (bad_typeid)
{
cout << "The object is null" << endl;
}
return 0;
}
a) No exception arises
b) The object is null
c) Error
d) The object is
View Answer
Explanation: As there is no object in the class, It is arising an exception in the program.
Output:
$ g++ std4.cpp $ a.out The object is null
9. Which of the following is best to include under try block?
a) static values
b) const values
c) dynamic allocations
d) default values
View Answer
Explanation: Because the dynamic allocations can change at any time, So it is best to include in try block.
10. What are the predefined exceptions in c++?
a) Memory allocation errors
b) I/O errors
c) Both Memory allocation errors & I/O errors
d) static errors
View Answer
Explanation: Both Memory allocation errors & I/O errors are the predefined exceptions in c++.
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.
- Apply for Computer Science Internship
- Check C++ Books
- Check Programming Books
- Practice Computer Science MCQs
- Practice Programming MCQs