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 1

What is the output: class A{public: A(){cout<<1;} A(const A&){cout<<2;} ~A(){cout<<3;}}; A f(A a){return a;} A b; f(b);
Depends on optimization
click to copy

OOP Using C++ → Inheritance 30

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
click to copy
What is 'diamond problem' in C++?
Ambiguity when class inherits from two classes sharing a base
click to copy
What is the output: class A{public: A(){cout<<'A';}}; class B:public A{public: B(){cout<<'B';}}; class C:public B{public: C(){cout<<'C';}}; C c;
ABC
click to copy
What is virtual inheritance?
Ensures single base class instance in diamond inheritance
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 &r=*new B; r.f();
B
click to copy
What is 'protected' access specifier?
Accessible in class and derived classes only
click to copy
What is the output: class A{public: int x=1;}; class B:public A{public: int x=2;}; B b; cout<<b.x<<b.A::x;
21
click to copy
What is 'private inheritance'?
Base class public/protected members become private in derived
click to copy
What is the output: struct B{int x=10;}; struct D:B{int y=20;}; D d; cout<<sizeof(d);
8
click to copy
What is the output: class A{public: virtual void f()=0;}; class B:public A{public: void f(){cout<<'B';}}; A *p=new B; p->f();
B
click to copy
What is 'slicing' problem?
Derived-specific data lost when copying to base object
click to copy
What is the output: class A{public: void f(){cout<<'A';} virtual void g(){cout<<'a';}}; class B:public A{public: void f(){cout<<'B';} void g()override{cout<<'b';}}; A*p=new B; p->f(); p->g();
Ab
click to copy
What is 'final' keyword in C++11?
Prevents further inheritance or overriding
click to copy
What is the output: class A{public: int x; A(int v):x(v){}}; class B:public A{public: B():A(5){}}; B b; cout<<b.x;
5
click to copy
What is 'covariant return type'?
Overriding function may return derived type of base's return type
click to copy
What is the output: class A{public: A(int){cout<<'A';}}; class B:public A{public: B():A(0){cout<<'B';}}; B b;
AB
click to copy
What is 'multiple inheritance' in C++?
Class inheriting from more than one base class
click to copy
What is the output: class A{public: virtual ~A(){cout<<'A';}}; class B:public A{public: ~B()override{cout<<'B';}}; A*p=new B; delete p;
BA
click to copy
What is 'abstract class' in C++?
Class with at least one pure virtual function
click to copy
What is the output: class A{int x=1; public: int get(){return x;}}; class B:private A{public: int f(){return get();}}; B b; cout<<b.f();
1
click to copy
What is the output: class A{public: void f(){cout<<'A';}}; class B:public A{using A::f; public: void f(int x){cout<<'B';}}; B b; b.f();
A
click to copy
What is 'interface' in C++ (no interface keyword)?
Abstract class with only pure virtual functions
click to copy
What is the output: class A{public: int x=5;}; class B:public A{}; class C:public A{}; class D:public B,public C{}; D d; cout<<d.B::x;
5
click to copy
What is the output: class A{public: virtual void f(){cout<<'A';} virtual void g(){cout<<'G';}}; class B:public A{public: void f()override{cout<<'B';}}; A*p=new B; p->g();
G
click to copy
What is 'protected constructor'?
Constructor callable only from derived classes
click to copy
What is the output: class A{public: int f(){return 1;}}; class B:public A{public: int f(){return 2;}}; A a=B(); cout<<a.f();
1
click to copy
What is 'EBO' (Empty Base Optimization)?
Empty base class occupies no space in derived class
click to copy
What is the output: class A{public: void f(){cout<<1;} virtual void g(){cout<<2;}}; class B:public A{public: void f(){cout<<3;} void g()override{cout<<4;}}; B b; A&r=b; r.f(); r.g();
14
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';}}; B b;
ABba
click to copy
What is 'using declaration' in derived class context?
Brings base class member into derived class scope
click to copy

OOP Using C++ → Polymorphism 9

What is 'runtime polymorphism' in C++?
Virtual function dispatch resolved at runtime via vtable
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;}}; void call(A&a){a.f();} B b; call(b);
2
click to copy
What is a 'vtable'?
Compiler-generated table of virtual function pointers
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 'compile-time polymorphism'?
Templates and function overloading
click to copy
What is the output: class A{public: virtual void f()=0;}; class B:public A{public: void f(){cout<<'B';}}; class C:public A{public: void f(){cout<<'C';}}; A*p[2]={new B,new C}; for(auto x:p) x->f();
BC
click to copy
What is RTTI in C++?
Run-Time Type Information — dynamic_cast and typeid
click to copy
What is 'dynamic_cast' used for?
Safe downcasting of polymorphic types
click to copy
What is the output: class A{public: virtual void f(){cout<<'A';} void g(){f();}}; class B:public A{public: void f()override{cout<<'B';}}; B b; b.g();
B
click to copy