MCQs
None.
Because the dynamic allocations can change at any time, So it is best to include in try block.
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
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
class shape
5.
{
6.
public:
7.
virtual void myvirtualfunc() const {}
8.
};
9.
class mytriangle: public shape
10.
{
11.
public:
12.
virtual void myvirtualfunc() const
13.
{
14.
};
15.
};
16.
int main()
17.
{
18.
shape shape_instance;
19.
shape &ref_shape = shape_instance;
20.
try
21.
{
22.
mytriangle &ref_mytriangle = dynamic_cast(ref_shape);
23.
}
24.
catch (bad_cast)
25.
{
26.
cout
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
As we are dividing the zero by three, it is returning 0.
Output:
$ g++ std2.cpp
$ a.out
0
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
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.
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
As these are standard exceptions, they need to be defined in the standard block, So it is defined
under namespace std.
Answer:a
Explanation:None.