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++ → Constructors and Destructors 8

What is the output: class A{public:~A(){cout<<'X';} A()=default; A(A&&)=default;}; A f(){return A();} A a=f(); cout<<'Y';
Y
click to copy
What is the output: class A{public:int x; A():x(0){} A(int v):x(v){}}; A arr[3]={1,2,3}; cout<<arr[0].x<<arr[2].x;
13
click to copy
What is the output: class A{public:int x=0; ~A(){x=-1;} int getAfterDtor(){return x;}}; A *p=new A; delete p; // p->getAfterDtor() would be UB
Undefined behavior
click to copy
What is the output: class A{public:int x; constexpr A(int v):x(v*v){}}; constexpr A a(4); cout<<a.x;
16
click to copy
What is the output: class A{public:int x=0,y=0; A()=default; A(int a,int b):x(a),y(b){}}; A a{3,4}; cout<<a.x<<a.y;
Compile error
click to copy
What is the output: class A{int x; public: void setX(int v){x=v;} int getX()const{return x;} A(int v=0):x(v){}}; vector<A> v(3); v[1].setX(5); cout<<v[1].getX();
5
click to copy
What is the output: class A{public:int x; A(int v):x(v){} bool operator<(const A& o)const{return x<o.x;}}; A a(3),b(5),c(1); cout<<min({a,b,c}).x;
1
click to copy
What is the output: class A{public:int x=10; A(const A& o):x(o.x-1){} A()=default;}; A a; A b=a; A c=b; A d=c; cout<<a.x<<b.x<<c.x<<d.x;
10 9 8 7
click to copy

OOP Using C++ → Inheritance 10

What is the output: class A{public:int x=1; virtual int f(){return x;}}; class B:public A{public:int x=2; int f()override{return x;}}; B b; A&r=b; cout<<r.f()<<r.x;
21
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';}}; class B:public A{public:void f()override{cout<<'B';}}; class C:public B{}; C c; c.f();
B
click to copy
What is the output: class A{public:int x=0; A(int v):x(v){} virtual void f(){cout<<x;}}; class B:public A{public:B(int v):A(v*2){}}; B b(3); b.f();
6
click to copy
What is the output: class A{public:void f(){cout<<'A';}}; class B:public A{public:using A::f; void f(int){cout<<'B';}}; B b; b.f();
A
click to copy
What is the output: class A{public:A(int){cout<<'P';} virtual void f()=0;}; class B:public A{public:B():A(5){cout<<'B';} void f()override{cout<<'F';}}; B b; b.f();
PBF
click to copy
What is the output: class A{public:int x=1; virtual int g(){return x;}}; class B:public A{public:int x=2; int g()override{return x;}}; A *p=new B; cout<<p->g()<<((B*)p)->x;
22
click to copy
What is the output: class A{public:virtual ~A(){cout<<'A';}}; class B:public A{public:~B()override{cout<<'B';}}; class C:public B{public:~C()override{cout<<'C';}}; A*p=new C; delete p;
CBA
click to copy
What is the output: class A{public:int x=0;}; class B:public A{public:int x=1;}; class C:public A{public:int x=2;}; class D:public B,public C{}; D d; cout<<d.B::x<<d.C::x;
12
click to copy
What is the output: struct A{virtual void f(){cout<<1;}}; struct B:A{void f()override{cout<<2;}}; struct C:A{void f()override{cout<<3;}}; struct D:B,C{}; D d; d.B::f(); d.C::f();
23
click to copy
What is the output: class A{protected:int x=5;}; class B:public A{public: int getX(){return x;}}; class C:public A{public: int getX(){return x+1;}}; B b; C c; cout<<b.getX()<<c.getX();
56
click to copy

OOP Using C++ → Polymorphism 10

