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 11

What is the output: class A{public:int x; A(int v):x(v){cout<<v;} ~A(){cout<<~x;} }; {A a(3);}
3-4
click to copy
What is the output: class A{public:int x=1; A()=default; A(const A&o):x(o.x+1){}}; A a,b(a),c(b); cout<<a.x<<b.x<<c.x;
123
click to copy
What is the output: class A{int x; public:explicit A(int v):x(v){} explicit A(double v):x((int)v){} int get()const{return x;}}; A a(3.7); cout<<a.get();
3
click to copy
What is the output: class Base{public:Base(){cout<<'B';} ~Base(){cout<<'b';}}; class Der:public Base{public:Der(){cout<<'D';} ~Der(){cout<<'d';}}; Der d;
BDdb
click to copy
What is the output: class A{public:int x; A(int v=0):x(v){} A(const A&)=default; A(A&&o)noexcept:x(move(o.x)){o.x=-1;}}; A a(5); A b(move(a)); cout<<a.x<<b.x;
-15
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(const A& o):x(o.x){}}; A f(A a){a.x*=2; return a;} A a(5); A b=f(a); cout<<a.x<<b.x;
510
click to copy
What is the output: class A{public:int x; A(int v):x(v){} void print()const{cout<<x;}}; A* p=new A(42); p->print(); delete p;
42
click to copy
What is the output: struct POD{int x,y,z;}; POD a={1,2,3}; POD b=a; b.x=10; cout<<a.x<<b.x;
110
click to copy
What is the output: class A{public:int x=0; A& inc(int n=1){x+=n;return *this;} A& dec(int n=1){x-=n;return *this;}}; A a; a.inc().inc(5).dec(2); cout<<a.x;
4
click to copy
What is the output: class A{public:int x; A(int v):x(v){} auto operator+(const A& o)const{return A{x+o.x};}}; vector<A>v={{1},{2},{3},{4},{5}}; cout<<accumulate(v.begin(),v.end(),A{0}).x;
15
click to copy
What is the output: class A{public:int x; A(int v):x(v){cout<<'A';} ~A(){cout<<'Z';}}; try{A a(1); A b(2); throw 1;}catch(int){cout<<'E';}
AAZZE
click to copy

