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 10

What is a 'nested class' in C++?
A class defined inside another class
click to copy
What is the output: class A{public: ~A(){cout<<'D';}}; A *p=new A; delete p;
D
click to copy
What is 'method chaining' in C++?
Returning *this from member functions to chain calls
click to copy
What is the output: struct A{int x,y; A(int a,int b):x(a),y(b){}}; A a{3,4}; cout<<a.x+a.y;
7
click to copy
What is 'CRTP' (Curiously Recurring Template Pattern)?
Base class template parameterized with derived class
click to copy
What is the output: class A{public: int x=5; int& f(){return x;}}; A a; a.f()=10; cout<<a.x;
10
click to copy
What is the output: class A{int x=0; public: void inc(){x++;} int get()const{return x;}}; A a,b=a; a.inc(); cout<<a.get()<<b.get();
10
click to copy
What is 'type punning' in C++?
Reinterpreting memory as different type
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*p=new B; p->f(); delete p;
B
click to copy
What is the output: class A{public: A(){cout<<'A';} A(const A&){cout<<'C';}}; A f(){return A();} A a=f();
A
click to copy

OOP Using C++ → Constructors and Destructors 30

What is the order of constructor calls in: class A{}; class B:public A{};?
A then B
click to copy
What is the output: class A{public: A(){cout<<1;} ~A(){cout<<2;}}; A a;
12
click to copy
What is a 'delegating constructor' in C++11?
Constructor calling another constructor of same class
click to copy
What is the output: struct A{int x,y; A(int a=0,int b=0):x(a),y(b){}}; A a(1,2); cout<<a.x<<a.y;
12
click to copy
What is the output: class A{public: A(){cout<<'A';} A(const A&){cout<<'C';} ~A(){cout<<'D';}}; A a; A b(a);
ACDD
click to copy
What is the output of destructor order for: class A{public:~A(){cout<<'A';}}; class B{A a; public:~B(){cout<<'B';}}; B b;
AB
click to copy
What is 'member initializer list'?
Syntax for initializing members before constructor body
click to copy
What is the output: class A{public: int x; A(int v):x(v){cout<<x;} ~A(){cout<<-x;}}; A a(3);
3-3
click to copy
When is default constructor automatically generated?
When no user-defined constructor exists
click to copy
What is the output: class A{public: A(){cout<<'A';} A(A&&){cout<<'M';} ~A(){cout<<'D';}}; A f(){return A();} A a=f();
A
click to copy
What is the output: class A{int x; public: A(int v=5):x(v){} ~A(){cout<<x;}}; {A a; A b(3);}
35
click to copy
What is 'virtual destructor'?
Destructor called via vtable for proper derived class cleanup
click to copy
What is the output: class A{public: A(int){cout<<'P';} A(){cout<<'D';}}; A a; A b(1);
DP
click to copy
What is the output: class B{public:~B(){cout<<'B';}}; class D:public B{public:~D(){cout<<'D';}}; B*p=new D; delete p;
Undefined behavior (memory leak possible)
click to copy
What is 'RAII constructor'?
Constructor that acquires resources
click to copy
What is the output: class A{public: A(){x=++c;} static int c; int x;}; int A::c=0; A a,b,c; cout<<a.x<<b.x<<c.x;
123
click to copy
What is a 'converting constructor'?
Constructor enabling implicit type conversion
click to copy
What is the output: class A{public: ~A(){cout<<'X';}}; A *p=new A[3]; delete[] p;
XXX
click to copy
What happens if constructor throws an exception?
Object is partially constructed; destructor NOT called
click to copy
What is the output: class A{public: A(int x){cout<<x;} A(const A&a){cout<<'C';}}; void f(A a){} A obj(5); f(obj);
5C
click to copy
What is 'move constructor'?
Constructor transferring resources from rvalue
click to copy
What is the output: class A{public: A(){cout<<'N';} A(int){cout<<'I';} A(double){cout<<'D';}}; A a(1), b(1.0), c;
IDN
click to copy
What is the output: class A{public: int x=0; A()=default;}; A a{}; cout<<a.x;
0
click to copy
What is 'Rule of Five' in C++11?
Define all 5: destructor, copy-ctor, copy-assign, move-ctor, move-assign
click to copy
What is the output: class A{public: A(){cout<<'B';} ~A(){cout<<'E';} void f(){cout<<'M';}}; A a; a.f();
BME
click to copy
When are destructors called in reverse order?
All of the above
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=A(5); cout<<a.get();
5
click to copy
What is the output: class A{public: A(int x=10,int y=20){cout<<x+y;}}; A a,b(5),c(5,6);
302511
click to copy
What is the output: class A{public: ~A()=delete;}; {A a;}
Compile error
click to copy
What is 'trivial destructor'?
Destructor that does nothing and is compiler-generated
click to copy