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++ → Encapsulation 22

What is the output: class A{int x=0; public:int get()const{return x;} void f(){x++;} void g(){f();f();f();}}; A a; a.g(); cout<<a.get();
3
click to copy
What is the output: class A{int n; public:A(int v):n(v){} auto begin(){return &n;} auto end(){return &n+1;}}; A a(42); for(int x:a) cout<<x;
42
click to copy
What is the output: class A{int x; public:A(int v):x(v){} int operator()(int y)const{return x*y;} int operator()(int y,int z)const{return x*(y+z);}}; A a(3); cout<<a(4)<<a(4,5);
1227
click to copy
What is the output: class A{int x=0; public:A& inc(){x++;return *this;} A& mul(int n){x*=n;return *this;} int val()const{return x;}}; A a; cout<<a.inc().inc().inc().mul(3).val();
9
click to copy
What is the output: class A{int x; public:explicit A(int v):x(v){} A operator-()const{return A(-x);} int get()const{return x;}}; A a(5); A b=-a; cout<<b.get();
-5
click to copy
What is the output: class Stack{vector<int>s; public:void push(int x){s.push_back(x);} int top()const{return s.back();} void pop(){s.pop_back();} bool empty()const{return s.empty();} int size()const{return s.size();}}; Stack st; st.push(1);st.push(2);st.push(3); cout<<st.top()<<st.size(); st.pop(); cout<<st.top();
332
click to copy
What is the output: class A{int x=0; public:void set(int v){x=v%10;} int get()const{return x;}}; A a; a.set(42); cout<<a.get();
2
click to copy
What is the output: class A{int x; public:A(int v):x(v){} int get()const{return x;} bool operator>(const A& o)const{return x>o.x;}}; vector<A>v={{5},{3},{8},{1}}; cout<<(*max_element(v.begin(),v.end())).get();
8
click to copy
What is the output: class A{int x=0; public:A& operator<<(int v){x+=v;return *this;} int get()const{return x;}}; A a; a<<1<<2<<3<<4<<5; cout<<a.get();
15
click to copy
What is the output: class A{int x; public:explicit A(int v):x(v){} int get()const{return x;} A& operator++(){++x;return *this;} A operator++(int){auto t=*this;++*this;return t;}}; A a(3); cout<<(a++).get()<<a.get()<<(++a).get();
345
click to copy
What is the output: class A{string name_; public:A(string n):name_(n){} const string& name()const{return name_;} bool operator==(const A& o)const{return name_==o.name_;}}; A a("Bob"),b("Bob"),c("Alice"); cout<<(a==b)<<(a==c);
10
click to copy
What is the output: class A{int n=0; public:void add(int x){n+=x;} void sub(int x){n-=x;} void neg(){n=-n;} int get()const{return n;}}; A a; a.add(5);a.add(3);a.neg();a.sub(2); cout<<a.get();
-10
click to copy
What is the output: class A{int x,y; public:A(int a,int b):x(a),y(b){} A scale(int n)const{return {x*n,y*n};} int mag2()const{return x*x+y*y;}}; A a(3,4); cout<<a.scale(2).mag2();
100
click to copy
What is the output: class A{int x=5; public:const int& cref()const{return x;} int& ref(){return x;}}; A a; a.ref()=10; cout<<a.cref();
10
click to copy
What is the output: class Acc{double s=0,n=0; public:void add(double x){s+=x;n++;} double mean()const{return n?s/n:0;}}; Acc acc; acc.add(2);acc.add(4);acc.add(6); cout<<acc.mean();
4
click to copy
What is the output: class A{map<string,int>m; public:int& at(const string& k){return m[k];} int get(const string& k)const{auto it=m.find(k);return it!=m.end()?it->second:0;}}; A a; a.at("x")=5; cout<<a.get("x")<<a.get("y");
50
click to copy
What is the output: class A{int x=0; public:A& operator+=(const A& o){x+=o.x;return *this;} int get()const{return x;} A(int v=0):x(v){}}; A a(3),b(4); a+=b; cout<<a.get()<<b.get();
74
click to copy
What is the output: class A{int x=0; public:operator bool()const{return x!=0;} void set(int v){x=v;}}; A a; cout<<(bool)a; a.set(5); cout<<(bool)a;
01
click to copy
What is the output: class A{int x; public:A(int v):x(v){} A operator%(const A& o)const{return {x%o.x};} int get()const{return x;}}; A a(10),b(3); cout<<(a%b).get();
1
click to copy
What is the output: class A{int v=0; public:int peek()const{return v;} void push(int x){v=x;} int pop(){int t=v;v=0;return t;}}; A a; a.push(42); cout<<a.pop()<<a.peek();
420
click to copy
What is the output: class A{int x; public:A(int v):x(v){} A twice()const{return {x*2};} A inc()const{return {x+1};} int get()const{return x;}}; A a(3); cout<<a.twice().inc().twice().get();
14
click to copy
What is the output: class A{int x=0; public:void f(){x+=2;} void g(){f();f();} void h(){g();g();} int get()const{return x;}}; A a; a.h(); cout<<a.get();
8
click to copy

OOP Using C++ → Abstraction 18

