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++ → Inheritance 4

What is the output: struct A{int x=1; virtual void f(){x=2;}}; struct B:A{void f()override{x=3;}}; A a; B b; A& ra=a; B& rb=b; ra.f(); rb.f(); cout<<a.x<<b.x;
23
click to copy
What is the output: class A{public:virtual ~A(){} virtual int f()=0;}; class B:public A{int v; public:B(int x):v(x){} int f()override{return v;}}; shared_ptr<A>p=make_shared<B>(99); cout<<p->f();
99
click to copy
What is the output: class A{public:int x=0; A(){} A(const A& o):x(o.x+10){}}; class B:public A{public:B(){x=5;}}; B b; A a=b; cout<<a.x;
15
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* p=&b; p->f(); p->g(); b.f(); b.g();
1434
click to copy

OOP Using C++ → Polymorphism 27

What is the output: class A{public:virtual int f(int x=1){return x*2;}}; class B:public A{public:int f(int x=2)override{return x*3;}}; A*p=new B; cout<<p->f();
3
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} virtual void f(int){cout<<'X';}}; class B:public A{public:void f()override{cout<<'B';}}; A*p=new B; p->f(); p->f(1);
BX
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 g(){f(); A::f();}}; B b; b.g();
21
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:void f()override{cout<<'C';}}; A*p=new C; B*q=dynamic_cast<B*>(p); q->f();
C
click to copy
What is the output: class A{public:virtual int f()=0; int g(){return f()*2;}}; class B:public A{public:int f()override{return 7;}}; A*p=new B; cout<<p->g();
14
click to copy
What is the output: class A{public:virtual void f(){cout<<1;}}; class B:public A{public:void f()final{cout<<2;}}; A*p=new B; p->f();
2
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;}}; auto v=vector<A>{{},{},{}};A* p=new B; v.push_back(*p); v.back().f();
1
click to copy
What is the output: class A{public:virtual ~A(){} virtual void f()=0;}; class B:public A{public:void f()override{cout<<'B';}}; class C:public A{public:void f()override{cout<<'C';}}; vector<A*>v={new B,new C,new B,new C}; int n=0; for(auto p:v) {p->f(); if(dynamic_cast<B*>(p))n++;} cout<<n;
BCBC2
click to copy
What is the output: class A{public:virtual int f(){return 1;} virtual int g(){return f()+1;}}; class B:public A{public:int f()override{return 2;}}; B b; cout<<b.g();
3
click to copy
What is the output: class A{public:virtual void f()=0;}; struct B:A{void f()override{cout<<'B';}}; struct C:A{void f()override{cout<<'C';}}; auto apply=[](A& a){a.f();}; B b; C c; apply(b); apply(c);
BC
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 *arr[3]={new A,new B,new B}; for(auto p:arr)p->f();
122
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;}}; int apply(A* p,int n){int s=0;for(int i=0;i<n;i++)s+=p[i].f();return s;}; A arr[3]; cout<<apply(arr,3);
3
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* make(int t){return t?new B:new A;} for(int i=0;i<3;i++){auto*p=make(i%2);p->f();delete p;}
ABA
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;}}; template<typename T> void call(T* p){p->f();} A a; B b; call(&a); call(&b);
12
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} virtual void g()=0;}; class B:public A{public:void f()override{cout<<'B';} void g()override{cout<<'G';}}; B b; b.f(); b.g();
BG
click to copy
What is the output: class A{public:int x=0; virtual void inc(){x++;}}; class B:public A{public:void inc()override{x+=5;}}; vector<A*>v={new A,new B,new A}; for(auto p:v)p->inc(); int s=0; for(auto p:v)s+=p->x; cout<<s;
7
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} void g(){f();cout<<'G';}}; class B:public A{public:void f()override{cout<<'B';}}; B b; b.g();
BG
click to copy
What is the output: class A{public:virtual void f(){cout<<'A';} ~A(){}}; class B:public A{public:void f()override{cout<<'B';}}; unique_ptr<A>p=make_unique<B>(); p->f();
B
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 obj; B bobj; A* ptrs[]={&obj,&bobj,&obj}; for(A*p:ptrs)p->f();
121
click to copy
What is the output: class A{public:virtual string name(){return "A";} string greet(){return "Hi "+name();}}; class B:public A{public:string name()override{return "B";}}; B b; cout<<b.greet();
Hi B
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;}}; function<void(A*)>fp=[](A*p){p->f();}; fp(new A); fp(new B);
12
click to copy
What is the output: class A{public:virtual void f()=0; virtual ~A()=default;}; class B:public A{int x; public:B(int v):x(v){} void f()override{cout<<x;}}; vector<shared_ptr<A>>v; v.push_back(make_shared<B>(5)); v.push_back(make_shared<B>(10)); for(auto&p:v)p->f();
510
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; cout<<typeid(*p).name();
1B
click to copy
What is the output: class A{public:virtual void f()=0; virtual ~A()=default;}; class B:public A{public:void f()override{}}; A *p=new B; delete p; cout<<'X';
X
click to copy
What is the output: class A{public:virtual int f(){return 1;} int h(){return f()*f();}}; class B:public A{public:int f()override{return 3;}}; B b; cout<<b.h();
9
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.f();B &rb=b;rb.f();
22
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;}}; map<string,A*>m;m["a"]=new A;m["b"]=new B; for(auto&[k,v]:m)v->f();
12
click to copy

OOP Using C++ → Encapsulation 9

What is the output: class A{int x=5; public:int get()const{return x;} void set(int v){if(v>0)x=v;}}; A a; a.set(-3); cout<<a.get(); a.set(7); cout<<a.get();
57
click to copy
What is the output: class A{int x; public:A(int v):x(v){} int get()const{return x;} A operator+(const A& o)const{return {x+o.x};}}; A a(3),b(4); A c=a+b; cout<<c.get();
7
click to copy
What is the output: class A{int x=0,y=0; public:void setX(int v){x=v;} void setY(int v){y=v;} int sum()const{return x+y;}}; A a; a.setX(3); a.setY(4); cout<<a.sum();
7
click to copy
What is the output: class A{vector<int>v; public:void add(int x){if(x>=0)v.push_back(x);} int max()const{return *max_element(v.begin(),v.end());}}; A a; a.add(-1);a.add(3);a.add(7);a.add(-2);a.add(5); cout<<a.max();
7
click to copy
What is the output: class A{int x; public:A(int v):x(v){} int get()const{return x;} A& operator*=(int n){x*=n;return *this;}}; A a(3); a*=2; a*=3; cout<<a.get();
18
click to copy
What is the output: class Safe{int arr[10]={0}; public:int& at(int i){if(i<0||i>=10)throw out_of_range("OOB");return arr[i];}}; Safe s; s.at(3)=42; cout<<s.at(3);
42
click to copy
What is the output: class A{int x=0; public:int get()const{return x;} void reset(){x=0;} A& set(int v){x=v;return *this;}}; A a; cout<<a.set(5).get()<<a.reset(),a.get();
50
click to copy
What is the output: class A{int x; public:A(int v=0):x(v){} bool isValid()const{return x>=0&&x<=100;} void clip(){if(x<0)x=0;if(x>100)x=100;}}; A a(-5); a.clip(); cout<<a.isValid();
1
click to copy
What is the output: class A{int x=1,y=2,z=3; public:auto getAll()const{return make_tuple(x,y,z);}}; A a; auto [x,y,z]=a.getAll(); cout<<x+y+z;
6
click to copy