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 19

What is the output: class A{public:int x; A(int v):x(v){cout<<'C';} ~A(){cout<<'D';}}; auto p=make_unique<A>(5); cout<<p->x;
C5D
click to copy
What is the output: class A{public:A(){cout<<'A';}}; class B{public:B(){cout<<'B';}}; class C:public A{B b; public:C(){cout<<'C';}}; C c;
ABC
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(const A& o):x(o.x+10){}}; A a(5); A b(a); cout<<a.x<<b.x;
515
click to copy
What is the output: class A{public:int x=0; A(){} A(A&&o):x(o.x){o.x=-1;}}; A a; a.x=5; A b=move(a); cout<<a.x<<b.x;
5-1
click to copy
What is the output: class A{static int n; public: A(){n++;} ~A(){n (1, 6, 79, 9, 4, 'What is the output: class A{public:int* p; A():p(new int(5)){} A(const A& o):p(new int(*o.p)){} ~A(){delete p;}}; A a; A b=a; *b.p=10; cout<<*a.p<<*b.p;', 'Deep copy ctor?', '510', '510', '1010', '1010', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_a', 'Deep copy: b gets own int. *b.p=10 doesn't affect *a.p=5. Output: 510.
2026-05-25
click to copy
What is the output: class A{public:int* p; A():p(new int(5)){} A(const A& o):p(new int(*o.p)){} ~A(){delete p;}}; A a; A b=a; *b.p=10; cout<<*a.p<<*b.p;
510
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A& operator=(const A& o){x=o.x; return *this;}}; A a(3),b(5); a=b; cout<<a.x;
5
click to copy
What is the output: class A{public:~A()=default; A()=default;}; A a; cout<<'X';
X
click to copy
What is the output: class A{public:A(){cout<<'N';} A(int){cout<<'I';} A(double){cout<<'D';} A(const A&){cout<<'C';}}; A a=5; A b=5.0; A c=a;
IDC
click to copy
What is the output: class A{public:int x; A(initializer_list<int> il):x(*il.begin()){}}; A a={42,1,2}; cout<<a.x;
42
click to copy
What is 'deleted special member'?
=delete explicitly disables the function
click to copy
What is the output: class A{public:int x; A():x(0){} A(A&&o) noexcept:x(o.x){o.x=0;} A(const A&)=delete;}; A f(){return A();} A a=f(); cout<<a.x;
0
click to copy
What is the output: class A{public:int x=0; A(int v):x(v){} A& operator+=(const A& o){x+=o.x;return *this;}}; A a(3),b(4); a+=b; cout<<a.x;
7
click to copy
What is the output: class A{int x; public: void setX(int v){x=v;} int getX()const{return x;} A():x(100){}}; A a; cout<<a.getX(); a.setX(200); cout<<a.getX();
100200
click to copy
What is the output: class T{public: T(){cout<<'T';} T(const T&){cout<<'C';} ~T(){cout<<'D';}}; T f(T t){return t;} T a; f(a);
Depends on optimization
click to copy
What is the output: class A{public:int x; A(int v):x(v*v){}}; A a(4); cout<<a.x;
16
click to copy
What is the output: class A{public:A(){cout<<1;} A(int){cout<<2;} A(int,int){cout<<3;}}; A a; A b(1); A c(1,2);
123
click to copy
What is the output: class A{public:int x; constexpr A(int v):x(v){}}; constexpr A a(5); cout<<a.x;
5
click to copy
What is the output: class A{public:int x=0;}; class B:public A{public:B(){x=5;}}; B b; cout<<b.x;
5
click to copy

OOP Using C++ → Inheritance 21

What is the output: class A{public:void f(){cout<<'A';}}; class B:public A{public:void f(){cout<<'B';}}; B b; b.f(); b.A::f();
BA
click to copy
What is the output: class A{public:int x=1;}; class B:public A{public:int y=2;}; B b; cout<<b.x<<b.y;
12
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} virtual ~A(){}}; class B:public A{public:void f()override{cout<<'B';}}; unique_ptr<A> p=make_unique<B>(); p->f();
B
click to copy
What is the output: class A{protected:int x=10;}; class B:public A{public:void show(){cout<<x;}}; B b; b.show();
10
click to copy
What is the output: class A{public:A(int){cout<<'A';}}; class B:public A{public:B():A(5){cout<<'B';}}; B b;
AB
click to copy
What is 'virtual inheritance' use case?
Diamond: ensure single base subobject
click to copy
What is the output: class A{public:virtual int f(){return 1;}}; class B:public A{public:int f()override{return 2;}}; class C:public B{public:int f()override{return 3;}}; A*p=new C; cout<<p->f();
3
click to copy
What is the output: class A{public:void f(){cout<<1;}}; class B:public A{public:void f(){cout<<2;} void g(){A::f(); f();}}; B b; b.g();
12
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;
34
click to copy
What is the output: class A{public:static void f(){cout<<'S';}}; class B:public A{}; B::f();
S
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';}}; class B:public A{public:void f()final{cout<<'B';}}; B b; b.f();
B
click to copy
What is 'covariant return type' example?
Base:virtual A* f(); Derived:B* f() where B derives A
click to copy
What is the output: struct A{int x=1;}; struct B:A{int y=2;}; struct C:B{int z=3;}; C c; cout<<c.x<<c.y<<c.z;
123
click to copy
What is the output: class A{public:virtual void f()=0; void g(){f();}}; class B:public A{public:void f(){cout<<'B';}}; B b; b.g();
B
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;}}; A *p=new B; p->f(); cout<<p->x;
2
click to copy
What is the output: class A{public:int x=5; virtual int f(){return x;}}; class B:public A{public:int f()override{return x*2;}}; A a; B b; A&r=b; cout<<a.f()<<r.f();
510
click to copy
What is the output: class A{public:void f(){cout<<'A';}}; class B:public A{using A::f;}; B b; b.f();
A
click to copy
What is the output: class Base{public:int x=1; virtual int f(){return x;}}; class Mid:public Base{public:int x=2; int f()override{return x;}}; class Der:public Mid{public:int x=3;}; Der d; Base*p=&d; cout<<p->f();
2
click to copy
What is the output: class A{public:virtual ~A(){cout<<'A';}}; class B:public A{public:~B(){cout<<'B';}}; A*p=new B; delete p;
BA
click to copy
What is the output: class A{public:int f(int x){return x*2;}}; class B:public A{public:int f(double x){return x*3;}}; B b; cout<<b.f(5);
15
click to copy
What is the output: class A{public:int x=0; A(int v=0):x(v){}}; class B:public A{public:B(int v):A(v*2){}}; B b(3); cout<<b.x;
6
click to copy