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++ → Classes and Objects 19

What is the output: class A{public:int x; A(int v):x(v){} A(const A& o):x(o.x){} A& operator=(A o){swap(x,o.x);return *this;}}; A a(3),b(5); a=b; cout<<a.x<<b.x;
53
click to copy
What is the output: class A{public:int n; A(int v):n(v){} bool operator==(const A& o)const{return n==o.n;} 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{mutable int cache=-1; public:int compute(int x)const{if(cache==-1)cache=x*x; return cache;}}; const A a; cout<<a.compute(5)<<a.compute(6);
2525
click to copy
What is the output: class A{public:int x,y; A(int a,int b):x(a),y(b){} auto operator<=>(const A&)const=default;}; A a{1,2},b{1,3}; cout<<(a<b)<<(a==b);
10
click to copy
What is the output: class A{int x; public: A(int v=0):x(v){} int get()const{return x;} A operator+(const A& o)const{return A(x+o.x);} A operator*(const A& o)const{return A(x*o.x);}}; A a(2),b(3),c(4); cout<<(a+b*c).get();
14
click to copy
What is the output: struct Point{int x=0,y=0; Point& move(int dx,int dy){x+=dx;y+=dy;return *this;}}; Point p; p.move(1,2).move(3,4); cout<<p.x<<p.y;
46
click to copy
What is the output: class A{int x; public: A(int v):x(v){} auto operator*(const A& o)const{return A(x*o.x);} int get()const{return x;}}; A a(2); auto r=a*a*a; cout<<r.get();
8
click to copy
What is the output: class Ratio{int n,d; public:Ratio(int a,int b):n(a),d(b){} double val()const{return (double)n/d;}}; Ratio r(3,4); cout<<r.val();
0.75
click to copy
What is the output: class A{int x=5; public:int get()const{return x;} friend bool eq(A a,A b){return a.x==b.x;}}; A a,b; cout<<eq(a,b);
1
click to copy
What is the output: class A{public:int x=0; auto operator[](int i)->int&{x=i;return x;}}; A a; a[5]=10; cout<<a.x;
10
click to copy
What is the output: class A{public:vector<int>v; void push(int x){v.push_back(x);} int sum()const{return accumulate(v.begin(),v.end(),0);}}; A a; a.push(1);a.push(2);a.push(3); cout<<a.sum();
6
click to copy
'What is the output: class A{static int count; public:A(){count++;} ~A(){count (1, 6, 78, 8, 4, 'What is the output: class A{public:int x=10; A* self(){return this;}}; A a; cout<<a.self()->x;', 'this pointer return?', '10', '10', 'Compile error', 'Compile error', 'Undefined', 'Undefined', '0', '0', NULL, 'option_a', 'self() returns this; ->x=10. Output: 10.', 0, '2026-05-25', '2026-05-25'), (1, 6, 78, 8, 4, 'What is the output: class A{public:int x; A(int v):x(v){} bool isPositive()const{return x>0;} bool isEven()const{return x%2==0;}}; A a(4); cout<<a.isPositive()<<a.isEven();', 'Const methods?', '11', '11', '10', '10', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_a', '4>0=1; 4%2==0=1. Output: 11.', 0, '2026-05-25', '2026-05-25'), (1, 6, 78, 8, 4, 'What is the output: class A{int x=5; public:operator string()const{return to_string(x);}}; A a; string s=a; cout<<s.size()<<s;', 'Conversion to string operator?', '15', '15', '5', '5', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_a', 'to_string(5)="5"; size=1. Output: 15.', 0, '2026-05-25', '2026-05-25'), (1, 6, 78, 8, 4, 'What is the output: class A{public:int x; constexpr A(int v):x(v){} constexpr int sq()const{return x*x;}}; constexpr A a(4); constexpr int r=a.sq(); cout<<r;', 'constexpr method?', '16', '16', '4', '4', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_a', 'a.sq()=16. Output: 16.', 0, '2026-05-25', '2026-05-25'), (1, 6, 78, 8, 4, 'What is the output: class A{public:int x=0; void f(int v=x){}};', 'Default arg referring to member?', 'Compile error', 'Compile error', 'Works', 'Works', 'Undefined', 'Undefined', '0', '0', NULL, 'option_a', 'Default argument cannot refer to non-static member. Compile error.', 0, '2026-05-25', '2026-05-25'), (1, 6, 78, 8, 4, 'What is the output: class A{public: int x; A(int v):x(v){} friend ostream& operator<<(ostream&os,const A&a){return os<<a.x;}}; A a(42); cout<<a;', 'Friend stream operator?', '42', '42', 'Compile error', 'Compile error', 'Undefined', 'Undefined', '0', '0', NULL, 'option_a', 'Friend operator<< outputs x=42. Output: 42.', 0, '2026-05-25', '2026-05-25'), (1, 6, 78, 8, 4, 'What is the output: class A{public:int x; A(int v):x(v){} A& operator++(){++x;return *this;} A operator++(int){A t=*this;++x;return t;}}; A a(5); cout<<(a++).x<<a.x<<(++a).x;', 'Pre and post increment mix?', '577', '577', '567', '567', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_b', 'a++=5(a=6); a.x=6; ++a: a=7
Move with elision (C++17)?
click to copy
What is the output: class A{public:int x=10; A* self(){return this;}}; A a; cout<<a.self()->x;
10
click to copy
What is the output: class A{public:int x; A(int v):x(v){} bool isPositive()const{return x>0;} bool isEven()const{return x%2==0;}}; A a(4); cout<<a.isPositive()<<a.isEven();
11
click to copy
What is the output: class A{int x=5; public:operator string()const{return to_string(x);}}; A a; string s=a; cout<<s.size()<<s;
15
click to copy
What is the output: class A{public:int x; constexpr A(int v):x(v){} constexpr int sq()const{return x*x;}}; constexpr A a(4); constexpr int r=a.sq(); cout<<r;
16
click to copy
What is the output: class A{public:int x=0; void f(int v=x){}};
Compile error
click to copy
What is the output: class A{public: int x; A(int v):x(v){} friend ostream& operator<<(ostream&os,const A&a){return os<<a.x;}}; A a(42); cout<<a;
42
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A& operator++(){++x;return *this;} A operator++(int){A t=*this;++x;return t;}}; A a(5); cout<<(a++).x<<a.x<<(++a).x;
567
click to copy