What is the output: class A{public:virtual int f(int x){return x;}}; class B:public A{public:int f(int x)override{return x*2;}}; class C:public B{public:int f(int x)override{return x*3;}}; A *p=new C; cout<<p->f(5);
15
click to copy
What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<'B';}}; function<void()> fn=[]{unique_ptr<A> p=make_unique<B>(); p->f();}; fn();
B
click to copy
What is the output: class A{public:virtual int f()const{return 1;} virtual ~A()=default;}; class B:public A{public:int f()const override{return 2;}}; vector<unique_ptr<A>> v; v.push_back(make_unique<A>()); v.push_back(make_unique<B>()); int s=0; for(auto&p:v)s+=p->f(); cout<<s;
3
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; A::f();}}; B b; b.f();
21
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} void g(){cout<<'G'; f();}}; class B:public A{public:void f()override{cout<<'B';}}; A *p=new B; p->g();
GB
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;}}; A *arr[]={new A,new B,new A,new B}; for(auto p:arr) p->f();
1212
click to copy
What is the output: class A{public:int x; A(int v):x(v){} virtual bool eq(const A& o)const{return x==o.x;}}; class B:public A{public:B(int v):A(v){} bool eq(const A& o)const override{return A::eq(o);}}; A *p=new B(5); A *q=new B(5); cout<<p->eq(*q);
1
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} virtual void g(){cout<<'G';} void h(){f(); g();}}; class B:public A{public:void f()override{cout<<'B';}}; B b; b.h();
BG
click to copy
What is the output: class A{public:virtual int f(int x=10){return x;}}; class B:public A{public:int f(int x=20)override{return x*2;}}; A *p=new B; cout<<p->f();
20
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;}}; class C:public A{public:void f()override{cout<<3;}}; auto v=vector<A*>{new B,new C,new B}; int s=0; for(auto p:v)p->f(); cout<<' '<<v.size();
2 3 2 3
click to copy

OOP Using C++ → Encapsulation 10

What is the output: class A{int x=0; public: int get()const{return x;} void f(A& o){o.x=x+1;}}; A a,b; a.f(b); cout<<b.get();
1
click to copy
What is the output: class A{int x=0; public: void set(int v){if(v>=0&&v<=100) x=v;} int get()const{return x;}}; A a; a.set(200); a.set(50); cout<<a.get();
50
click to copy
What is the output: class A{int x=5; public: A& operator=(int v){x=v;return *this;} operator int(){return x;}}; A a; a=10; cout<<(int)a;
10
click to copy
What is the output: class A{vector<int> v; public: void add(int x){v.push_back(x);} int sum()const{return accumulate(v.begin(),v.end(),0);}}; A a; a.add(1);a.add(2);a.add(3); cout<<a.sum();
6
click to copy
What is the output: class A{int x; public: A(int v):x(v){} int operator-()const{return -x;} int operator+()const{return +x;}}; A a(5); cout<<-a<<+a;
-55
click to copy
What is the output: class A{int n=0; public: A& operator+=(int v){n+=v;return *this;} A& operator-=(int v){n-=v;return *this;} int get()const{return n;}}; A a; a+=5; a+=3; a-=2; cout<<a.get();
6
click to copy
What is the output: class A{int x; public: A(int v):x(v){} A operator*(int n)const{return A(x*n);} friend A operator*(int n,const A& a){return a*n;} int get()const{return x;}}; A a(3); cout<<(a*4).get()<<(5*a).get();
1215
click to copy
What is the output: class Matrix{int d[2][2]={{1,2},{3,4}}; public: int& at(int i,int j){return d[i][j];} int get(int i,int j)const{return d[i][j];}}; Matrix m; m.at(0,1)=10; cout<<m.get(0,1);
10
click to copy
What is the output: class A{string s; public: A(string t):s(t){} string get()const{return s;} A operator+(const A& o)const{return A(s+o.s);}}; A a("hi"),b("there"); cout<<(a+b).get();
hithere
click to copy
What is the output: class A{int x=0; public: void inc(){x++;} int get()const{return x;} A operator+(const A& o)const{A r; r.x=x+o.x; return r;}}; A a,b; a.inc();a.inc(); b.inc(); cout<<(a+b).get();
3
click to copy

OOP Using C++ → Abstraction 2

What is the output: class A{public:virtual int f()const=0;}; class B:public A{int x; public:B(int v):x(v){} int f()const override{return x;}}; auto sum=[](vector<A*>& v){int s=0;for(auto p:v)s+=p->f();return s;}; vector<A*>v={new B(1),new B(2),new B(3)}; cout<<sum(v);
6
click to copy
What is the output: template<typename T> class Functor{T v; public:Functor(T x):v(x){} T operator()(T x){return v*x;}}; Functor<int> f(3); cout<<f(5)<<f(7);
1521
click to copy