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++ → Control Statements 13

What is the output: for(int i=1;i<=5;i++) cout<<(i%2?"*":"_");
*_*_*
click to copy
What is the output: int x=5,y=0; while(x>0){y=y*10+x%10;x/=10;} cout<<y;
5
click to copy
'What is the output: for(int i=0;i<3;i++) {for(int j=2;j>=0;j (1, 6, 74, 4, 4, 'What is the output: int x=0; for(int i=0;i<10;i+=3) x++; cout<<x;', 'Steps of 3 from 0 to 9?', '4', '4', '3', '3', 'Compile error', 'Compile error', 'Undefined', 'Undefined', NULL, 'option_a', 'i=0
6
click to copy
What is the output: int x=0; for(int i=0;i<10;i+=3) x++; cout<<x;
4
click to copy
What is the output: int x=255; while(x>0){cout<<(x&1); x>>=1;}
11111111
click to copy
What is the output: int s=0; for(int i=1;i<=100;i++) {if(i%5==0) s+=i; if(s>50) break;} cout<<s;
55
click to copy
What is the output: for(int i=1;i<=5;i++) cout<<i<<(i<5?", ":"");
1, 2, 3, 4, 5
click to copy
What is the output: int x=5; if(x>0&&x<10) cout<<'M'; else if(x>=10) cout<<'H'; else cout<<'L';
M
click to copy
What is the output: for(int i=0;i<5;i++) {if(i<2) continue; if(i>3) break; cout<<i;}
23
click to copy
What is the output: int x=2,count=0; while(x<=100){x*=x; count++;} cout<<count;
2
click to copy
What is the output: int n=12,d=0; while(n>0){d++;n/=10;} cout<<d;
2
click to copy
What is the output: int x=100,n=0; do{x=x/2+x%2; n++;}while(x>1); cout<<n<<x;
71
click to copy
What is the output: for(int i=1;i<6;i++) cout<<(i==3?string(3,'*'):to_string(i));
12***45
click to copy

OOP Using C++ → Functions 27

What is the output: int f(int x,int y){return x%y==0?x/y:f(x,y-1);} cout<<f(12,5);
3
click to copy
What is the output: auto f=[](int n)->string{return n==0?"zero":n>0?"pos":"neg";}; cout<<f(5)<<f(0)<<f(-3);
Compile error
click to copy
What is the output: int f(int a,int b,int c){return a>b?(a>c?a:c):(b>c?b:c);} cout<<f(3,7,5);
7
click to copy
What is the output: template<typename T> bool all(vector<T>v,function<bool(T)>p){for(T x:v) if(!p(x)) return false; return true;} cout<<all<int>({2,4,6},[](int x){return x%2==0;});
1
click to copy
What is the output: int f(int n){int r=0; while(n>0){r=r*10+n%10;n/=10;} return r;} cout<<f(1234);
4321
click to copy
What is the output: int f(int n){return n==0?0:n%10+f(n/10);} cout<<f(12345);
15
click to copy
What is the output: bool f(string s){int n=s.size();for(int i=0;i<n/2;i++)if(s[i]!=s[n-1-i])return false;return true;} cout<<f("racecar")<<f("hello");
10
click to copy
What is the output: int f(vector<int>&v,int l,int r){if(l>r)return -1;int m=(l+r)/2;return v[m]<5?f(v,m+1,r):v[m]==5?m:f(v,l,m-1);} vector<int>v={1,2,3,4,5,6,7}; cout<<f(v,0,6);
4
click to copy
What is the output: int f(int x){return x<=1?x:f(x-1)+f(x-2);} int g(int n){int s=0;for(int i=0;i<n;i++)s+=f(i);return s;} cout<<g(7);
20
click to copy
What is the output: string f(int n){if(n==0)return "0";string r="";while(n){r=char('0'+n%2)+r;n/=2;} return r;} cout<<f(10);
1010
click to copy
What is the output: int f(int a,int b){return a>b?a-b:b-a;} cout<<f(7,3)+f(3,7);
8
click to copy
What is the output: auto curry=[](int a){return [a](int b){return [a,b](int c){return a+b+c;};};};cout<<curry(1)(2)(3);
6
click to copy
What is the output: int f(int n,int d=10){return n<d?n:f(n-d,d)+d;} cout<<f(37);
7
click to copy
What is the output: int count_if_positive(vector<int>v){return count_if(v.begin(),v.end(),[](int x){return x>0;});} cout<<count_if_positive({-1,2,-3,4,5});
3
click to copy
What is the output: int f(int x){return x*(x-1)/2;} cout<<f(5)+f(4);
16
click to copy
What is the output: auto f=[](auto v){return accumulate(v.begin(),v.end(),0);}; cout<<f(vector{1,2,3,4,5});
15
click to copy
What is the output: int f(int n){if(n<=0)return 0;return f(n-1)*2+(n%2);}cout<<f(5);
10
click to copy
What is the output: auto memoise=[](auto f){map<int,int> c; return [=](int n)mutable{if(!c.count(n))c[n]=f(n); return c[n];};}; auto sq=memoise([](int n){return n*n;}); cout<<sq(5)<<sq(6);
2536
click to copy
What is the output: vector<int> f(int n){vector<int>r;for(int i=1;i<=n;i++)if(n%i==0)r.push_back(i);return r;} auto v=f(12); cout<<v.size()<<v.back();
612
click to copy
What is the output: int f(int x,int y){return x==0?0:f(x-1,y)+y;} cout<<f(3,5);
15
click to copy
What is the output: auto twice=[](auto f){return [=](auto x){return f(f(x));};}; cout<<twice([](int x){return x+3;})(5);
11
click to copy
What is the output: int f(int n){return n<2?n:f(n-2)+f(n-3);} cout<<f(7);
4
click to copy
What is the output: int f(int a[],int n,int i=0){return i==n?0:a[i]+f(a,n,i+1);} int a[]={1,2,3,4,5}; cout<<f(a,5);
15
click to copy
What is the output: int f(int n){return n==0?1:n==1?1:f(n-1)+f(n-2);} auto v=vector<int>{}; for(int i=0;i<6;i++) v.push_back(f(i)); cout<<v[5];
5
click to copy
What is the output: string f(int n){if(n<=0)return "";return f(n-1)+char('a'+n-1);} cout<<f(5);
abcde
click to copy
What is the output: int f(int x,int hi=100){return x>=hi?hi:f(x*2,hi);} cout<<f(7);
112
click to copy
What is the output: int f(int n){return n<=0?0:(n%2)+f(n/2);} cout<<f(15);
4
click to copy