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 8

What is the output: class A{public:virtual int eval()const=0;}; class Const:public A{int v;public:Const(int x):v(x){} int eval()const override{return v;}}; class Mul:public A{A *l,*r;public:Mul(A*a,A*b):l(a),r(b){} int eval()const override{return l->eval()*r->eval();}}; A*e=new Mul(new Const(3),new Mul(new Const(4),new Const(5))); cout<<e->eval();
60
click to copy
What is the output: template<typename T> auto zip_with(vector<T>a,vector<T>b,function<T(T,T)>f){vector<T>r;for(int i=0;i<min(a.size(),b.size());i++)r.push_back(f(a[i],b[i]));return r;} auto r=zip_with<int>({1,2,3},{4,5,6},[](int a,int b){return a*b;}); cout<<r[1];
10
click to copy
What is the output: class Protocol{public:virtual void send(string)=0; virtual string recv()=0;}; class Echo:public Protocol{string last; public:void send(string s)override{last=s;} string recv()override{return last;}}; Echo e; e.send("hello"); cout<<e.recv();
hello
click to copy
What is the output: template<typename T> auto pipeline(T x){return x;} template<typename T,typename F,typename...Fs> auto pipeline(T x,F f,Fs...fs){return pipeline(f(x),fs...);} cout<<pipeline(5,[](int x){return x*2;},[](int x){return x+3;},[](int x){return x*x;});
169
click to copy
What is the output: class A{public:virtual int f()const=0; int g()const{return f()*f()+f();}}; class B:public A{public:int f()const override{return 3;}}; B b; cout<<b.g();
12
click to copy
What is the output: class I{public:virtual void exec()=0; virtual ~I()=default;}; class Greet:public I{string n;public:Greet(string s):n(s){} void exec()override{cout<<"Hi "+n;}}; queue<unique_ptr<I>>q; q.push(make_unique<Greet>("A")); q.push(make_unique<Greet>("B")); while(!q.empty()){q.front()->exec();q.pop();}
Hi AHi B
click to copy
What is the output: template<typename T> class Lazy2{mutable optional<T>cache; function<T()>fn; public:Lazy2(function<T()>f):fn(f){} T get()const{if(!cache)cache=fn();return *cache;}}; int c=0; Lazy2<int>l([&c](){return ++c;}); cout<<l.get()<<l.get()<<c;
111
click to copy
What is the output: template<typename T,typename U> auto transform_pair(pair<T,U>p,function<T(T)>f,function<U(U)>g){return make_pair(f(p.first),g(p.second));} auto r=transform_pair<int,string>({5,"hi"},[](int x){return x*2;},[](string s){return s+"!";}); cout<<r.first<<r.second;
10hi!
click to copy

OOP Using C++ → Exception Handling 32

What is the output: try{int x=5,y=0;if(!y)throw domain_error("div0");cout<<x/y;}catch(domain_error&e){cout<<e.what();}
div0
click to copy
What is the output: try{vector<int>v={1,2,3};cout<<v.at(5);}catch(out_of_range&){cout<<'O';}catch(exception&){cout<<'E';}
O
click to copy
What is the output: struct A{~A()noexcept{cout<<'D';}}; try{A a;throw 1;}catch(int){cout<<'C';}
DC
click to copy
What is the output: int f()noexcept{return 42;} try{cout<<f();}catch(...){cout<<'X';}
42
click to copy
What is the output: auto safe=[](auto f,auto... args)->optional<decltype(f(args...))>{try{return f(args...);}catch(...){return nullopt;}}; auto r=safe([](int x,int y)->int{if(!y)throw 1;return x/y;},10,0); cout<<r.has_value();
0
click to copy
What is the output: try{string s=string(1,'A'); s.at(5);}catch(out_of_range&){cout<<'O';}
O
click to copy
What is the output: int n=0; try{for(int i=0;i<5;i++){n++;if(n==3)throw n;}}catch(int x){cout<<x<<n;}
33
click to copy
What is the output: class E:public runtime_error{public:E(string s):runtime_error(s){}}; try{throw E("myerr");}catch(E& e){cout<<e.what();}
myerr
click to copy
What is the output: int x=0; try{x=1;try{x=2;throw 1;x=3;}catch(int){x=4;}} cout<<x;
4
click to copy
What is the output: void f(){try{throw 1;}catch(int i){cout<<i; throw i+10;}} try{f();}catch(int i){cout<<i;}
1 11
click to copy
What is the output: try{auto p=make_unique<int>(5); throw *p;}catch(int i){cout<<i;}
5
click to copy
What is the output: struct Guard{int& r; Guard(int& x):r(x){r=1;} ~Guard(){r=2;}}; int x=0; try{Guard g(x); throw 1;}catch(int){cout<<x;}
2
click to copy
What is the output: try{try{throw string("inner");}catch(int){cout<<'I';}}catch(string& s){cout<<s;}
inner
click to copy
What is the output: int x=5; try{if(x>0)throw x; cout<<'N';}catch(int i){cout<<i*2;}
10
click to copy
What is the output: struct E{int code; string msg;}; try{throw E{42,"bad"};}catch(E& e){cout<<e.code<<e.msg;}
42bad
click to copy
What is the output: auto divide=[](double a,double b)->expected<double,string>{if(b==0)return unexpected("zero");return a/b;}; auto r=divide(10,2); cout<<(r?r.value():-1);
5
click to copy
What is the output: try{throw overflow_error("big");}catch(overflow_error& e){cout<<e.what();}
big
click to copy
What is the output: class Ex{public:Ex()=default; Ex(const Ex&){cout<<'C';}}; try{throw Ex();}catch(Ex e){cout<<'E';}
CE
click to copy
What is the output: int x=0; auto cleanup=[&x](){x=99;}; try{cleanup(); throw 1;}catch(int){cout<<x;}
99
click to copy
What is the output: try{throw vector<int>{1,2,3};}catch(vector<int>& v){cout<<v.size()<<v[1];}
32
click to copy
What is the output: void f(int x){if(x<0)throw invalid_argument("neg"); cout<<x;} try{f(5);f(-1);}catch(invalid_argument&e){cout<<e.what();}
5neg
click to copy
What is the output: int x=0; try{x=1;throw;} catch(...){}
std::terminate
click to copy
What is the output: struct A{A(){cout<<'A';} ~A(){cout<<'a';}}; struct B{B(){cout<<'B';} ~B(){cout<<'b';}}; try{A a;B b;throw 1;}catch(int){cout<<'E';}
ABbaE
click to copy
What is the output: auto f=[]()->pair<bool,string>{try{throw runtime_error("oops");return{true,""};}catch(exception&e){return{false,e.what()};}}; auto [ok,msg]=f(); cout<<ok<<msg;
0oops
click to copy
What is the output: int n=0; try{while(true){n++;if(n>3)throw n;}}catch(int x){cout<<x;}
4
click to copy
What is the output: try{throw 3.14;}catch(double d){cout<<d;}catch(float f){cout<<f;}
3.14
click to copy
What is the output: class E{int c;public:E(int v):c(v){} int code(){return c;}}; try{throw E(7);}catch(E e){cout<<e.code();}
7
click to copy
What is the output: try{int* p=nullptr;if(!p)throw bad_alloc();}catch(bad_alloc&){cout<<'B';}
B
click to copy
What is the output: int s=0; for(int i=0;i<5;i++){try{if(i%2)throw i;s+=i;}catch(int x){s-=x;}} cout<<s;
10
click to copy
What is the output: void g(){throw 1;} void f(){try{g();}catch(int i){cout<<i*2; throw i*3;}} try{f();}catch(int i){cout<<i;}
26
click to copy
What is the output: try{throw;}catch(...){cout<<'C';}
terminate
click to copy
What is the output: class A{public:A(){} ~A()noexcept{cout<<'X';}}; void f(){A a;return;} f(); cout<<'Y';
XY
click to copy