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.

Data Structures and Algorithms → Introduction to DSA 40

The output of: class A{public:virtual int f(){return 1;}}; class B:public A{public:int f()override{return 2;}}; class C:public B{public:int f()override{return 3;}}; A*p=new C; cout<<p->f();
3
click to copy
The output of: class A{public:int f(){return 1;} virtual int g(){return 10;}}; class B:public A{public:int f(){return 2;} int g()override{return 20;}}; A*p=new B; cout<<p->f()<<p->g();
1 20
click to copy
The output of: template<class T> T min3(T a,T b,T c){return a<b?(a<c?a:c):(b<c?b:c);} cout<<min3(1,2);
1
click to copy
The output of: vector<int> v={5,2,8,1,9}; auto comp=[](int a,int b){return a>b;}; sort(v.begin(),v.end(),comp); cout<<v[0];
9
click to copy
The output of: map<int,string> m={{1,"one"},{2,"two"},{3,"three"}}; for(auto &[k,v]:m) cout<<v[0]; (first char of each value)
ott
click to copy
The output of: auto v=vector{3,1,4,1,5,9,2,6}; ranges::sort(v); cout<<v[0]<<v[7];
19
click to copy
The output of: auto r=views::iota(1)|views::take(5); for(auto x:r)cout<<x;
12345
click to copy
The output of: int n=100; auto isprime=[](int n){for(int i=2;i*i<=n;i++) if(n%i==0)return false; return n>1;}; auto primes=views::iota(2)|views::filter(isprime)|views::take(5); cout<<ranges::distance(primes);
5
click to copy
The output of: expected<int,string> e=42; cout<<e.value();
42
click to copy
The output of: expected<int,string> e=unexpected("error"); cout<<e.has_value();
0
click to copy
What is the output: int x=5; auto f=[x=move(x)](){return x;}; cout<<f()<<x;
50
click to copy
The output of: string s1="Hello"; string s2=move(s1); cout<<s1.size()<<s2.size();
0 5
click to copy
The output of: vector<int> v1={1,2,3}; vector<int> v2=move(v1); cout<<v1.size()<<v2.size();
0 3
click to copy
The output of: unique_ptr<int> p=make_unique<int>(42); unique_ptr<int> q=move(p); cout<<(p?"has":"null")<<(q?"has":"null");
nullhas
click to copy
The output of: shared_ptr<int> sp=make_shared<int>(10); { shared_ptr<int> sp2=sp; cout<<sp.use_count(); } cout<<sp.use_count();
2 1
click to copy
What is the output: int x=5; decltype(x) y=10; cout<<y;
10
click to copy
The output of: auto f(int x,int y)->int{return x+y;} cout<<f(4);
7
click to copy
The output of: int x=10; int y=[&]{return x*2;}(); cout<<y;
20
click to copy
The output of: constexpr auto factorial=[](auto self,int n)->int{return n<=1?1:n*self(self,n-1);}; cout<<factorial(factorial,6);
720
click to copy
The output of: struct A{int x; auto getX(){return x;}}; A a{42}; cout<<a.getX();
42
click to copy
The output of: int arr[]={1,2,3,4,5}; span<int> s(arr,3); cout<<s[2];
3
click to copy
The output of: span<int> s={1,2,3,4,5}; cout<<s.front()<<" "<<s.back();
1 5
click to copy
The output of: array<int,5> a={5,3,1,4,2}; sort(a.begin(),a.end()); cout<<a[0]<<a[4];
15
click to copy
The output of: map<int,int> m={{1,1},{2,4},{3,9}}; auto it=m.lower_bound(2); cout<<it->first<<it->second;
24
click to copy
The output of: set<int> s={1,3,5,7,9}; auto it=s.upper_bound(5); cout<<*it;
7
click to copy
The output of: multiset<int> ms={1,2,2,3,3,3}; cout<<ms.count(3);
3
click to copy
The output of: multimap<int,string> mm; mm.insert({1,"a"}); mm.insert({1,"b"}); mm.insert({2,"c"}); cout<<mm.count(1);
2
click to copy
The output of: int x=42; const int &cr=x; x=100; cout<<cr;
100
click to copy
The output of: const int x=5; int *p=const_cast<int*>(&x); *p=10; cout<<x;
5 (modifying const is UB)
click to copy
The output of: class A{public:void f()const{cout<<"const";}void f(){cout<<"non-const";}}; const A a; a.f();
const
click to copy
The output of: class A{public:void f()const{cout<<"C";}void f(){cout<<"N";}}; A a; a.f();
N
click to copy
The output of: int arr[5]; fill(arr,arr+5,7); cout<<arr[0]<<arr[4];
77
click to copy
The output of: vector<int> v={1,2,3}; v.assign(0); cout<<v.size()<<v[0];
50
click to copy
The output of: list<int> l={1,2,3,4,5}; l.remove(3); cout<<l.size();
4
click to copy
The output of: list<int> l={3,1,4,1,5}; l.sort(); l.unique(); cout<<l.size();
4
click to copy
The output of: vector<int> v={1,2,3,4,5}; auto r=remove_if(v.begin(),v.end(),[](int x){return x%2==0;}); v.erase(r,v.end()); cout<<v.size();
3
click to copy
The output of: forward_list<int> fl={1,2,3}; fl.push_front(0); cout<<fl.front();
0
click to copy
forward_list vs list in C++:
forward_list is singly-linked (no backward traversal, less memory)
click to copy
The output of: auto r=views::iota(6)|views::transform([](int x){return x*x;})|views::take(3); for(auto x:r)cout<<x<<" ";
1 4 9
click to copy
The output of: int n=2; while(n<=100){cout<<n<<" "; n*=2;}
2 4 8 16 32 64
click to copy