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 33

What is 'PIMPL' benefit for compilation?
Implementation changes don't require recompiling users
click to copy
What is the output: class A{int x=0; public: void set(int v){if(v>=0) x=v;} int get()const{return x;}}; A a; a.set(-5); a.set(3); cout<<a.get();
3
click to copy
What is the output: class A{int x; public: A():x(42){} int& ref(){return x;}}; A a; cout<<a.ref();
42
click to copy
What is 'interface' vs 'implementation' in encapsulation?
Interface: what client uses; implementation: how it works
click to copy
What is the output: class A{int x=0,y=0; public: void move(int dx,int dy){x+=dx;y+=dy;} int getX()const{return x;} int getY()const{return y;}}; A a; a.move(3,4); a.move(-1,2); cout<<a.getX()<<a.getY();
26
click to copy
What is the output: class A{int x; public: A(int v):x(v){} int getX()const{return x;} A operator+(const A& o)const{return A(x+o.x);}}; A a(3),b(4); cout<<(a+b).getX();
7
click to copy
What is the output: class Counter{int n=0; public: void inc(){n++;} void dec(){n (1, 6, 82, 12, 4, 'What is 'const member function' and const object?
const object can only call const member functions
click to copy
What is 'const member function' and const object?
const object can only call const member functions
click to copy
What is the output: class A{mutable int c=0; public: int get()const{return ++c;}}; const A a; cout<<a.get()<<a.get();
12
click to copy
What is the output: class A{int x=0; public: void f(A& o){o.x=5;} int get()const{return x;}}; A a,b; a.f(b); cout<<b.get();
5
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;} bool operator!=(const A& o)const{return !(*this==o);}}; A a(3),b(3),c(4); cout<<(a==b)<<(a!=c);
11
click to copy
What is the output: class A{int x=0; public: class B{A& a; public: B(A& a):a(a){} void f(){a.x=10;}}; int get()const{return x;}}; A a; A::B b(a); b.f(); cout<<a.get();
10
click to copy
What is the output: class A{int x=5; public: A& setX(int v){x=v; return *this;} int getX()const{return x;}}; A a; cout<<a.setX(10).setX(20).getX();
20
click to copy
What is the output: class A{static int n; public: A(){n++;} ~A(){n (1, 6, 82, 12, 4, 'What is the output: class A{int x; public: A(int v):x(v){} void print()const{cout<<x;} friend void f(A a){a.x=99; a.print();}}; A a(5); f(a);', 'Friend function modifies copy?', '99', '99', '5', '5', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_a', 'f(a) passes by value; f modifies its own copy x=99; prints 99. Output: 99.', 0, '2026-05-25', '2026-05-25'), (1, 6, 82, 12, 4, 'What is 'opaque pointer' (pimpl)?
Forward-declared impl pointer hides implementation details
click to copy
What is the output: class A{int x; public: A(int v):x(v){} void print()const{cout<<x;} friend void f(A a){a.x=99; a.print();}}; A a(5); f(a);
99
click to copy
What is 'opaque pointer' (pimpl)?
Forward-declared impl pointer hides implementation details
click to copy
What is the output: class A{int x=0; public: void set(int v){x=v;} auto get()const{return x;}}; A a; a.set(7); cout<<a.get();
7
click to copy
What is the output: class A{int x=0; public: int& operator[](int i){x=i; return x;}}; A a; a[5]; cout<<a[3];
3
click to copy
What is 'accessor' pattern?
get/set functions controlling access to private data
click to copy
What is the output: class A{int x; public: A(int v):x(v){} int operator()(int y)const{return x+y;}}; A a(10); cout<<a(5);
15
click to copy
What is the output: class A{int x=1,y=2,z=3; public: tuple<int,int,int> getAll()const{return {x,y,z};}}; A a; auto [x,y,z]=a.getAll(); cout<<x<<y<<z;
123
click to copy
What is the output: class A{int x=0; public: void f(){x=42;} int get()const{return x;}}; const A a; a.f();
Compile error
click to copy
What is the output: class A{int x=0; public: void set(int v){x=v;} int get()const{return x;} void reset(){x=0;}}; A a; a.set(5); a.reset(); cout<<a.get();
0
click to copy
What is the output: class A{int x; public: A(int v):x(v){} A(const A& o):x(o.x){} A& operator=(const A& o){x=o.x;return *this;}}; A a(3); A b=a; b=A(7); cout<<a.x<<b.x;
37
click to copy
What is the output: class A{int x=5; public: int get()const{return x;} void set(int v){x=v;}}; A *p=new A; p->set(10); cout<<p->get(); delete p;
10
click to copy
What is 'class invariant enforcement'?
Setter validates input to maintain invariant
click to copy
What is the output: class A{int x=0; public: A& operator<<(int v){x+=v;return *this;} int get()const{return x;}}; A a; a<<1<<2<<3; cout<<a.get();
6
click to copy
What is the output: class A{int x=0; public: int get()const{return x;} void set(int v){x=(v>0?v:x);}}; A a; a.set(-1); a.set(5); cout<<a.get();
5
click to copy
What is the output: class A{int x; public: A(int v):x(v){} operator string()const{return to_string(x);}}; A a(42); string s=a; cout<<s;
42
click to copy
What is the output: class A{vector<int> v; public: void add(int x){v.push_back(x);} int size()const{return v.size();} int at(int i)const{return v.at(i);}}; A a; a.add(1);a.add(2);a.add(3); cout<<a.size()<<a.at(1);
32
click to copy
What is the output: class A{int x=0; public: A& operator+=(int v){x+=v;return *this;} operator int()const{return x;}}; A a; a+=5; a+=3; cout<<(int)a;
8
click to copy
What is the output: class A{int x=0; public: int get()const{return x;} friend A operator+(A a,const A& b){a.x+=b.x;return a;}}; A a; a.x; int y=0; //can't access x directly, use friend
Works with friend
click to copy
What is the output: class A{int x=0; public: void f(){x++;} int g()const{return x;}}; A *p=new A[3]; for(int i=0;i<3;i++) p[i].f(); cout<<p[1].g(); delete[]p;
1
click to copy

OOP Using C++ → Abstraction 7

What is the output: class A{public:virtual int f()=0; int g(){return f()*2;}}; class B:public A{public:int f(){return 5;}}; B b; cout<<b.g();
10
click to copy
What is the output: template<typename T> T cube(T x){return x*x*x;} cout<<cube(3)<<cube(2.0);
278.0
click to copy
What is the output: class Stack{vector<int>v; public: void push(int x){v.push_back(x);} int top(){return v.back();} int size(){return v.size();}}; Stack s; s.push(1);s.push(2);s.push(3); cout<<s.top()<<s.size();
33
click to copy
What is the output: class A{public:virtual void f()=0;}; class B:public A{public:void f()override{cout<<'B';}}; function<void()> fn=[]{B().f();}; fn();
B
click to copy
What is the output: auto add=[](int a,int b){return a+b;}; auto mul=[](int a,int b){return a*b;}; auto apply=[](auto f,int a,int b){return f(a,b);}; cout<<apply(add,3,4)<<apply(mul,3,4);
712
click to copy
What is the output: class Queue{deque<int>d; public: void enq(int x){d.push_back(x);} int deq(){int x=d.front();d.pop_front();return x;} int size(){return d.size();}}; Queue q; q.enq(1);q.enq(2);q.enq(3); cout<<q.deq()<<q.size();
12
click to copy
What is the output: class A{public:virtual double compute(double x)=0;}; class Square:public A{public:double compute(double x)override{return x*x;}}; class Cube:public A{public:double compute(double x)override{return x*x*x;}}; vector<A*> ops={new Square,new Cube}; for(auto p:ops) cout<<p->compute(3)<<' ';
9 27
click to copy