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++ → Classes and Objects 25

What is the output: class A{public:int x=10; int f(int y){return x+y;}}; A a; cout<<a.f(5);
15
click to copy
What is 'friend function' accessing private?
Declared inside class; can access private members
click to copy
What is the output: class A{int x; public: A(int v):x(v){} friend int get(A a){return a.x;}}; A a(42); cout<<get(a);
42
click to copy
What is the output: class A{public: A(){} A(A&&)=delete;}; A f(){return A();} A a=f();
OK (compiles)
click to copy
What is the output: class A{public:int v; A(int x):v(x){} A operator+(const A& o){return A(v+o.v);}}; A a(3),b(4); cout<<(a+b).v;
7
click to copy
What is 'non-static data member initializer'?
int x=5; inside class body — default member value
click to copy
What is the output: class A{public:int x=1,y=2,z=3; int sum(){return x+y+z;}}; A a; a.x=10; cout<<a.sum();
15
click to copy
What is 'this pointer' type?
T* const (in non-const member) or const T* const (in const member)
click to copy
What is the output: class A{int x; public: explicit A(int v):x(v){} int get()const{return x;}}; A a(5); cout<<a.get();
5
click to copy
What is 'std::move' on class object?
Casts to rvalue reference, enabling move constructor
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(A&& o):x(o.x*2){o.x=0;}}; A a(5); A b=move(a); cout<<a.x<<b.x;
010
click to copy
What is the output: class A{int x=0; public: void f()const{cout<<x;} void f(){x=5;cout<<x;}}; A a; a.f(); const A ca; ca.f();
50
click to copy
What is 'CRTP for static polymorphism'?
Base<Derived>: call derived method without virtual
click to copy
What is the output: class Counter{int n=0; public: Counter& operator++(){n++;return *this;} int get()const{return n;}}; Counter c; ++c;++c;++c; cout<<c.get();
3
click to copy
What is the output: class A{public:A(){cout<<'C';} A(const A&){cout<<'P';} A(A&&){cout<<'M';} ~A(){cout<<'D';}}; A a=A();
CD
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); cout<<(a<b)<<(b<a);
10
click to copy
What is the output: class A{public:int x=0; void f(int v){x=v;} auto g(){return [this]{return x;};}}; A a; a.f(42); cout<<a.g()();
42
click to copy
What is 'object layout' in C++?
Order of data members in memory (plus padding, vptr)
click to copy
What is the output: class A{public:int x=5; operator bool()const{return x!=0;}}; A a; if(a) cout<<'Y'; else cout<<'N';
Y
click to copy
What is the output: class A{int x; public: void setX(int v){x=v;} int getX(){return x;} A& chain(){return *this;}}; A a; a.chain().setX(10); cout<<a.getX();
10
click to copy
What is the output: struct Point{int x,y; Point(int x,int y):x(x),y(y){}}; Point p{3,4}; cout<<p.x<<p.y;
34
click to copy
What is the output: class A{public: static int f(int x){return x*2;}}; cout<<A::f(5);
10
click to copy
What is 'proxy object pattern'?
Object that controls access to another object
click to copy
What is the output: class A{int x; public: A(int v):x(v){} int operator%(int n)const{return x%n;}}; A a(10); cout<<a%3;
1
click to copy
What is the output: class A{public:int x=0; void f(){x+=2;}}; A a; a.f();a.f();a.f(); cout<<a.x;
6
click to copy

OOP Using C++ → Constructors and Destructors 15

What is the output: class A{public:int x; A(int v=5):x(v){} }; A a,b(10); cout<<a.x<<b.x;
510
click to copy
What is the output: class A{public:~A(){cout<<'D';}}; {A a; {A b;}}
DD
click to copy
What is the output: class A{public:int x,y; A(int a,int b):y(b),x(a){}}; A a(1,2); cout<<a.x<<a.y;
12
click to copy
What is 'copy-and-swap idiom'?
Implement assignment via copy ctor and swap for exception safety
click to copy
What is the output: class A{public:int x; A():x(0){cout<<'D';} A(int v):x(v){cout<<'P';} ~A(){cout<<'X';}}; A a; A b(5);
DPXX
click to copy
What is the output: class A{int* p; public: A():p(new int(42)){} ~A(){delete p; cout<<'D';} int get(){return *p;}}; {A a; cout<<a.get();}
42D
click to copy
What is the order of member initialization?
Declaration order in class, not initializer list order
click to copy
What is the output: class A{public:int x; A(int v):x(v){cout<<x;} ~A(){cout<<'~';}}; A a(3);
3~
click to copy
What is 'virtual constructor idiom'?
Clone pattern: virtual A* clone()=0; returns new derived copy
click to copy
What is the output: class A{public:A(){cout<<1;} ~A(){cout<<2;}}; class B:public A{public:B(){cout<<3;} ~B(){cout<<4;}}; B b;
1342
click to copy
What is the output: class A{public:int x=10; A(int v):x(v){} A()=default;}; A a; A b(5); cout<<a.x<<b.x;
105
click to copy
What is the output: struct A{int x; ~A(){cout<<x;}}; A a{1},b{2},c{3};
321
click to copy
What is 'delegating constructor'?
Constructor calling another of same class: A():A(0){}
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A():A(99){}}; A a; cout<<a.x;
99
click to copy
What is the output: class A{public:int x=1,y=2; A(int v):y(v),x(y){}}; A a(5); cout<<a.x<<a.y;
15
click to copy