What is the output: class A{public:virtual int calc(int x)const=0;}; class SqA:public A{public:int calc(int x)const override{return x*x;}}; class CuA:public A{public:int calc(int x)const override{return x*x*x;}}; auto eval=[](const A& a,int x){return a.calc(x);}; SqA sq; CuA cu; cout<<eval(sq,3)<<eval(cu,3);
927
click to copy
What is the output: template<typename T> T clampT(T v,T lo,T hi){return v<lo?lo:v>hi?hi:v;} cout<<clampT(5,1,10)<<clampT(-5,0,10)<<clampT(15,0,10);
1510
click to copy
What is the output: class Filter{public:virtual bool pass(int x)const=0;}; class Even:public Filter{public:bool pass(int x)const override{return x%2==0;}}; class Pos:public Filter{public:bool pass(int x)const override{return x>0;}}; auto apply=[](vector<int>v,const Filter& f){vector<int>r;for(int x:v)if(f.pass(x))r.push_back(x);return r;};Even e; auto r=apply({-2,1,2,3,4},e); cout<<r.size();
2
click to copy
What is the output: template<typename T> class Box{T v; public:Box(T x):v(x){} T get()const{return v;} Box map(function<T(T)>f)const{return Box(f(v));}}; Box<int>b(5); cout<<b.map([](int x){return x*2;}).map([](int x){return x+1;}).get();
11
click to copy
What is the output: class Expr{public:virtual int eval()const=0; virtual ~Expr()=default;}; class Num:public Expr{int v;public:Num(int x):v(x){} int eval()const override{return v;}}; class Add:public Expr{Expr*l,*r;public:Add(Expr*a,Expr*b):l(a),r(b){} int eval()const override{return l->eval()+r->eval();}}; Expr*e=new Add(new Num(3),new Add(new Num(4),new Num(5))); cout<<e->eval();
12
click to copy
What is the output: template<typename T> T fold(vector<T>v,T init,function<T(T,T)>f){for(T x:v)init=f(init,x);return init;} cout<<fold<int>({1,2,3,4,5},0,[](int a,int b){return a+b;})<<fold<int>({1,2,3,4,5},1,[](int a,int b){return a*b;});
15120
click to copy
What is the output: class Predicate{public:virtual bool operator()(int x)const=0;}; class GT{int t; public:GT(int v):t(v){} bool operator()(int x)const{return x>t;}}; auto count=[](vector<int>v,function<bool(int)>p){return count_if(v.begin(),v.end(),p);}; GT g(3); cout<<count({1,2,3,4,5},g);
2
click to copy
What is the output: template<typename T,typename F> vector<T> mapV(vector<T>v,F f){for(T&x:v)x=f(x);return v;} auto r=mapV<int>({1,2,3,4,5},[](int x){return x*x;}); cout<<r[2];
9
click to copy
What is the output: class Transform{public:virtual int apply(int x)const=0; virtual ~Transform()=default;}; class Scale:public Transform{int f;public:Scale(int v):f(v){} int apply(int x)const override{return x*f;}}; class Shift:public Transform{int s;public:Shift(int v):s(v){} int apply(int x)const override{return x+s;}}; vector<Transform*>ts={new Scale(2),new Shift(3)}; int v=5; for(auto t:ts)v=t->apply(v); cout<<v;
13
click to copy
What is the output: template<typename T> class Optional{bool has;T v;public:Optional():has(false){} Optional(T x):has(true),v(x){} bool hasValue()const{return has;} T value()const{return v;} T valueOr(T d)const{return has?v:d;}}; Optional<int>a(5),b; cout<<a.valueOr(0)<<b.valueOr(99);
599
click to copy
What is the output: class Visitor{public:virtual void visitInt(int x)=0; virtual void visitStr(const string& s)=0;}; class Print:public Visitor{public:void visitInt(int x)override{cout<<x;} void visitStr(const string& s)override{cout<<s;}}; Print p; p.visitInt(42); p.visitStr("hi");
42hi
click to copy
What is the output: template<int N> struct IsPrime{static const bool v=[](){for(int i=2;i*i<=N;i++)if(N%i==0)return false;return N>1;}();}; cout<<IsPrime<7>::v<<IsPrime<9>::v;
10
click to copy
What is the output: class A{public:virtual string repr()const=0;}; class Int:public A{int v;public:Int(int x):v(x){} string repr()const override{return to_string(v);}}; class Str:public A{string s;public:Str(string t):s(t){} string repr()const override{return '"'+s+'"';}}; vector<A*>v={new Int(42),new Str("hi")}; for(auto p:v)cout<<p->repr()<<' ';
42 "hi"
click to copy
What is the output: template<typename T> T identity_(T x){return x;} template<typename F,typename T> T applyFn(F f,T x){return f(x);} cout<<applyFn(identity_<int>,5);
5
click to copy
What is the output: class Lens{int A::*ptr; public:Lens(int A::*p):ptr(p){} int get(const A& a)const{return a.*ptr;} void set(A& a,int v)const{a.*ptr=v;}}; struct A{int x=0,y=0;}; Lens lx(&A::x); A a; lx.set(a,5); cout<<lx.get(a);
5
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 make=[]()->unique_ptr<A>{return make_unique<B>();}; auto p=make(); p->f();
B
click to copy
What is the output: template<typename...Ts> auto sumTuple(tuple<Ts...>t){return apply([](auto...xs){return (xs+...);},t);} cout<<sumTuple(make_tuple(1,2,3,4,5));
15
click to copy
What is the output: class Cache{map<int,int>m; function<int(int)>f; public:Cache(function<int(int)>fn):f(fn){} int get(int x){if(!m.count(x))m[x]=f(x);return m[x];}}; Cache sq([](int x){return x*x;}); cout<<sq.get(5)<<sq.get(5)<<sq.get(3);
25259
click to copy