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++ → Exception Handling 32

What is the output: void f() noexcept{} try{f();}catch(...){cout<<'X';} cout<<'Y';
Y
click to copy
What is the output: try{throw 1;}catch(int& i){i=99;}
Nothing
click to copy
What is the output: try{throw;}catch(int){cout<<'I';}catch(...){cout<<'A';}
terminate
click to copy
What is the output: try{throw 1;}catch(int i){cout<<i; throw 2;}catch(int i){cout<<i;}
1
click to copy
What is the output: class E:public exception{public:const char* what()const noexcept override{return "E";}}; try{throw E();}catch(exception& e){cout<<e.what();}
E
click to copy
What is the output: try{new int[1000000000000];}catch(bad_alloc& e){cout<<'M';}
M
click to copy
What is the output: try{throw 1;}catch(int i){throw i+1;}catch(int i){cout<<i;}
terminate
click to copy
What is the output: int a=0; try{a=1;}finally{a=2;}
Compile error
click to copy
What is the output: try{int*p=nullptr; delete p;}catch(...){cout<<'X';} cout<<'Y';
Y
click to copy
What is the output: struct Guard{~Guard(){cout<<'G';}}; auto f=[](){Guard g; throw 1;}; try{f();}catch(int){cout<<'C';}
GC
click to copy
What is the output: try{throw 1.0f;}catch(float f){cout<<'F';}catch(double d){cout<<'D';}
F
click to copy
What is the output: int x=0; try{throw 1;}catch(int i){x=i;} cout<<x;
1
click to copy
What is the output: try{throw exception();}catch(exception& e){cout<<e.what();}
std::exception
click to copy
What is the output: class A{public:A(){cout<<'A';} ~A(){cout<<'a';}}; class B{public:B(){cout<<'B';throw 1;} ~B(){cout<<'b';}}; try{A a;B b;}catch(int){cout<<'C';}
ABaC
click to copy
What is the output: try{throw 1;}catch(const int& i){cout<<i+1;}
2
click to copy
What is the output: try{throw runtime_error("test");}catch(logic_error&){cout<<'L';}catch(runtime_error&){cout<<'R';}
R
click to copy
What is the output: try{try{throw 1;}catch(int i){cout<<'I'; if(i==1)throw;}}catch(int i){cout<<'O';}
IO
click to copy
What is the output: auto safe=[](auto f)->optional<decltype(f())>{try{return f();}catch(...){return nullopt;}}; auto r=safe([]()->int{throw 1; return 0;}); cout<<r.has_value();
0
click to copy
What is the output: try{throw 1;}catch(int){throw;}catch(...){cout<<'A';}
Propagates (terminate if uncaught)
click to copy
What is the output: struct Ex{int code;}; try{throw Ex{42};}catch(Ex& e){cout<<e.code;}
42
click to copy
What is the output: try{throw 1;}catch(int i){cout<<i*2;}catch(int i){cout<<i*3;}
Compile error
click to copy
What is the output: class A{public:~A(){cout<<'X';}}; void f(){A a;} try{f();}catch(...){} cout<<'Y';
XY
click to copy
What is the output: try{throw bad_cast();}catch(bad_cast& e){cout<<'B';}catch(exception& e){cout<<'E';}
B
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 logic_error("L");}catch(logic_error& e){cout<<e.what();}
L
click to copy
What is the output: auto f=[]()noexcept->int{return 42;}; try{cout<<f();}catch(...){cout<<'X';}
42
click to copy
What is the output: try{throw invalid_argument("bad");}catch(exception& e){cout<<e.what();}
bad
click to copy
What is the output: struct A{~A(){if(uncaught_exceptions()>0)cout<<'U'; else cout<<'N';}}; try{A a;throw 1;}catch(int){}
U
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 the output: try{int x=stoi("abc");}catch(invalid_argument&){cout<<'I';}catch(exception&){cout<<'E';}
I
click to copy
What is the output: try{cout<<1; throw 1; cout<<2;}catch(int){cout<<3;}cout<<4;
134
click to copy
What is the output: struct A{A(){cout<<'C';} ~A(){cout<<'D';}}; try{A a; A b; throw 1;}catch(int){cout<<'E';}
CCEDD
click to copy

OOP Using C++ → Templates 8

What is the output: template<typename T> T maxOf(T a,T b){return a>b?a:b;} cout<<maxOf(3,7)<<maxOf(2.5,1.5);
72.5
click to copy
What is the output: template<typename T> struct Pair{T a,b; Pair(T x,T y):a(x),b(y){} T sum(){return a+b;}}; Pair<int> p(3,4); cout<<p.sum();
7
click to copy
What is the output: template<typename T,typename U> auto add(T a,U b){return a+b;} cout<<add(1,2.5);
3.5
click to copy
What is the output: template<typename T> void print(T v){cout<<v<<' ';} print(1); print('A'); print(3.14);
1 A 3.14
click to copy
What is the output: template<int N> struct Square{static const int v=N*N;}; cout<<Square<7>::v;
49
click to copy
What is the output: template<typename T> class Stack{vector<T>v; public: void push(T x){v.push_back(x);} T top(){return v.back();} int size(){return v.size();}}; Stack<int> s; s.push(1);s.push(2);s.push(3); cout<<s.top()<<s.size();
33
click to copy
What is the output: template<typename... Ts> int count(Ts...){return sizeof...(Ts);} cout<<count(1,2,3,4,5);
5
click to copy
What is the output: template<typename T> T clamp(T v,T lo,T hi){return v<lo?lo:v>hi?hi:v;} cout<<clamp(5,1,10)<<clamp(15,1,10)<<clamp(-5,1,10);
5101
click to copy