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++ → Polymorphism 20

What is 'static polymorphism' via templates?
CRTP — behavior customized at compile time via template parameter
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';}}; void call(A a){a.f();} B b; call(b);
A
click to copy
What is the output: class A{public: virtual int f(){return 10;} int g(){return f()*2;}}; class B:public A{public: int f()override{return 20;}}; B b; cout<<b.g();
40
click to copy
What happens when you call a virtual function on a null pointer?
Undefined behavior
click to copy
What is the output: class A{public: virtual void f(){cout<<1;}}; class B:public virtual A{public: void f(){cout<<2;}}; class C:public virtual A{public: void f(){cout<<3;}}; class D:public B,public C{}; D d; d.f();
Compile error
click to copy
What is 'pure virtual function'?
virtual void f()=0 — must be overridden in derived class
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';}}; class C:public B{public: void f(){cout<<'C';}}; A*p=new C; p->f();
Compile error
click to copy
What is the output: class A{public: virtual void f(){cout<<1;} virtual void f(int){cout<<2;}}; class B:public A{public: void f()override{cout<<3;}}; B b; b.f(5);
Compile error
click to copy
What is the output: class A{public: virtual void f(){cout<<'A';} ~A()=default;}; class B:public A{}; B b; b.f();
A
click to copy
What is 'function hiding' in C++?
Derived class function with same name hides all base class overloads
click to copy
What is the output: class A{public: virtual void f(){cout<<'A';}}; class B:public A{public: virtual void f(){cout<<'B';}}; class C:public B{public: virtual void f(){cout<<'C';}}; A*p=new C; delete p;
UB: A's dtor called only
click to copy
What is typeid() used for?
Getting runtime type information
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;}}; A *p=new B; static_cast<B*>(p)->A::f();
1
click to copy
What is 'open/closed principle' relation to polymorphism?
Polymorphism enables extending behavior without modifying existing code
click to copy
What is the output: class A{public: int x; virtual void f(){}}; class B:public A{}; sizeof(A), sizeof(B) on 64-bit?
16 and 16
click to copy
What is 'dynamic dispatch overhead' in C++?
Indirect function call through vtable pointer
click to copy
What is the output: class A{public: virtual void f()=0; virtual ~A()=default;}; class B:public A{public: void f(){cout<<'B';}}; A *p=new B; p->f(); delete p;
B
click to copy
What is 'override' specifier?
Checks at compile time that function actually overrides a virtual
click to copy
What is 'polymorphic type' in C++?
Class with at least one virtual function
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;}}; B b; A&r=b; r.A::f();
1
click to copy

OOP Using C++ → Encapsulation 20

What is encapsulation?
Bundling data and methods, controlling access
click to copy
What is the output: class A{int x=42; public: int get()const{return x;}}; A a; cout<<a.get();
42
click to copy
What does 'const' member function guarantee?
Doesn't modify non-mutable data members
click to copy
What is the output: class A{public: int x; void setX(int v){x=v;} int getX(){return x;}}; A a; a.setX(10); cout<<a.getX();
10
click to copy
What is 'information hiding'?
Hiding implementation details from users
click to copy
What is the output: class A{int x=5; public: void f(){x*=2;} int g()const{return x;}}; A a; a.f(); a.f(); cout<<a.g();
20
click to copy
What is a 'getter' function?
Public function that returns value of private member
click to copy
What is 'data abstraction' vs 'data hiding'?
Abstraction shows interface; hiding conceals implementation
click to copy
What is the output: class A{int x; public: A():x(0){} void inc(){x++;} bool isEven()const{return x%2==0;}}; A a; a.inc(); a.inc(); a.inc(); cout<<a.isEven();
0
click to copy
What is the output: class A{static int c; public: A(){c++;} static int count(){return c;}}; int A::c=0; A a,b,c; cout<<A::count();
3
click to copy
What is 'class invariant'?
Condition always true for valid object state
click to copy
What is the output: class A{int x=10; public: int& ref(){return x;}}; A a; a.ref()=20; cout<<a.ref();
20
click to copy
What is 'opaque type' in C++?
Type with hidden implementation (forward declaration only)
click to copy
What is the output: class A{int x,y; public: A(int a,int b):x(a),y(b){} int sum()const{return x+y;}}; A a(3,4); cout<<a.sum();
7
click to copy
What is 'PIMPL idiom'?
Pointer to Implementation — hiding implementation in separate class
click to copy
What is the output: class A{int x; public: A(int v):x(v){} operator int(){return x;}}; A a(5); cout<<a+3;
8
click to copy
What is 'access specifier' in C++?
public, protected, private — controls member visibility
click to copy
What is the output: class A{int x=0; public: A& operator++(){x++; return *this;} int get()const{return x;}}; A a; ++a; ++a; ++a; cout<<a.get();
3
click to copy
What is 'const correctness'?
Using const wherever possible to prevent unintended modification
click to copy
What is the output: class A{int x; public: A(int v):x(v){} bool operator==(const A&o)const{return x==o.x;}}; A a(5),b(5); cout<<(a==b);
1
click to copy