LearnFrenzy
Sign in Take a free test

Exception Handling · Exercise 1 of 4

Exception Handling Exception Handling MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on C++ exception handling: try/catch/throw, the standard exception hierarchy, catch order, rethrowing, stack unwinding, noexcept, constructors, destructors and exception safety. Pick an option to check your answer instantly.

Question 11 Medium

If a constructor throws after its members were initialised, what is destroyed?

A Nothing
B The already-constructed members and bases – but the class's own destructor does not run
C The class destructor runs
D Everything leaks
Question page & discussion
Question 12 Hard

A constructor does buf_ = new char[1024]; and then calls a function that throws. What happens to the buffer?

A It is freed by the destructor
B It leaks – the destructor does not run and a raw pointer member has no destructor of its own; use std::unique_ptr or std::vector
C It is freed automatically
D Compile error
Question page & discussion
Question 13 Medium

Which is the best fit for std::logic_error rather than std::runtime_error?

A A network timeout
B A caller passing a negative size to a function that requires a positive one
C A missing file
D The disk being full
Question page & discussion
Question 14 Easy

What is std::exception::what()?

A The exception's type name
B A virtual function returning a C-string description of the error
C The line number
D A std::string
Question page & discussion
Question 15 Medium

What does the strong exception guarantee promise?

A The function never throws
B If the operation throws, the program state is exactly as it was before (commit or roll back)
C No memory leaks only
D The exception is always caught
Question page & discussion
Question 16 Medium

What is wrong with throw new std::runtime_error("x");?

A It does not compile
B Handlers expecting std::exception& will not catch it, and someone must remember to delete the object
C It is faster
D Nothing
Question page & discussion
Question 17 Hard

What is a typical use of std::exception_ptr?

A Faster throwing
B Capturing an exception (std::current_exception) in one place – e.g. a worker thread – and rethrowing it elsewhere with std::rethrow_exception
C Catching by pointer
D Replacing noexcept
Question page & discussion
Question 18 Hard

Why use a function-try-block on a constructor: Widget() try : member(f()) { } catch (…) { }?

A To ignore member errors
B To catch exceptions thrown by member or base initialisers – the handler can log or translate, but the exception is always rethrown
C To make the constructor noexcept
D To call the destructor
Question page & discussion
Question 19 Medium

Which situation suits std::optional/std::expected better than throwing?

A Out of memory
B Looking up a username that often does not exist
C A corrupted program configuration at start-up
D A broken database connection mid-transaction
Question page & discussion
Question 20 Medium

What does the expression noexcept(f()) evaluate to?

A It calls f and catches exceptions
B A compile-time bool: true if the call f() is declared not to throw
C The result of f()
D A run-time check
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links