Standard Exceptions(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option B. -> The object is nullAs 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
Question 4.
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
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
Explanation:-
Answer: Option C. -> Caught: bad_cast exceptionAs 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
Explanation:-
Answer: Option D. -> Depends on memoryVariable 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
Explanation:-
Answer: Option B. -> My exceptionThis 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