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++ → Encapsulation 11

What is the output: class A{int x=5; public: void print()const{cout<<x;} void modify(){x=10;}}; const A a; a.print();
5
click to copy
What is 'interface segregation' and encapsulation?
Many specific interfaces better than one general interface
click to copy
What is the output: class A{int x=0; public: int get()const{return x;} void set(int v){if(v>0) x=v;}}; A a; a.set(-5); cout<<a.get();
0
click to copy
What is the output: class A{int x; public: A(int v=0):x(v){} int operator+(const A&o)const{return x+o.x;}}; A a(3),b(4); cout<<a+b;
7
click to copy
What is 'friend class' in C++?
Class with access to another class's private/protected members
click to copy
What is the output: class A{int x; public: A(int v):x(v){} friend ostream& operator<<(ostream& os,const A& a){os<<a.x; return os;}}; A a(99); cout<<a;
99
click to copy
What is the purpose of making constructors private?
Singleton pattern — prevents direct instantiation
click to copy
What is the output: class A{int x=1,y=2; public: int sum()const{return x+y;} void scale(int f){x*=f; y*=f;}}; A a; a.scale(3); cout<<a.sum();
9
click to copy
What is 'module' in C++20 related to encapsulation?
Replaces header files; exports only declared interface
click to copy
What is the output: class A{int x; public: void setX(int v){x=v;} int getX()const{return x;}}; A a; cout<<a.getX();
Undefined behavior
click to copy
What is 'cohesion' in OOP?
Degree to which class members relate to single purpose
click to copy

OOP Using C++ → Abstraction 29

What is abstraction in C++?
Showing essential features, hiding unnecessary details
click to copy
What is the output: class Shape{public: virtual double area()=0;}; class Circle:public Shape{double r; public: Circle(double r):r(r){} double area(){return 3.14*r*r;}}; Shape*s=new Circle(1); printf("%.2f",s->area());
3.14
click to copy
What is an 'interface' in C++ abstraction?
Abstract class with only pure virtual functions and no data
click to copy
What is the output: class A{public: virtual int f()=0; virtual ~A()=default;}; class B:public A{public: int f(){return 42;}}; A*p=new B; cout<<p->f();
42
click to copy
What is 'type erasure' in C++?
Hiding concrete type behind abstract interface
click to copy
What is the output: class A{protected: int x; public: A(int v):x(v){} virtual void show()=0;}; class B:public A{public: B(int v):A(v){} void show(){cout<<x;}}; B b(7); b.show();
7
click to copy
What is 'concept' in C++20?
Named set of requirements on template arguments
click to copy
What is the output: class Animal{public: virtual string sound()=0; string describe(){return "I say: "+sound();}}; class Dog:public Animal{public: string sound(){return "Woof";}}; Dog d; cout<<d.describe();
I say: Woof
click to copy
What is 'dependency inversion principle'?
Depend on abstractions, not on concrete types
click to copy
What is the output: template<typename T> T maxVal(T a,T b){return a>b?a:b;} cout<<maxVal(3,7)<<maxVal(2.5,1.5);
72.5
click to copy
What is the output: class Stack{vector<int> v; public: void push(int x){v.push_back(x);} int pop(){int x=v.back();v.pop_back();return x;}}; Stack s; s.push(1); s.push(2); cout<<s.pop();
2
click to copy
What is 'generic programming'?
Writing code independent of specific types using templates
click to copy
What is 'template specialization' as abstraction?
Providing specific implementation for particular type
click to copy
What is the output: auto square=[](auto x){return x*x;}; cout<<square(3)<<square(2.5);
96.25
click to copy
What is 'std::function' and type erasure?
Wraps any callable with matching signature, hiding concrete type
click to copy
What is the output: class Shape{public: virtual double perimeter()=0; virtual double area()=0;}; class Rect:public Shape{double w,h; public: Rect(double w,double h):w(w),h(h){} double perimeter(){return 2*(w+h);} double area(){return w*h;}}; Rect r(3,4); cout<<r.area()<<' '<<r.perimeter();
12 14
click to copy
What is 'abstraction layer'?
All of the above
click to copy
What is 'std::variant' as abstraction?
Type-safe union holding one of several types
click to copy
What is the output: class Logger{public: virtual void log(string msg)=0;}; class ConsoleLog:public Logger{public: void log(string msg){cout<<msg;}}; Logger*l=new ConsoleLog; l->log("OK");
OK
click to copy
What is 'template method pattern'?
Base class defines algorithm skeleton; derived classes fill in steps
click to copy
What is the output: template<int N> struct Factorial{static const int value=N*Factorial<N-1>::value;}; template<> struct Factorial<0>{static const int value=1;}; cout<<Factorial<5>::value;
120
click to copy
What is 'std::any' in C++17?
Type-safe container for single value of any type
click to copy
What is 'abstraction vs implementation' tradeoff?
More abstraction: flexibility + overhead; less: efficiency + rigidity
click to copy
What is the output: class Base{public: void interface(){impl();} private: virtual void impl(){cout<<'B';}}; class Der:public Base{private: void impl()override{cout<<'D';}}; Der d; d.interface();
D
click to copy
What is 'non-virtual interface' (NVI) pattern?
Public non-virtual function calls private virtual customization point
click to copy
What is the output: struct Functor{int x; Functor(int v):x(v){} int operator()(int y){return x+y;}}; Functor f(10); cout<<f(5);
15
click to copy
What is 'policy-based design'?
Using template parameters to inject behavior at compile time
click to copy
What is the output: class A{public: virtual void f(){cout<<1;}}; void g(A* a){a->f();} class B:public A{public: void f(){cout<<2;}}; g(new B);
2
click to copy
What is 'duck typing' concept in C++?
If it has the right interface, use it — achieved via templates
click to copy