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++ → Templates 7

What is the output: template<typename T> T clamp2(T v,T lo,T hi){return max(lo,min(v,hi));} cout<<clamp2(15,1,10)<<clamp2(-5,0,5);
100
click to copy
What is the output: template<int N> void printN(){cout<<N<<' '; if constexpr(N>1) printN<N-1>();} printN<5>();
5 4 3 2 1
click to copy
What is the output: template<typename T> auto makeRange(T start,T end,T step){vector<T>v;for(T i=start;i<end;i+=step)v.push_back(i);return v;} auto r=makeRange(0,10,3); cout<<r.size()<<r.back();
49
click to copy
What is the output: template<typename T> bool isPalindrome(vector<T>v){return equal(v.begin(),v.begin()+v.size()/2,v.rbegin());} cout<<isPalindrome<int>({1,2,3,2,1})<<isPalindrome<int>({1,2,3});
10
click to copy
What is the output: template<typename T> T fromBinary(string s){T n=0;for(char c:s) n=n*2+(c-'0');return n;} cout<<fromBinary<int>("1010");
10
click to copy
What is the output: template<typename K,typename V> class Bimap{map<K,V>fwd;map<V,K>bwd; public:void insert(K k,V v){fwd[k]=v;bwd[v]=k;} V get(K k){return fwd[k];} K rget(V v){return bwd[v];}}; Bimap<int,string>m; m.insert(1,"one"); cout<<m.get(1)<<m.rget("one");
one1
click to copy
What is the output: template<std::size_t N,typename T> struct NTuple{array<T,N>d; NTuple(initializer_list<T>il){copy(il.begin(),il.begin()+N,d.begin());} T& get(size_t i){return d[i];}}; NTuple<3,int>t={1,2,3}; cout<<t.get(0)+t.get(2);
4
click to copy

OOP Using C++ → Introduction to C++ 33

What is the output: int x=5; cout<<(x<<1)-(x>>1);
8
click to copy
What is the output: auto x=5; auto y=2; cout<<(x/y)<<(x%y);
21
click to copy
What is the output: constexpr int arr[]={1,2,3,4,5}; constexpr int s=arr[0]+arr[4]; cout<<s;
6
click to copy
What is the output: int x=0b1010; int y=0b0101; cout<<(x+y);
15
click to copy
What is the output: string s="hello"; cout<<s.length()<<s[0]<<s.back();
5he
click to copy
What is 'std::string::npos'?
Maximum value of size_t; returned when search fails
click to copy
What is the output: string s="hello world"; cout<<s.find("world");
6
click to copy
What is the output: string s="abcde"; s.erase(1,3); cout<<s;
ae
click to copy
What is the output: string s="hello"; s.insert(2,"XY"); cout<<s;
heXYllo
click to copy
What is the output: string s="Hello World"; transform(s.begin(),s.end(),s.begin(),::tolower); cout<<s;
hello world
click to copy
What is the output: stringstream ss; ss<<1<<' '<<2<<' '<<3; int a,b,c; ss>>a>>b>>c; cout<<a+b+c;
6
click to copy
What is the output: int x=42; string s=to_string(x); cout<<s.size()<<s[0];
24
click to copy
What is the output: vector<string> v={"a","bb","ccc"}; cout<<accumulate(v.begin(),v.end(),string{});
abbccc
click to copy
What is the output: auto s=string(5,'x'); cout<<s;
xxxxx
click to copy
What is the output: string s="abcdef"; cout<<s.substr(2,3);
cde
click to copy
What is the output: int a=10,b=3; cout<<a/b<<' '<<a%b<<' '<<(double)a/b;
3 1 3.33333
click to copy
What is the output: cout<<fixed<<setprecision(3)<<(1.0/7);
0.143
click to copy
What is the output: int x=5; auto f=[]()->decltype(x){return x*2;}; cout<<f();
10
click to copy
What is the output: int x=0; auto inc=[&x](int n=1){x+=n;}; inc(); inc(5); cout<<x;
6
click to copy
What is the output: vector<int> v={1,2,3,4,5}; cout<<v.front()*v.back();
5
click to copy
What is the output: deque<int> d={1,2,3}; d.push_front(0); d.push_back(4); cout<<d.front()<<d.back()<<d.size();
045
click to copy
What is the output: map<string,int> m; m["a"]=1; m["b"]=2; m["c"]=3; cout<<m["b"]<<m.size();
23
click to copy
What is the output: set<int> s={3,1,4,1,5,9,2,6}; cout<<s.size()<<*s.begin();
71
click to copy
What is the output: unordered_map<int,int> m; for(int i=1;i<=5;i++) m[i]=i*i; cout<<m[3];
9
click to copy
What is the output: stack<int> s; s.push(1);s.push(2);s.push(3); cout<<s.top(); s.pop(); cout<<s.top();
32
click to copy
What is the output: queue<int> q; q.push(1);q.push(2);q.push(3); cout<<q.front(); q.pop(); cout<<q.front();
12
click to copy
What is the output: priority_queue<int> pq; pq.push(3);pq.push(1);pq.push(4);pq.push(2); cout<<pq.top();
4
click to copy
What is the output: list<int> l={1,2,3,4,5}; l.remove(3); cout<<l.size();
4
click to copy
What is the output: int x=5; cout<<(x>0?to_string(x):"neg");
5
click to copy
What is the output: regex r("\\\\d+"); string s="abc123def"; smatch m; regex_search(s,m,r); cout<<m[0];
123
click to copy
What is the output: auto t=make_tuple(1,string("hi"),3.14); cout<<get<0>(t)<<get<1>(t);
1hi
click to copy
What is the output: pair<int,string> p={42,"hello"}; cout<<p.first<<p.second.size();
425
click to copy
What is the output: int x=5; auto y=exchange(x,10); cout<<x<<y;
105
click to copy