OOP Using C++ → Inheritance 29

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; cout<<b.x<<b.A::x<<b.f();
212
click to copy
What is the output: class A{public:A(){cout<<'A';} virtual ~A(){cout<<'a';}}; class B:public A{public:B(){cout<<'B';} ~B(){cout<<'b';}}; A*p=new B; delete p;
ABba
click to copy
What is the output: class A{public:int x; A(int v):x(v){}}; class B:public A{public:int y; B(int a,int b):A(a),y(b){}}; B b(3,4); cout<<b.x*b.y;
12
click to copy
What is the output: class A{public:void f(){cout<<'A';} void g(){f();}};class B:public A{public:void f(){cout<<'B';}};B b;b.g();
A
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 B{};A*p=new C;p->f();
2
click to copy
What is the output: class A{public:int x=10; virtual int f(){return x;}}; class B:public A{public:B(){x=20;} int f()override{return x*2;}}; A*p=new B; cout<<p->f();
40
click to copy
What is the output: class A{public:virtual void f()=0; int g(){return 42;}}; class B:public A{public:void f()override{cout<<g();}}; B b;b.f();
42
click to copy
What is the output: class A{public:int x=5;}; class B:public A{}; class C:public B{}; C c; cout<<c.x;
5
click to copy
What is the output: class A{public:A(int v){cout<<v;}}; class B:public A{public:B():A(1){cout<<2;}}; class C:public B{public:C():B(){cout<<3;}}; C c;
123
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} virtual void g(){f();}}; class B:public A{public:void f()override{cout<<'B';}}; B b; b.g();
B
click to copy
What is the output: class A{public:int x=1; virtual int f(){return x;}}; class B:public A{public:int f()override{return x+1;}}; class C:public B{public:int f()override{return x+2;}}; A a; B b; C c; cout<<a.f()<<b.f()<<c.f();
123
click to copy
'What is the output: class A{public:static int n; A(){n++;} ~A(){n (1, 6, 80, 10, 4, 'What is the output: class A{public:int x; A(int v):x(v){}}; class B:virtual public A{public:B():A(1){}}; class C:virtual public A{public:C():A(2){}}; class D:public B
B()
click to copy
What is the output: class A{public:int x; A(int v):x(v){}}; class B:virtual public A{public:B():A(1){}}; class C:virtual public A{public:C():A(2){}}; class D:public B,public C{public:D():A(3),B(),C(){}}; D d; cout<<d.x;
3
click to copy
What is the output: class A{public:void f(){cout<<'A';}}; class B:private A{public:using A::f;}; B b; b.f();
A
click to copy
What is the output: class A{public:virtual void f(){cout<<1;} void g(){cout<<2;}}; class B:public A{public:void f()override{cout<<3;} void g(){cout<<4;}}; A *p=new B; p->f(); p->g(); B *q=(B*)p; q->f(); q->g();
3234
click to copy
What is the output: class A{public:int x=0; virtual void inc(){x++;}}; class B:public A{public:void inc()override{x+=2;}}; A *p=new B; p->inc(); p->inc(); cout<<p->x;
4
click to copy
What is the output: class A{public:virtual int f()const{return 1;}}; class B:public A{public:int f()const override{return 2;} int f(){return 3;}}; const A*p=new B; cout<<p->f();
2
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(const A&o)=default;}; class B:public A{public:int y; B(int a,int b):A(a),y(b){}}; B b(3,4); B c=b; cout<<c.x<<c.y;
34
click to copy
What is the output: class Shape{public:virtual double area()=0; double perimeter(){return 0;}}; class Sq:public Shape{double s; public:Sq(double v):s(v){} double area()override{return s*s;}}; Sq sq(5); cout<<sq.area();
25
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} virtual ~A()=default;}; class B:public A{public:void f()override{cout<<'B';}}; class C:public A{public:void f()override{cout<<'C';}}; auto v=vector<unique_ptr<A>>{}; v.push_back(make_unique<B>()); v.push_back(make_unique<C>()); v.push_back(make_unique<B>()); for(auto&p:v) p->f();
BCB
click to copy
What is the output: class A{protected:int x; public:A(int v):x(v){}}; class B:public A{public:B(int v):A(v){} int f(){return x*x;}}; B b(5); cout<<b.f();
25
click to copy
What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<1;}}; auto fp=&B::f; B b; (b.*fp)();
1
click to copy
What is the output: class A{public:int x=0; virtual void f(){x=1;}}; class B:public A{public:void f()override{x=2;} void g(){A::f();}}; B b; b.g(); cout<<b.x;
1
click to copy
What is the output: class A{public:int x; A(int v=0):x(v){}}; class B:public A{public:B(int v):A(v*2){}}; class C:public B{public:C(int v):B(v+1){}}; C c(2); cout<<c.x;
6
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'; A::f();}}; class C:public B{public:void f()override{cout<<'C'; B::f();}}; C c; c.f();
CBA
click to copy
What is the output: class A{public:virtual void show(){cout<<"A";}}; class B:public A{public:void show()override{cout<<"B";}}; void print(A& a){a.show();} B b; print(b);
B
click to copy
What is the output: class A{public:int n; A(int v):n(v){} virtual A* clone(){return new A(*this);}}; class B:public A{public:B(int v):A(v){} B* clone()override{return new B(*this);}}; A *p=new B(42); A *q=p->clone(); cout<<q->n;
42
click to copy
What is the output: class A{public:int x=1;}; class B:public A{public:int x=2;}; B b; A& ra=b; B& rb=b; cout<<ra.x<<rb.x;
12
click to copy
What is the output: class A{public:A(){cout<<'A';} A(const A&){cout<<'C';}};class B:public A{public:B():A(){cout<<'B';}};B b1;B b2=b1;
ABAC
click to copy