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++ → Exception Handling 1

What is the output: try{try{throw 1;}catch(int i){throw string("err");}}catch(string& s){cout<<s;}
err
click to copy

OOP Using C++ → Templates 34

What is the output: template<typename T> T sum(T* arr,int n){T s=T{};for(int i=0;i<n;i++)s+=arr[i];return s;} int a[]={1,2,3,4,5}; cout<<sum(a,5);
15
click to copy
What is the output: template<typename T> bool contains(vector<T>v,T x){return find(v.begin(),v.end(),x)!=v.end();} cout<<contains<int>({1,2,3,4,5},3)<<contains<int>({1,2,3},9);
10
click to copy
What is the output: template<typename T> T median(vector<T>v){sort(v.begin(),v.end());return v[v.size()/2];} cout<<median<int>({5,3,8,1,4});
4
click to copy
What is the output: template<typename T,int N> struct Arr{T d[N]; int size(){return N;} T& operator[](int i){return d[i];}}; Arr<int,3>a{}; a[0]=1;a[1]=2;a[2]=3; cout<<a[0]+a[2];
4
click to copy
What is the output: template<typename T> T gcd2(T a,T b){return b==T{}?a:gcd2(b,a%b);} cout<<gcd2(24,36);
12
click to copy
What is the output: template<typename... Args> string join(string sep,Args... args){ostringstream oss;bool first=true;((oss<<(first?first=false,string{}:sep)<<args),...);return oss.str();} cout<<join("-",1,2,3);
1-2-3
click to copy
What is the output: template<int N> constexpr int fib=N<2?N:fib<N-1>+fib<N-2>; cout<<fib<10>;
55
click to copy
What is the output: template<typename T> auto makeVector(int n,T val)->vector<T>{return vector<T>(n,val);} auto v=makeVector(5,3); cout<<v.size()<<v[0];
53
click to copy
What is the output: template<typename T> T dotProduct(vector<T>a,vector<T>b){return inner_product(a.begin(),a.end(),b.begin(),T{});} cout<<dotProduct<int>({1,2,3},{4,5,6});
32
click to copy
What is the output: template<typename T> class Pair2{T first,second; public:Pair2(T a,T b):first(a),second(b){} T min_()const{return first<second?first:second;} T max_()const{return first>second?first:second;}}; Pair2<int>p(3,7); cout<<p.min_()<<p.max_();
37
click to copy
What is the output: template<typename T> struct TypeSize{static constexpr int v=sizeof(T);}; cout<<TypeSize<int>::v<<TypeSize<double>::v<<TypeSize<char>::v;
481
click to copy
What is the output: template<typename T> T zipAdd(vector<T>a,vector<T>b){T s=T{};for(int i=0;i<min(a.size(),b.size());i++)s+=a[i]*b[i];return s;} cout<<zipAdd<int>({1,2,3},{10,20,30});
140
click to copy
What is the output: template<typename T,typename Comp> T myMax2(T a,T b,Comp comp){return comp(a,b)?b:a;} cout<<myMax2(3,7,[](int a,int b){return a<b;});
7
click to copy
What is the output: template<auto V> struct Lit{static constexpr auto value=V;}; cout<<Lit<42>::value<<Lit<3.14>::value;
423.14
click to copy
What is the output: template<typename T> T reduce2(vector<T>v,T init,function<T(T,T)>f){return accumulate(v.begin(),v.end(),init,f);} cout<<reduce2<int>({1,2,3,4,5},0,[](int a,int b){return a+b;});
15
click to copy
What is the output: template<typename T> class Stack2{vector<T>s; public:void push(T x){s.push_back(x);} T pop(){T x=s.back();s.pop_back();return x;} T top()const{return s.back();} bool empty()const{return s.empty();}}; Stack2<int>st; st.push(1);st.push(2);st.push(3); cout<<st.pop()<<st.top();
32
click to copy
What is the output: template<typename T> bool isPalin(const vector<T>&v){return equal(v.begin(),v.begin()+v.size()/2,v.rbegin());} cout<<isPalin(vector<int>{1,2,3,2,1})<<isPalin(vector<int>{1,2,3});
10
click to copy
What is the output: template<int N> struct Pow2{static const int v=2*Pow2<N-1>::v;}; template<> struct Pow2<0>{static const int v=1;}; cout<<Pow2<10>::v;
1024
click to copy
What is the output: template<typename T> T accumulate2(T* begin,T* end,T init){while(begin!=end)init+=*begin++;return init;} int a[]={1,2,3,4,5}; cout<<accumulate2(a,a+5,0);
15
click to copy
What is the output: template<typename T> class Maybe{optional<T>v; public:Maybe():v{}{} Maybe(T x):v(x){} template<typename F> auto bind(F f)->Maybe<decltype(f(*v))>{if(!v)return {};return {f(*v)};} T orElse(T d)const{return v.value_or(d);}}; Maybe<int>m(5); auto r=m.bind([](int x){return x*2;}).bind([](int x){return x+1;}); cout<<r.orElse(0);
11
click to copy
What is the output: template<typename T> T fromStr(const string& s); template<> int fromStr<int>(const string& s){return stoi(s);} template<> double fromStr<double>(const string& s){return stod(s);} cout<<fromStr<int>("42")+fromStr<double>("3.5");
45.5
click to copy
What is the output: template<typename T> auto groupBy(vector<T>v,function<int(T)>f)->map<int,vector<T>>{map<int,vector<T>>m;for(T x:v)m[f(x)].push_back(x);return m;} auto g=groupBy<int>({1,2,3,4,5,6},[](int x){return x%3;}); cout<<g[0].size()<<g[1].size()<<g[2].size();
222
click to copy
What is the output: template<typename T> struct Reverse{using type=T;}; template<typename H,typename...Ts> struct Reverse<tuple<H,Ts...>>{using rest=typename Reverse<tuple<Ts...>>::type; using type=decltype(tuple_cat(declval<rest>(),declval<tuple<H>>()));}; cout<<is_same_v<Reverse<tuple<int,double>>::type,tuple<double,int>>;
1
click to copy
What is the output: template<typename T> struct Add1{using type=T;}; template<> struct Add1<int>{using type=long;}; template<> struct Add1<long>{using type=long long;}; cout<<is_same_v<Add1<int>::type,long><<is_same_v<Add1<char>::type,char>;
11
click to copy
What is the output: template<typename T,size_t N> T arraySum(const T(&arr)[N]){T s=T{};for(auto x:arr)s+=x;return s;} int a[]={1,2,3,4,5}; cout<<arraySum(a);
15
click to copy
What is the output: template<typename T> auto makeShared2(auto&&...args){return make_shared<T>(forward<decltype(args)>(args)...);} auto p=makeShared2<pair<int,string>>(5,"hi"); cout<<p->first<<p->second;
5hi
click to copy
What is the output: template<typename T> struct Traits{static constexpr bool isNum=false; static constexpr int bytes=sizeof(T);}; template<> struct Traits<int>{static constexpr bool isNum=true; static constexpr int bytes=4;}; cout<<Traits<int>::isNum<<Traits<int>::bytes<<Traits<string>::isNum;
140
click to copy
What is the output: template<typename T> bool isInRange(T v,T lo,T hi){return v>=lo&&v<=hi;} cout<<isInRange(5,1,10)<<isInRange(15,1,10)<<isInRange(1,1,10);
101
click to copy
What is the output: template<typename T> vector<T> range_(T start,T stop,T step=T(1)){vector<T>v;for(T i=start;i<stop;i+=step)v.push_back(i);return v;} auto r=range_(1,10,3); cout<<r.size()<<r.back();
37
click to copy
What is the output: template<typename F,int N,int...Ns> auto applyAll(F f){if constexpr(sizeof...(Ns)>0)return f(N)+applyAll<F,Ns...>(f);else return f(N);} cout<<applyAll<decltype([](int x){return x*x;}),1,2,3>([](int x){return x*x;});
Compile error
click to copy
What is the output: template<typename T> class TypeInfo{public:static string name(){return "unknown";}}; template<> class TypeInfo<int>{public:static string name(){return "int";}}; template<> class TypeInfo<double>{public:static string name(){return "double";}}; cout<<TypeInfo<int>::name()<<' '<<TypeInfo<float>::name();
int unknown
click to copy
What is the output: template<std::size_t I,typename T,typename...Ts> struct NthType{using type=typename NthType<I-1,Ts...>::type;}; template<typename T,typename...Ts> struct NthType<0,T,Ts...>{using type=T;}; cout<<is_same_v<NthType<1,int,double,char>::type,double>;
1
click to copy
What is the output: template<typename T> auto zip_(vector<T>a,vector<T>b){vector<pair<T,T>>r;for(int i=0;i<min(a.size(),b.size());i++)r.push_back({a[i],b[i]});return r;} auto r=zip_<int>({1,2,3},{10,20,30}); int s=0;for(auto[x,y]:r)s+=x*y; cout<<s;
140
click to copy
What is the output: template<bool B> struct EnableType{}; template<> struct EnableType<true>{using type=int;}; EnableType<true>::type x=42; cout<<x;
42
click to copy