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++ → Pointers 29

What is the output: int a[]={10,20,30}; for(int *p=a;p<a+3;p++) *p*=2; cout<<a[1];
40
click to copy
What is the output: char s[]="hello"; for(char *p=s;*p;p++) if(*p=='l') *p='L'; cout<<s;
heLLo
click to copy
What is the output: int a[]={5,3,8,1,4}; int *mx=a; for(int *p=a+1;p<a+5;p++) if(*p>*mx) mx=p; cout<<*mx<<(mx-a);
82
click to copy
What is the output: int x=5; const int* cp=&x; int* const pc=&x; *pc=10; cout<<*cp;
10
click to copy
What is the output: auto p=make_unique<pair<int,int>>(3,4); cout<<p->first+p->second;
7
click to copy
What is the output: int a[]={1,2,3,4,5}; int *p=a; while(*p++!=3) {} cout<<*p;
4
click to copy
What is the output: int x=5; shared_ptr<int> p=make_shared<int>(); *p=x*2; cout<<*p;
10
click to copy
What is the output: int a[5]; int *p=a; for(int i=0;i<5;i++) *p++=i*i; cout<<a[3];
9
click to copy
What is the output: string s="hello"; char *p=&s[0]; *(p+4)='!'; cout<<s;
hell!
click to copy
What is the output: int a[]={1,2,3,4,5}; int *b=new int[5]; copy(a,a+5,b); transform(b,b+5,b,[](int x){return x*x;}); cout<<b[2]; delete[]b;
9
click to copy
What is the output: int x=5,y=10; int *p=&x; swap(p,*(int**)&y);
Undefined behavior
click to copy
What is the output: int a[]={3,1,4,1,5}; sort(a,a+5); int *it=lower_bound(a,a+5,1); cout<<(it-a);
0
click to copy
What is the output: vector<int> v={1,2,3}; auto p=v.data(); p[1]=10; cout<<v[1];
10
click to copy
What is the output: int a[]={1,2,3,4,5}; auto* p=ranges::max_element(a); cout<<*p;
5
click to copy
What is the output: int x=0; auto inc=[&x]()noexcept{x++;}; for(int i=0;i<10;i++) inc(); cout<<x;
10
click to copy
What is the output: int a[]={1,2,3}; int (*p)[3]=&a; cout<<(*p)[0]+(*p)[2];
4
click to copy
What is the output: int x=10; auto sp1=make_shared<int>(x); auto sp2=sp1; *sp1=20; cout<<*sp2;
20
click to copy
What is the output: void f(unique_ptr<int>p){cout<<*p;} f(make_unique<int>(99));
99
click to copy
What is the output: int a[]={1,2,3,4,5}; auto it=find(begin(a),end(a),4); *it*=10; cout<<a[3];
40
click to copy
What is the output: int x=5; auto& r=*make_unique<int>(x); cout<<r;
Undefined behavior
click to copy
What is the output: int a[]={10,20,30}; int *p=a; cout<<p[1]<<1[p]<<*(1+p);
202020
click to copy
What is the output: int x=5; int *p=new int(*p);
Undefined behavior
click to copy
What is the output: int a[]={5,4,3,2,1}; auto f=[](int *b,int *e){while(b<e) cout<<*b++;};f(a,a+5);
54321
click to copy
What is the output: int x=42; auto wp=weak_ptr<int>{}; {auto sp=make_shared<int>(x); wp=sp; cout<<*wp.lock()<<' ';} cout<<wp.expired();
42 1
click to copy
What is the output: int a[3]={1,2,3}; int *p=a+3; cout<<(p==end(a));
1
click to copy
What is the output: int a[]={1,2,3,4,5}; int* mid=a+2; cout<<mid[-2]<<mid[-1]<<mid[0]<<mid[1]<<mid[2];
12345
click to copy
What is the output: int x=0; auto p=&x; for(int i=0;i<5;++i) ++*p; cout<<x;
5
click to copy
What is the output: struct P{int x,y;}; P a{3,4}; P *p=&a; cout<<p->x*p->x+p->y*p->y;
25
click to copy
What is the output: int a[]={2,4,6,8,10}; for(auto *p=rbegin(a);p!=rend(a);++p) cout<<*p;
108642
click to copy

OOP Using C++ → Classes and Objects 11

What is the output: class A{public:int x=0; A& add(int v){x+=v;return *this;} A& mul(int v){x*=v;return *this;}}; A a; cout<<a.add(3).add(4).mul(2).x;
14
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A operator-()const{return A(-x);}}; A a(5); cout<<(-a).x;
-5
click to copy
What is the output: class A{public:int x=0; A(int v=0):x(v){} bool operator<(const A& o)const{return x<o.x;}}; vector<A>v={{5},{3},{8},{1}}; sort(v.begin(),v.end()); cout<<v[0].x<<v[3].x;
18
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){x=o.x; return *this;}}; A a(3),b(5); a=b=A(10); cout<<a.get()<<b.get();
1010
click to copy
What is the output: class Vec{public:double x,y; Vec(double a,double b):x(a),y(b){} double dot(const Vec& v)const{return x*v.x+y*v.y;}}; Vec a(1,2),b(3,4); cout<<a.dot(b);
11
click to copy
What is the output: class A{public:int x=0; void set(int v){x=v;} void reset(){x=0;} int get()const{return x;}}; A a; a.set(5); cout<<a.get(); a.reset(); cout<<a.get();
50
click to copy
What is the output: class A{public:int data[3]; A(int a,int b,int c){data[0]=a;data[1]=b;data[2]=c;} int& operator[](int i){return data[i];}}; A a(1,2,3); a[1]=10; cout<<a[0]<<a[1]<<a[2];
1103
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A operator+(int n)const{return A(x+n);} friend A operator+(int n,const A& a){return A(n+a.x);}}; A a(5); cout<<(a+3).x<<(3+a).x;
88
click to copy
What is the output: class A{int x=1; public: class B{int y=2; public: void f(A& a){cout<<a.x+y;}};}; A a; A::B b; b.f(a);
3
click to copy
What is the output: class A{public:int v; explicit A(int x):v(x){} explicit operator int()const{return v;}}; A a(5); cout<<(int)a;
5
click to copy
What is the output: class A{public:int x=5; int f()const{return x;} int g(){return x*2;}}; const A a; cout<<a.f();
5
click to copy