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 9

What is the output: class A{public:virtual int area()const=0;}; class R:public A{int w,h;public:R(int a,int b):w(a),h(b){} int area()const{return w*h;}}; class C:public A{int r;public:C(int v):r(v){} int area()const{return r*r*3;}}; A*s[]={new R(3,4),new C(5)}; cout<<s[0]->area()<<s[1]->area();
1275
click to copy
What is the output: auto memoize=[](auto f){map<int,int> cache;return [=](auto self,int n)mutable->int{if(cache.count(n))return cache[n];return cache[n]=f(self,n);};};
Compile error
click to copy
What is the output: class Interface{public:virtual void exec()const=0; virtual ~Interface()=default;}; class Impl:public Interface{int v; public:Impl(int x):v(x){} void exec()const override{cout<<v*v;}}; shared_ptr<Interface> p=make_shared<Impl>(5); p->exec();
25
click to copy
What is the output: template<typename T> auto makeAdder(T n){return [n](T x){return x+n;};}; auto add5=makeAdder(5); cout<<add5(3)<<add5(10);
815
click to copy
What is the output: class Abstract{public:virtual int calc(int)const=0;}; class Double:public Abstract{public:int calc(int x)const override{return x*2;}}; class Triple:public Abstract{public:int calc(int x)const override{return x*3;}}; auto apply=[](Abstract*a,int x){return a->calc(x);}; cout<<apply(new Double,5)<<apply(new Triple,5);
1015
click to copy
What is the output: class EventBus{map<string,vector<function<void()>>> handlers; public: void on(string e,function<void()>f){handlers[e].push_back(f);} void emit(string e){for(auto&f:handlers[e])f();}}; EventBus bus; int x=0; bus.on("inc",[&]{x++;}); bus.on("inc",[&]{x+=2;}); bus.emit("inc"); cout<<x;
3
click to copy
What is the output: template<typename T,typename F> T foldRight(vector<T>v,T init,F f){for(int i=v.size()-1;i>=0;i (1, 6, 83, 13, 4, 'What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<'B';}}; auto clone=[](A*p)->A*{return new B(*dynamic_cast<B*>(p));}; auto p=new B; auto q=clone(p); q->f();
B
click to copy
What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<'B';}}; auto clone=[](A*p)->A*{return new B(*dynamic_cast<B*>(p));}; auto p=new B; auto q=clone(p); q->f();
B
click to copy
What is the output: class Lazy{mutable optional<int> cache; function<int()>fn; public:Lazy(function<int()>f):fn(f){} int get()const{if(!cache) cache=fn(); return *cache;}}; Lazy l([]()->int{cout<<'C';return 42;}); cout<<l.get()<<l.get();
C4242
click to copy

OOP Using C++ → Exception Handling 15

What is the output: try{string s=string(size_t(-1),'x');}catch(bad_alloc&){cout<<'M';}catch(length_error&){cout<<'L';}
M
click to copy
What is the output: try{stod("3.14");}catch(invalid_argument&){cout<<'I';}catch(...){cout<<'X';} cout<<stod("3.14");
3.14
click to copy
What is the output: auto safe_div=[](int a,int b)->optional<int>{if(b==0) return nullopt; return a/b;}; auto r=safe_div(10,3); cout<<(r?*r:-1);
3
click to copy
What is the output: try{throw 1;}catch(int i){cout<<i; try{throw 2;}catch(int j){cout<<j;} cout<<i;}
121
click to copy
What is the output: class Ex{int c; public:Ex(int v):c(v){} int code()const{return c;}}; try{throw Ex(42);}catch(Ex& e){cout<<e.code();}
42
click to copy
What is the output: int x=0; try{x=1;if(x) throw 'E';x=2;}catch(char c){cout<<c;} cout<<x;
E1
click to copy
What is the output: struct RAII{int& r;RAII(int& x):r(x){r=1;} ~RAII(){r=0;}}; int x=0; try{RAII g(x); throw 1;}catch(int){cout<<x;}
0
click to copy
What is the output: try{try{throw runtime_error("err");}catch(logic_error&){cout<<'L';}}catch(runtime_error&e){cout<<e.what();}
err
click to copy
What is the output: auto f=[]()->expected<int,string>{return 42;}; auto g=[]()->expected<int,string>{return unexpected("fail");}; cout<<f().value()<<(g().has_value()?1:0);
420
click to copy
What is the output: int n=0; try{for(int i=0;i<5;i++){n++; if(i==2) throw i;}}catch(int i){cout<<n<<i;}
33
click to copy
What is the output: class A{public:~A()noexcept(false){throw 1;}}; try{A a;}catch(...){cout<<'X';}
std::terminate
click to copy
What is the output: void f()noexcept{cout<<1;} void g(){try{f();}catch(...){cout<<2;}} g();
1
click to copy
What is the output: try{int x=stoi("2147483648");}catch(out_of_range&){cout<<'O';}
O
click to copy
What is the output: try{throw 1;}catch(int i){cout<<i; throw i+1;}catch(int i){cout<<i;}
std::terminate
click to copy
What is the output: auto ex=make_exception_ptr(runtime_error("err")); try{rethrow_exception(ex);}catch(runtime_error& e){cout<<e.what();}
err
click to copy

OOP Using C++ → Templates 16

What is the output: template<typename T> T sq(T x){return x*x;} template<> string sq(string s){return s+s;} cout<<sq(3)<<sq(string("ab"));
9abab
click to copy
What is the output: template<typename T> struct Opt{bool h;T v; Opt():h(false){} Opt(T x):h(true),v(x){} T or_(T d){return h?v:d;}}; Opt<int> a(5),b; cout<<a.or_(0)<<b.or_(99);
599
click to copy
What is the output: template<typename T> T myAbs(T x){return x<0?-x:x;} cout<<myAbs(-5)<<myAbs(-3.14);
53.14
click to copy
What is the output: template<typename T,typename U> bool same(T a,U b){return is_same_v<T,U>&&a==b;} cout<<same(5,5)<<same(5,5.0);
10
click to copy
What is the output: template<int N> struct Bits{static const int v=1<<N;}; cout<<Bits<0>::v<<Bits<3>::v<<Bits<7>::v;
18128
click to copy
What is the output: template<typename T> struct Neg{using type=T;}; template<> struct Neg<int>{using type=unsigned;}; Neg<int>::type x=-1; cout<<x;
4294967295
click to copy
What is the output: template<typename...Ts> struct Count{static const int v=sizeof...(Ts);}; cout<<Count<int,double,char>::v;
3
click to copy
What is the output: template<typename T> T mid(T a,T b,T c){T s[]={a,b,c};sort(s,s+3);return s[1];}cout<<mid(3,7,5);
5
click to copy
What is the output: template<bool B> struct YN{static const char v='N';}; template<> struct YN<true>{static const char v='Y';}; cout<<YN<(2>1)>::v;
Y
click to copy
What is the output: template<typename T> struct Zero{static constexpr T v=T{};}; cout<<Zero<int>::v<<Zero<double>::v<<Zero<bool>::v;
000
click to copy
What is the output: template<typename T,T V> struct Constant{static constexpr T value=V;}; using Five=Constant<int,5>; cout<<Five::value;
5
click to copy
What is the output: template<typename T> T dot(vector<T>a,vector<T>b){T s=T{};for(int i=0;i<a.size();i++)s+=a[i]*b[i];return s;} cout<<dot<int>({1,2,3},{4,5,6});
32
click to copy
What is the output: template<typename T> struct IsSame{template<typename U> static bool f(){return false;} template<> static bool f<T>(){return true;}}; cout<<IsSame<int>::f<int>()<<IsSame<int>::f<double>();
Compile error
click to copy
What is the output: template<int A,int B> struct Add{static const int v=A+B;}; cout<<Add<10,20>::v;
30
click to copy
What is the output: template<typename T> void swap3(T& a,T& b,T& c){T t=a;a=b;b=c;c=t;} int x=1,y=2,z=3; swap3(x,y,z); cout<<x<<y<<z;
231
click to copy
What is the output: template<typename T> class Vec2{public:T x,y; Vec2(T a,T b):x(a),y(b){} T dot(Vec2 o){return x*o.x+y*o.y;}}; Vec2<int>a(1,2),b(3,4); cout<<a.dot(b);
11
click to copy