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 28

What is the output: int a=1,b=2,c=3; int *arr[3]={&c,&b,&a}; cout<<*arr[0]+*arr[2];
4
click to copy
What is the output: struct A{int x=5; int* p=&x;}; A a; cout<<*(a.p);
5
click to copy
What is the output: int x=10; int *p=&x; auto q=p; *q=20; cout<<x;
20
click to copy
What is the output: int a[]={1,2,3,4,5}; int *p=a; int *q=a+5; cout<<(q-p)<<*(q-1);
55
click to copy
What is the output: shared_ptr<int> p; cout<<(bool)p<<' '; p=make_shared<int>(5); cout<<(bool)p;
0 1
click to copy
What is the output: int x=5; int *p=&x; *p=*p*2; cout<<x;
10
click to copy
What is the output: int a[]={10,20,30}; auto [x,y,z]=a; cout<<x<<y<<z;
102030
click to copy
What is the output: int x=5; weak_ptr<int> w; {auto s=make_shared<int>(x); w=s;} cout<<w.expired();
1
click to copy
What is the output: int a[]={1,2,3}; int *p=end(a); while(p!=begin(a)) cout<<*( (1, 6, 77, 7, 4, 'What is the output: auto del=[](int *p){delete p; cout<<'D';}; unique_ptr<int,decltype(del)> p(new int(5),del); cout<<*p;
5D
click to copy
What is the output: auto del=[](int *p){delete p; cout<<'D';}; unique_ptr<int,decltype(del)> p(new int(5),del); cout<<*p;
5D
click to copy
What is the output: int x=5,y=10; int* arr[]={&x,&y}; *arr[0]+=*arr[1]; cout<<x;
15
click to copy
What is the output: int a[]={1,2,3,4,5}; sort(a,a+5,[](int a,int b){return a>b;}); cout<<a[0]<<a[4];
51
click to copy
What is the output: int x=42; const int* p=&x; cout<<*p;
42
click to copy
What is the output: int a[3]={1,2,3}; auto f=[](auto *p,int n){return accumulate(p,p+n,0);}; cout<<f(a,3);
6
click to copy
What is the output: int* f(int n){return new int(n*n);} auto p=unique_ptr<int>(f(5)); cout<<*p;
25
click to copy
What is the output: int a[]={5,4,3,2,1}; int *p=max_element(a,a+5); cout<<*p<<(p-a);
50
click to copy
What is the output: int x=10; int *p=&x; int *q=&x; cout<<(*p=20,*q);
20
click to copy
What is the output: shared_ptr<int> a=make_shared<int>(1); auto b=a; auto c=a; cout<<a.use_count();
3
click to copy
What is the output: int a[]={1,2,3,4,5}; int *p=a; advance(p,2); cout<<*p<<' '<<*(p+1);
3 4
click to copy
What is the output: int x=5; auto f=[p=&x](int v){*p+=v;}; f(10); cout<<x;
15
click to copy
What is the output: unique_ptr<int[]> p=make_unique<int[]>(5); iota(p.get(),p.get()+5,1); cout<<p[4];
5
click to copy
What is the output: int a[]={1,2,3}; cout<<accumulate(a,a+3,0,[](int s,int x){return s*10+x;});
123
click to copy
What is the output: int x=5; auto sp=make_shared<int>(x); auto wp=weak_ptr(sp); if(auto lp=wp.lock()) cout<<*lp;
5
click to copy
What is the output: int a[]={1,2,3,4,5}; int *b=new int[5]; copy(a,a+5,b); cout<<b[2]; delete[] b;
3
click to copy
What is the output: int x=5,y=10,z=15; int *ptrs[]={&x,&y,&z}; int s=0; for(auto p:ptrs) s+=*p; cout<<s;
30
click to copy
What is the output: int a[]={3,1,4,1,5}; nth_element(a,a+2,a+5); cout<<a[2];
3
click to copy
What is the output: struct Node{int v; shared_ptr<Node> next;}; auto a=make_shared<Node>(Node{1,nullptr}); auto b=make_shared<Node>(Node{2,a}); cout<<b->next->v;
1
click to copy
What is the output: int a[]={1,2,3}; int *p=a; cout<<(*p++)<<(*p++);
12
click to copy

OOP Using C++ → Classes and Objects 10

What is the output: class A{public:int x; A(int v):x(v){} A operator*(const A& o){return {x*o.x};}}; A a(3),b(4); cout<<(a*b).x;
12
click to copy
What is the output: class A{int x=0; public: auto& ref(){return x;}}; A a; a.ref()=42; cout<<a.ref();
42
click to copy
What is the output: class A{public:int x=5;}; class B{public:A a;}; B b; cout<<b.a.x;
5
click to copy
What is the output: class A{public:int x=0; friend A operator+(A a,A b){return A(a.x+b.x); A(int v):x(v){}}};
Compile error
click to copy
What is the output: class A{public: int x=0; void f()&{x=1;} void f()&&{x=2;}}; A a; a.f(); cout<<a.x; A().f(); cout<<A().x;
12
click to copy
What is the output: class A{public:A()=delete;}; A a;
Compile error
click to copy
What is the output: class A{public:int x; A(int v):x(v){} auto operator<=>(const A&)const=default;}; A a(3),b(5); cout<<(a<b)<<(a==b);
10
click to copy
What is the output: class A{public:int x=0; void inc(int n=1){x+=n;}}; A a; a.inc(); a.inc(5); cout<<a.x;
6
click to copy
What is the output: class A{public:int x=0; explicit operator bool()const{return x!=0;}}; A a; a.x=5; if(a) cout<<'Y'; else cout<<'N';
Y
click to copy
What is the output: class A{int x; public: A(int v):x(v){} void print()const{cout<<x;} A& operator++(){x++;return *this;}}; A a(5); ++a; ++a; a.print();
7
click to copy

OOP Using C++ → Constructors and Destructors 2

What is the output: class A{public:int x; A(int v):x(v){} A(const A& o):x(o.x*2){}}; A a(3); A b(a); A c(b); cout<<a.x<<b.x<<c.x;
3612
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=5; cout<<a.get();
5
click to copy