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 36

What is the output: template<typename T> bool isNull(T* p){return p==nullptr;} int x=5; cout<<isNull(&x)<<isNull((int*)nullptr);
01
click to copy
What is the output: template<typename T> struct Remove{using type=T;}; template<typename T> struct Remove<T*>{using type=T;}; cout<<is_same_v<Remove<int*>::type,int>;
1
click to copy
What is the output: template<typename T> T sumArr(T* arr,int n){T s{}; for(int i=0;i<n;i++) s+=arr[i]; return s;} int a[]={1,2,3,4,5}; cout<<sumArr(a,5);
15
click to copy
What is the output: template<class T> struct IsVoid{static const bool v=false;}; template<> struct IsVoid<void>{static const bool v=true;}; cout<<IsVoid<void>::v<<IsVoid<int>::v;
10
click to copy
What is the output: template<typename T> T power(T base,int exp){return exp?base*power(base,exp-1):1;} cout<<power(2,10);
1024
click to copy
What is the output: template<int N,int M> struct GCD{static const int v=GCD<M,N%M>::v;}; template<int N> struct GCD<N,0>{static const int v=N;}; cout<<GCD<48,18>::v;
6
click to copy
What is the output: template<typename T> void swap2(T& a,T& b){T t=a;a=b;b=t;} int x=3,y=7; swap2(x,y); cout<<x<<y;
73
click to copy
What is the output: template<typename T> T abs2(T x){return x<0?-x:x;} cout<<abs2(-5)<<abs2(-3.14);
53.14
click to copy
What is the output: template<typename T> int sign(T x){return x>0?1:(x<0?-1:0);} cout<<sign(-5)<<sign(0)<<sign(3);
-101
click to copy
What is the output: template<typename T> class Wrapper{T v; public: Wrapper(T x):v(x){} T get(){return v;} Wrapper operator+(Wrapper o){return Wrapper(v+o.v);}}; Wrapper<int>a(3),b(4); cout<<(a+b).get();
7
click to copy
What is the output: template<typename T> bool between(T v,T lo,T hi){return v>=lo&&v<=hi;} cout<<between(5,1,10)<<between(15,1,10);
10
click to copy
What is the output: template<std::integral T> T addOne(T x){return x+1;} cout<<addOne(5);
6
click to copy
What is the output: template<typename T> struct TypeOf{static string name(){return "unknown";}}; template<> struct TypeOf<int>{static string name(){return "int";}}; cout<<TypeOf<int>::name()<<TypeOf<double>::name();
intunknown
click to copy
What is the output: template<typename T,int N> struct Array{T d[N]; int size(){return N;} T& at(int i){return d[i];}}; Array<int,3> a; a.at(0)=1;a.at(1)=2;a.at(2)=3; cout<<a.size()<<a.at(1);
32
click to copy
What is the output: template<typename T> T myMax(initializer_list<T> il){return *max_element(il.begin(),il.end());} cout<<myMax({3,1,4,1,5,9,2,6});
9
click to copy
What is the output: template<typename... Args> auto product(Args... args){return (args*...);} cout<<product(1,2,3,4,5);
120
click to copy
What is the output: template<typename T> T fromString(const string& s); template<> int fromString<int>(const string& s){return stoi(s);} cout<<fromString<int>("42");
42
click to copy
What is the output: template<bool B,typename T,typename F> struct Cond{using type=T;}; template<typename T,typename F> struct Cond<false,T,F>{using type=F;}; cout<<is_same_v<Cond<true,int,double>::type,int>;
1
click to copy
What is the output: template<typename T> struct Decay{using type=remove_cvref_t<T>;}; cout<<is_same_v<Decay<const int&>::type,int>;
1
click to copy
What is the output: template<typename T> void f(T){cout<<'T';} template<> void f<int>(int){cout<<'I';} f(1); f(1.0); f('a');
ITT
click to copy
What is the output: template<typename T> auto makeVec(T a,T b,T c)->vector<T>{return {a,b,c};} auto v=makeVec(1,2,3); cout<<v[0]+v[2];
4
click to copy
What is the output: template<typename T> struct Twice{using type=pair<T,T>;}; Twice<int>::type p={3,4}; cout<<p.first<<p.second;
34
click to copy
What is the output: template<size_t N> constexpr auto makeArray(){array<int,N> a{}; for(size_t i=0;i<N;i++) a[i]=i*i; return a;} constexpr auto a=makeArray<5>(); cout<<a[3];
9
click to copy
What is the output: template<typename T> class Optional{bool has=false; T v; public: void set(T x){has=true;v=x;} bool hasVal(){return has;} T get(){return v;}}; Optional<int> o; cout<<o.hasVal(); o.set(5); cout<<o.hasVal()<<o.get();
015
click to copy
What is the output: template<typename T,typename Pred> int countIf(vector<T>& v,Pred p){int n=0;for(auto x:v) if(p(x)) n++;return n;} vector<int>v={1,2,3,4,5}; cout<<countIf(v,[](int x){return x%2==0;});
2
click to copy
What is the output: template<typename T> T lerp(T a,T b,double t){return a+(b-a)*t;} cout<<lerp(0.0,10.0,0.5);
5
click to copy
What is the output: template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;} cout<<gcd(36,48);
12
click to copy
What is the output: template<typename T> vector<T> filter(vector<T> v,function<bool(T)> p){vector<T>r;for(T x:v)if(p(x))r.push_back(x);return r;} auto r=filter<int>({1,2,3,4,5},[](int x){return x>3;}); cout<<r.size();
2
click to copy
What is the output: template<typename T> T reduce(vector<T> v,T init,function<T(T,T)> f){for(T x:v) init=f(init,x);return init;} cout<<reduce<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> T myMin(T a,T b){return a<b?a:b;} template<typename T,typename...Rest> T myMin(T a,Rest...rest){return myMin(a,myMin(rest...));} cout<<myMin(5,3,8,1,4);
1
click to copy
What is the output: template<typename T> class Singleton{static T* inst; public: static T& get(){if(!inst) inst=new T; return *inst;} Singleton()=delete;}; template<typename T> T* Singleton<T>::inst=nullptr; struct Config{int x=42;}; cout<<Singleton<Config>::get().x;
42
click to copy
What is the output: template<int...Ns> constexpr int sumNT=(...+Ns); cout<<sumNT<1,2,3,4,5>;
15
click to copy
What is the output: template<typename T> class TypeList{}; cout<<is_same_v<TypeList<int>,TypeList<int>>;
1
click to copy
What is the output: template<typename T> T constexprMax(T a,T b){return a>b?a:b;} constexpr int m=constexprMax(3,7); cout<<m;
7
click to copy
What is the output: template<typename T> struct IsIntegral{static const bool v=false;}; template<> struct IsIntegral<int>{static const bool v=true;}; template<> struct IsIntegral<long>{static const bool v=true;}; cout<<IsIntegral<int>::v<<IsIntegral<double>::v<<IsIntegral<long>::v;
101
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 v=zip<int>({1,2,3},{4,5,6}); cout<<v[1].first<<v[1].second;
25
click to copy

OOP Using C++ → Introduction to C++ 4

What is the output: int x=5; cout<<(x*x-1)%(x+1);
0
click to copy
What is the output: int x=5; cout<<(x+(x=10));
Undefined behavior
click to copy
What is 'std::execution::par' policy?
Parallel algorithm execution policy (C++17)
click to copy
What is the output: cout<<numeric_limits<int>::min()+numeric_limits<int>::max();
-1
click to copy