Quick Revision

GK One-Line Question & Answer

15541+ short questions with short answers, covering every category and sub-category on the site — no long articles to scroll through. Good for a fast recap before an exam, or a few minutes of daily practice.

OOP Using C++ → Abstraction 1

What is the output: template<typename T> void print(T v){cout<<v;} print(42); print('A'); print(3.14);
42A3.14
click to copy

OOP Using C++ → Exception Handling 30

What is the output: try{throw 42;}catch(int i){cout<<i;}
42
click to copy
What happens when no catch handler matches a thrown exception?
std::terminate is called
click to copy
What is the output: try{try{throw 1.0;}catch(int){cout<<'I';}}catch(double){cout<<'D';}
D
click to copy
What is 'exception safety guarantee'?
Level of guarantee a function provides about state when exception thrown
click to copy
What is the output: struct A{~A(){cout<<'D';}}; try{A a; throw 1;}catch(int){cout<<'C';}
DC
click to copy
What is the output: try{throw;}catch(int){cout<<'I';}catch(...){cout<<'A';}
std::terminate
click to copy
What is 'catch-all' handler in C++?
catch(...) — catches any exception type
click to copy
What is the output: void f(){throw string("Error");} try{f();}catch(const string& s){cout<<s;}
Error
click to copy
What is 'stack unwinding'?
Destructors called for local objects as exception propagates
click to copy
What is the output: class E{public: string msg; E(string s):msg(s){}}; try{throw E("Oops");}catch(E& e){cout<<e.msg;}
Oops
click to copy
What is the output: try{throw 1;}catch(int& i){i=99; cout<<i;}
99
click to copy
What is 'exception specification' in C++?
noexcept or noexcept(expr) declaring function won't throw
click to copy
What is the output: auto f=[]()noexcept{throw 1;}; try{f();}catch(...){cout<<'C';}
std::terminate called
click to copy
What is 'std::exception_ptr'?
Pointer-like type that can store and rethrow exceptions
click to copy
What is the output: try{int*p=nullptr; *p=1;}catch(runtime_error& e){cout<<'R';}catch(...){cout<<'A';}
Undefined behavior (no exception)
click to copy
What is the output: try{throw 1;}catch(long l){cout<<'L';}catch(int i){cout<<'I';}
I
click to copy
What is 'RETHROW' in C++?
throw; inside catch block re-throws current exception
click to copy
What is the output: struct Base{virtual ~Base()=default;}; struct Der:Base{}; try{throw Der();}catch(Base& b){cout<<'B';}catch(Der& d){cout<<'D';}
B
click to copy
What is 'std::terminate' called in situations besides uncaught exception?
noexcept function throws, or exception in destructor during unwinding
click to copy
What is the output: try{throw;}catch(...){cout<<'X';}
std::terminate
click to copy
What is the output: try{try{throw 1;}catch(int i){throw i+1;}}catch(int i){cout<<i;}
2
click to copy
What is 'function-try-block'?
Try-catch wrapping entire function including member initializer list
click to copy
What is the output: void f() noexcept(false){throw 1;} void g() noexcept{f();} try{g();}catch(...){cout<<'C';}
std::terminate
click to copy
What is the output: try{throw std::runtime_error("fail");}catch(std::exception& e){cout<<e.what();}
fail
click to copy
What is 'exception neutral' code?
Code that doesn't catch exceptions but allows them to propagate
click to copy
What is the output: struct A{A(){throw 1;} ~A(){cout<<'D';}}; try{A a;}catch(int){cout<<'C';}
C
click to copy
What is 'nested exception' in C++?
std::nested_exception stores current exception while throwing another
click to copy
What is the output: try{vector<int> v; v.at(5);}catch(out_of_range& e){cout<<'O';}catch(...){cout<<'X';}
O
click to copy
What is the output: int x=0; try{x=1; throw 1; x=2;}catch(int){x=3;} cout<<x;
3
click to copy
What is the output: try{throw 1;}catch(int i){cout<<i;}catch(int i){cout<<i*2;}
Compile error
click to copy

OOP Using C++ → Templates 9

What is the output: template<typename T> T add(T a,T b){return a+b;} cout<<add(3,4)<<' '<<add(1.5,2.5);
7 4.0
click to copy
What is 'template argument deduction'?
Compiler deduces template type from function call arguments
click to copy
What is the output: template<int N> int f(){return N*N;} cout<<f<5>();
25
click to copy
What is 'template specialization'?
Providing specific implementation for particular template argument
click to copy
What is the output: template<typename T> struct Box{T val; Box(T v):val(v){}}; Box b(42); cout<<b.val;
42
click to copy
What is 'variadic template'?
Template with variable number of parameters using ...
click to copy
What is the output: template<typename T,typename U> auto add(T a,U b)->decltype(a+b){return a+b;} cout<<add(1,2.5);
3.5
click to copy
What is 'SFINAE' and enable_if?
Conditionally enabling function template overloads based on type traits
click to copy
What is the output: template<typename T> void f(T){cout<<'G';} template<> void f<int>(int){cout<<'S';} f(3); f(3.0);
SG
click to copy