1. Which header file is used to declare the standard exception?
A. #include<exception>
B. #include<except>
C. #include<error>
D. #include<exce>
Answer: A
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
Answer: A
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
Answer: A
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
Answer: A
Explanation:
: We are checking the type id of char and float as they are not equal, We are printing c.
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
Answer: D
Explanation:
: Variable will be allocated depends on the available space in the memory, If there is no space means, It will throw an exception.
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
Answer: A
Explanation:
: As we are dividing the zero by three, it is returning 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(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
Answer: C
Explanation:
: As we are not able to allocate the values by using dynamic cast, So it is arising an 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
Answer: B
Explanation:
: As there is no object in the class, It is arising an exception in the program.
9. Which of the following is best to include under try block?
A. static values
B. const values
C. dynamic allocations
D. default values
Answer: C
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
Answer: C
Explanation:
: Both Memory allocation errors & I/O errors are the predefined exceptions in c++.