OOP Using C++ → Constructors and Destructors 21

What is the output: class A{public:int x; A(int v):x(v){cout<<'C';} ~A(){cout<<'D';} A(A&&o):x(o.x){o.x=0;cout<<'M';}}; A f(){return A(5);} A a=f(); cout<<a.x;
C5
click to copy
What is the output: class A{public:int x=0; A(){} A(int v):x(v){} A(const A& o):x(o.x*2){}}; A a(3); A b; b=a; cout<<b.x;
3
click to copy
What is the output: class A{public:int x; A():x(0){cout<<'D';} A(int v):x(v){cout<<'P';} A(const A&o):x(o.x){cout<<'C';}}; A a=5;
P
click to copy
What is the output: class A{public:int x; A(int v=10):x(v){} ~A(){cout<<x;}}; A a,b(5),c(1);
1510
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A& operator=(int v){x=v;return *this;}}; A a(3); a=5; cout<<a.x;
5
click to copy
What is the output: class A{public:string s; A(string t):s(move(t)){}}; A a("hello"); cout<<a.s;
hello
click to copy
What is the output: class A{public:int x,y; A(int a,int b):x(a),y(b){} A(pair<int,int>p):A(p.first,p.second){}}; A a({3,4}); cout<<a.x<<a.y;
34
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(const A& o)=delete;}; A f(){return A(5);} A a=f(); cout<<a.x;
5
click to copy
What is the output: class A{public:int x=0; ~A(){x=-1;} void print(){cout<<x;}}; A* p=new A; p->x=5; p->print(); delete p; cout<<p->x;
5 then UB
click to copy
What is the output: class A{public:int x; A(int v):x(v){cout<<v;} ~A(){cout<<-x;}}; A a(1),b(2),c(3);
123-3-2-1
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(A&& o)noexcept:x(o.x){o.x=0;}}; vector<A>v; v.reserve(1); v.emplace_back(5); v.emplace_back(10); cout<<v[0].x<<v[1].x;
510
click to copy
What is the output: class A{int* p; public:A(int v):p(new int(v)){} ~A(){delete p;} A(const A& o):p(new int(*o.p)){} int get()const{return *p;}}; A a(5); A b=a; *b.p=10; cout<<a.get()<<b.get();
510
click to copy
What is the output: class A{public:int x; A():x(0){cout<<'0';} A(int v):x(v){cout<<'I';} ~A(){cout<<'D';}}; A arr[3];
000DDD
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A operator+(const A& o){return A(x+o.x);} A& operator+=(const A& o){x+=o.x;return *this;}}; A a(3),b(4); a+=b; cout<<a.x;
7
click to copy
What is the output: class A{public:int x; A(int v):x(v){} A(A&& o):x(exchange(o.x,0)){}}; A a(5); A b=move(a); cout<<a.x<<b.x;
05
click to copy
What is the output: class A{public:vector<int>v; A(initializer_list<int>il):v(il){}}; A a={1,2,3,4,5}; cout<<a.v.size()<<a.v[2];
53
click to copy
What is the output: class A{int x; public:A(int v):x(v){} friend bool operator<(const A& a,const A& b){return a.x<b.x;}}; A arr[]={A(3),A(1),A(4),A(2)}; sort(begin(arr),end(arr)); cout<<arr[0].x<<arr[3].x;
14
click to copy
What is the output: class A{public:int x; explicit A(int v):x(v){}}; A a=A(5); cout<<a.x;
5
click to copy
What is the output: class A{public:int x=42; A()=default;}; A a{}; cout<<a.x;
42
click to copy
What is the output: class A{public:int x,y; A(int a,int b):y(b),x(a+y){}}; A a(1,2); cout<<a.x<<a.y;
Undefined behavior
click to copy
What is the output: class A{public:int x; A(int v):x(v){} auto clone(){return A(x);}}; A a(5); auto b=a.clone(); b.x=10; cout<<a.x<<b.x;
510
click to copy