ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
Which statement correctly describes the typical cost of exceptions in C++?
- A. Throwing and catching an exception is comparatively expensive (stack unwinding, type matching), so exceptions should signal truly exceptional conditions, not routine control flow
- B. A try block imposes runtime overhead on the non-throwing path equal to an actual throw
- C. Catching an exception by value never risks any data loss
- D. Exceptions are always faster than returning an error code
Correct answer: A. Throwing and catching an exception is comparatively expensive (stack unwinding, type matching), so exceptions should signal truly exceptional conditions, not routine control flow
Modern "zero-cost" exception implementations add essentially no overhead to code that never throws, but actually throwing and unwinding is relatively slow, so exceptions are best reserved for rare, truly exceptional situations.
More OOP with C++ and Java questions
- Why can a generic method not simply write new T[10] for a type parameter T in Java?
- java void f(int x) { System.out.print("int"); } void f(Integer x) { System.out.print("Integer"); } void f(Object... x) {…
- Why does adding a brand-new virtual function to a base class not increase the size of each object in that hierarchy (assuming objects…
- Which java.util.function functional interface best fits a lambda that takes one argument of type T and returns a boolean?
- A base class method is declared final in Java. What does this guarantee about calls to it through a base-class reference, regardless of…
- cpp struct S { int a, b; S(int x) : b(x), a(b) { } }; int main() { S s(5); cout << s.a << " " << s.b; } Members a and b are declared in…