Sample questions
Question 1Medium
Pick out the correct Answer.
- Exceptions are not suitable for critical points in code.
- Exception are suitable for critical points in code.
- Both a & b
- none of the mentioned
Answer: A. Exceptions are not suitable for critical points in code.
Explanation
If there is many number of exceptions in the program means, We have to use multiple catch statement and it is hard to keep track of the program.
Step-by-step solution
- Exceptions add many catch blocks and make flow hard to follow.
- In time-critical code they are not suitable.
- So the statement that exceptions are not suitable is correct.
- Option A.
Why the other options are wrong
- B. This is the opposite of the correct statement.
- C. Only a is correct.
- D. Option a is correct.
Tip: Use exceptions for rare errors, not for critical code paths.
Question 2Medium
What is the output of this program?
#include <iostream>
using namespace std;
void empty() throw()
{
cout << "In empty()";
}
void with_type() throw(int)
{
cout << "Will throw an int";
throw(1);
}
int main()
{
try
{
empty();
with_type();
}
catch (int)
{
cout << "Caught an int";
}
}
(Compile as C++11; dynamic exception specifications were removed in C++17.)
- In
empty() - Will throw an
int - Caught an
int - All of the mentioned
Answer: D. All of the mentioned
Explanation
It will print all three because we are calling all functions in the main().
Step-by-step solution
- empty() prints 'In empty()' and does not throw.
- with_type() prints 'Will throw an int' and then throws 1.
- The catch(int) block prints 'Caught an int', so all three printed texts appear, which is option D.
Why the other options are wrong
- A. This text is printed, but it is not the only output, so option A alone is incomplete.
- B. This text is printed, but it is not the only output, so option B alone is incomplete.
- C. This text is printed, but it is not the only output, so option C alone is incomplete.
Tip: Trace each function call in main() in order; when an exception is caught, the catch block output follows the throw.
Question 3Medium
Which handler is used to handle all types of exception?
- catch handler
- catch-all handler
- catch-none hendler
- none of the mentioned
Answer: B. catch-all handler
Explanation
A catch-all handler is used to handle all types of exceptions because it can catch any exception that is not caught by other specific handlers.
Step-by-step solution
- A catch-all handler catches every type of exception.
- It is used when no other specific handlers are present.
- This makes it suitable for handling all exceptions.
- Other options do not cover all exception types.
- Thus, the correct answer is catch-all handler.
Why the other options are wrong
- A. A catch handler only catches specific exceptions.
- C. A catch-none handler does not catch any exceptions.
Tip: Use a catch-all handler as a last resort to handle unexpected errors.
Question 4Medium
What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
void myunexpected ()
{
cout << "unexpected called\n";
throw 0;
}
void myfunction () throw (int)
{
throw 'x';
}
int main ()
{
set_unexpected (myunexpected);
try
{
myfunction();
}
catch (int)
{
cout << "caught int\n";
}
catch (...)
{
cout << "caught other exception\n";
}
return 0;
}
(Compile as C++11; dynamic exception specifications were removed in C++17.)
- caught other exception
- caught int
- unexpected called
- both caught int and unexpected called
Answer: D. both caught int and unexpected called
Explanation
myfunction throws 'x', which the throw(int) spec forbids, so myunexpected runs and prints 'unexpected called'. It throws 0, an int, which is allowed, so 'caught int' is printed as well.
Step-by-step solution
- myfunction() has the spec throw(int) but throws the char 'x', which breaks the spec.
- The runtime then calls the unexpected handler, myunexpected.
- myunexpected prints 'unexpected called' and throws 0, an int.
- An int is allowed by throw(int), so it leaves myfunction and reaches the try block.
- catch (int) matches, so 'caught int' is printed and catch (...) is skipped. Both lines print: option D.
Why the other options are wrong
- A. The int is caught by catch (int) first, so 'caught other exception' never prints.
- B. Misses the 'unexpected called' line printed by the handler before the int is thrown.
- C. Misses the 'caught int' line, because the thrown int is caught.
Tip: A wrong type thrown under a dynamic exception spec goes to unexpected(); whatever the handler throws must also match the spec.
Question 5Medium
Where does the exception are handled?
- inside the program
- outside the regular code
- both a & b
- none of the mentioned
Answer: B. outside the regular code
Explanation
Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called handlers.
Step-by-step solution
- Exceptions are raised when something unusual happens during the program.
- Control moves to special functions called handlers, which are kept apart from the normal code path.
- So exceptions are handled outside the regular code, and the correct answer is option B.
Why the other options are wrong
- A. Handlers are not part of the normal flow; they run only when an exception is thrown.
- C. Option B is correct, so both is wrong.
- D. Option B is correct, so none is wrong.
Tip: Try holds the normal code; catch holds the handler.
Question 6Medium
Identify the correct statement about throw(type).
- A function can throw any type of exceptions.
- A function can throw an exception of certain type only.
- A function cant throw any type of exception.
- none of the mentioned
Answer: B. A function can throw an exception of certain type only.
Explanation
When the throw keyword is written by itself, it is a way of asking the compiler to send the exception to another handler.
In fact, if there is no other handler written by you, the processing would be handed to the operating system. In this case, if you run the program and type a negative integer, since the program is not prepared to handle the exception itself, because of the presence of a single throw, the operating system would take over and display its own message.
This would be "abnormal program termination" on a Microsoft Windows operating system.
Step-by-step solution
- A dynamic exception specification such as throw(type) lists the exception types a function may throw.
- A function written with throw(int) can throw an exception of that certain type only.
- So the correct statement is option B.
Why the other options are wrong
- A. A function with an exception specification is not allowed to throw any type.
- C. A function with throw(type) can throw the listed type, so it is not barred from throwing.
- D. Option B is correct, so 'none of the mentioned' is wrong.
Tip: An exception specification limits which exception types a function may throw; an empty list means none.
Question 7Medium
What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
char *ptr;
ptr = new char[256];
try
{
if (x < 0)
{
throw x;
}
if (ptr == NULL)
{
throw " ptr is NULL ";
}
}
catch (...)
{
cout << "Exception occurred: exiting "<< endl;
}
return 0;
}
- -1
- ptr is NULL
- Exception occured: exiting
- none of the mentioned
Answer: C. Exception occured: exiting
Explanation
catch(...) is used to catch all types of exceptions arising in the program.
Output:
Exception occured: exiting
Step-by-step solution
- X is -1, so x < 0 is true and the code throws the int -1.
- catch(...) catches any exception type, including an int.
- The catch block prints the fixed text 'Exception occurred: exiting' and then a newline.
- The string throw is never reached, so the answer is option C; the option is spelled 'occured'.
Why the other options are wrong
- A. The value -1 is thrown and caught, but the catch block prints fixed text, not -1.
- B. The string throw is never reached because the int throw happens first.
- D. Option C matches the printed text, so none is wrong.
Tip: catch(...) catches every exception type; the first throw that is reached is the one handled.
Question 8Medium
What will happen if an exception that is thrown may causes a whole load of objects to go out of scope?
- Terminate the program
- Produce a runtime error
- It will be added to the overhead
- None of the mentioned
Answer: C. It will be added to the overhead
Explanation
If exception is occur and we does not handle that exception.
It may be chances to crash the program and it terminate abnormally.
it may be possibility to damage a hardware also.
But if we handle the situation through try catch block the programme run without any overheads.
Step-by-step solution
- The question says a thrown exception can take many objects out of scope.
- Leaving scope destroys those objects, which is extra work done during the throw.
- That extra work is the overhead that option C names.
- So the correct answer is option C, it will be added to the overhead.
Why the other options are wrong
- A. The program ends only when the exception is not caught.
- B. No runtime error is described; the question is about objects leaving scope.
- D. Option C does describe the result, so none is not right.
Tip: Exceptions unwind the stack and destroy local objects; keep code on the throw path light.
Question 9Medium
Where does the standard exception classes are grouped?
- namespace std
- error
- catch
- none of the mentioned
Answer: A. namespace std
Explanation
As these are standard exceptions, they need to be defined in the standard block, So it is defined under namespace std.
Step-by-step solution
- The C++ standard exception classes such as
std::exception are declared in the std namespace. - So the standard exception classes are grouped in namespace
std. - So the answer is
namespace std.
Why the other options are wrong
- B. Error is not the name of the group that holds the standard exceptions.
- C. Catch is a keyword for handling exceptions, not a grouping.
- D. Namespace std is correct, so none of the mentioned is wrong.
Tip: Standard library names such as exception live in namespace std.
Question 10Medium
How many parameters does the throw expression can have?
- 1
- 2
- 3
- 4
Answer: A. 1
Explanation
In c++ program, We can be able to throw only one error at a time.
Step-by-step solution
- Throw expression in C++ can only throw one value at a time.
- It is not possible to throw multiple values using a single throw expression.
- The
throw keyword is used with a single expression as its parameter. - Therefore, the throw expression can have only 1 parameter.
- Final answer: 1.
Why the other options are wrong
- C. Throwing three values is not allowed in C++.
- D. Throwing four values is not supported by the language.
Tip: Remember: Only one exception can be thrown per throw expression.
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →