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 27

What is the output: template<typename T> class Box{T v; public: Box(T x):v(x){} T get()const{return v;}}; Box<int> a(5); Box<string> b("hi"); cout<<a.get()<<b.get();
5hi
click to copy
What is the output: class Iter{int i=0,n; public: Iter(int n):n(n){} bool hasNext(){return i<n;} int next(){return i++;}}; Iter it(3); while(it.hasNext()) cout<<it.next();
012
click to copy
What is the output: struct Visitor{void operator()(int x){cout<<'I';} void operator()(string s){cout<<'S';}}; variant<int,string> v=string("hi"); visit(Visitor{},v);
S
click to copy
What is the output: class Logger{public:virtual void log(const string& m)=0;}; class ConsoleLogger:public Logger{public:void log(const string& m)override{cout<<m;}}; Logger* l=new ConsoleLogger; l->log("OK");
OK
click to copy
What is the output: class A{public:virtual int f(int x)=0; int g(int x){return f(x)+f(x+1);}}; class B:public A{public:int f(int x)override{return x*x;}}; B b; cout<<b.g(3);
25
click to copy
What is the output: class Event{public:virtual void handle()=0;}; class Click:public Event{public:void handle()override{cout<<"Click";}}; class Key:public Event{public:void handle()override{cout<<"Key";}}; vector<Event*> events={new Click,new Key}; for(auto e:events) e->handle();
ClickKey
click to copy
What is the output: template<typename T> T identity(T x){return x;} cout<<identity(5)<<identity('A')<<identity(3.14);
5A3.14
click to copy
What is the output: class Strategy{public:virtual int exec(int a,int b)=0;}; class Add:public Strategy{public:int exec(int a,int b){return a+b;}}; class Mul:public Strategy{public:int exec(int a,int b){return a*b;}}; Strategy*s=new Add; cout<<s->exec(3,4); s=new Mul; cout<<s->exec(3,4);
712
click to copy
What is the output: class Observable{vector<function<void()>> obs; public: void subscribe(function<void()> f){obs.push_back(f);} void notify(){for(auto& f:obs) f();}}; Observable o; int x=0; o.subscribe([&]{x++;}); o.subscribe([&]{x+=2;}); o.notify(); cout<<x;
3
click to copy
What is the output: class Transformer{public:virtual int transform(int x)=0;}; class Double:public Transformer{public:int transform(int x)override{return x*2;}}; class Inc:public Transformer{public:int transform(int x)override{return x+1;}}; vector<Transformer*> ts={new Double,new Inc}; int val=5; for(auto t:ts) val=t->transform(val); cout<<val;
11
click to copy
What is the output: class Singleton{static Singleton* inst; Singleton(){} public: static Singleton* get(){if(!inst) inst=new Singleton; return inst;} int x=42;}; Singleton* Singleton::inst=nullptr; cout<<Singleton::get()->x;
42
click to copy
What is the output: class A{public:virtual void f()=0; virtual ~A()=default;}; auto factory=[](int t)->A*{struct B:A{void f(){cout<<1;}}; struct C:A{void f(){cout<<2;}}; return t?new C:new B;}; auto p=factory(0); p->f();
1
click to copy
What is the output: template<typename Container> int sumAll(const Container& c){int s=0;for(int x:c)s+=x;return s;} vector<int>v={1,2,3,4,5}; cout<<sumAll(v);
15
click to copy
What is the output: class Decorator{function<int(int)> f; public: Decorator(function<int(int)> fn):f(fn){} Decorator add(function<int(int)> g){return {[=](int x){return g(f(x));}};} int call(int x){return f(x);}}; Decorator d{[](int x){return x*2;}}; auto d2=d.add([](int x){return x+1;}); cout<<d2.call(5);
11
click to copy
What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<typeid(*this).name();}}; B b; b.f();
B
click to copy
What is the output: class A{public:virtual void f()=0;}; try{A *p=new struct B:A{void f(){}}{};delete p;}catch(...){} cout<<'X';
Compile error
click to copy
What is the output: class Memo{map<int,int>m; public: int f(int n){if(m.count(n))return m[n]; return m[n]=n<=1?n:f(n-1)+f(n-2);}}; Memo memo; cout<<memo.f(10);
55
click to copy
What is the output: class Builder{int x=0,y=0; public: Builder& setX(int v){x=v;return *this;} Builder& setY(int v){y=v;return *this;} int build(){return x+y;}}; cout<<Builder().setX(3).setY(4).build();
7
click to copy
What is the output: class A{public:virtual int f()const=0;}; class B:public A{int v; public:B(int v):v(v){} int f()const override{return v*v;}}; vector<unique_ptr<A>> v; v.push_back(make_unique<B>(3)); v.push_back(make_unique<B>(4)); int s=0; for(auto&p:v)s+=p->f(); cout<<s;
25
click to copy
What is the output: class Pipe{vector<function<int(int)>>fns; public: Pipe& add(function<int(int)>f){fns.push_back(f);return *this;} int run(int x){for(auto&f:fns)x=f(x);return x;}}; Pipe p; p.add([](int x){return x*2;}).add([](int x){return x+3;}).add([](int x){return x*x;}); cout<<p.run(5);
169
click to copy
What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<'B';}}; class C:public A{public:void f()override{cout<<'C';}}; map<string,unique_ptr<A>> m; m["b"]=make_unique<B>(); m["c"]=make_unique<C>(); for(auto&[k,v]:m) v->f();
BC
click to copy
What is 'interface segregation principle' in C++?
Split large interfaces into smaller specific ones
click to copy
What is the output: class A{public:virtual void f()=0; virtual ~A()=default;}; class B:public A{public:void f()override{cout<<'B';}}; unique_ptr<A> p(new B); p->f();
B
click to copy
What is the output: template<typename F,typename...Args> auto call(F f,Args...args){return f(args...);} cout<<call([](int a,int b,int c){return a+b+c;},1,2,3);
6
click to copy
What is the output: class FSM{int state=0; public: void trans(int t){state=(state+t)%3;} int get()const{return state;}}; FSM m; m.trans(1);m.trans(2);m.trans(1); cout<<m.get();
0
click to copy
What is the output: class Command{public:virtual void exec()=0;}; class Print:public Command{string s; public:Print(string t):s(t){} void exec(){cout<<s;}}; vector<unique_ptr<Command>> cmds; cmds.push_back(make_unique<Print>("A")); cmds.push_back(make_unique<Print>("B")); for(auto&c:cmds) c->exec();
AB
click to copy
What is the output: class A{public:virtual void f(){cout<<1;}}; class B:public A{public:void f()override{cout<<2;}}; auto wrap=[](A* p)->function<void()>{return [p]{p->f();};}; auto fn=wrap(new B); fn();
2
click to copy

OOP Using C++ → Exception Handling 13

What is the output: try{throw 5;}catch(int i){cout<<i;}
5
click to copy
What is the output: try{throw string("err");}catch(const string& s){cout<<s.size();}
3
click to copy
What is the output: struct E{int code; string msg;}; try{throw E{404,"not found"};}catch(E& e){cout<<e.code;}
404
click to copy
What is the output: try{try{throw 1;}catch(int i){cout<<i; throw;}}catch(int i){cout<<i;}
11
click to copy
What is the output: void f(){throw runtime_error("oops");} try{f();}catch(exception& e){cout<<e.what();}
oops
click to copy
What is the output: int x=0; try{x=1; if(true) throw 'E'; x=2;}catch(char c){cout<<c<<x;}
E1
click to copy
What is the output: try{throw 1.0;}catch(int){cout<<'I';}catch(double){cout<<'D';}catch(...){cout<<'A';}
D
click to copy
What is the output: class A{public:~A(){cout<<'D';}}; void f(){A a; throw 1;} try{f();}catch(int){cout<<'C';}
DC
click to copy
What is the output: try{throw;}catch(...){cout<<'X';}
std::terminate
click to copy
What is the output: auto f=[]()->int{throw 1; return 0;}; try{cout<<f();}catch(int i){cout<<i;}
1
click to copy
What is the output: try{vector<int>v; v.at(10);}catch(out_of_range&){cout<<'O';}catch(exception&){cout<<'E';}
O
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 the output: int x=0; try{x=1;} catch(...){x=2;} cout<<x;
1
click to copy