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

What is the output: int x=0; do {x++; if(x==3) break;} while(x<10); cout<<x;
3
click to copy
The output of: int a=2; switch(a){case 1: case 2: cout<<"1or2"; break; case 3: cout<<"3";}
1or2
click to copy
The output of: int x=5; if(x>0) if(x>3) cout<<"A"; else cout<<"B"; else cout<<"C";
A
click to copy
The output of: int x=1; if(x>0) if(x>3) cout<<"A"; else cout<<"B"; else cout<<"C";
B
click to copy
The output of: int x=-1; if(x>0) if(x>3) cout<<"A"; else cout<<"B"; else cout<<"C";
C
click to copy
The output of: int i=0; for(;;){if(i++>=3) break; cout<<i;} cout<<i;
1235
click to copy
What is the output: int n=1234; int rev=0; while(n){rev=rev*10+n%10; n/=10;} cout<<rev;
4321
click to copy
The output of: int n=121; bool isPalin=true; int tmp=n,rev=0; while(tmp){rev=rev*10+tmp%10;tmp/=10;} cout<<(rev==n?"Yes":"No");
Yes
click to copy
The output of: int n=123; int sum=0; while(n){sum+=n%10; n/=10;} cout<<sum;
6
click to copy
The output of: int n=153; int sum=0,tmp=n; while(tmp){int d=tmp%10; sum+=d*d*d; tmp/=10;} cout<<(sum==n?"Armstrong":"Not");
Armstrong
click to copy
The output of: int n=6; int sum=0; for(int i=1;i<n;i++) if(n%i==0) sum+=i; cout<<(sum==n?"Perfect":"Not");
Perfect
click to copy
What is the output: int x=10; void* p=&x; cout<<*(int*)p;
10
click to copy
The output of: int arr[]={1,2,3,4,5}; int *p=arr; cout<<p[2];
3
click to copy
The output of: int x=5; int &r=x; int &s=r; s=10; cout<<x;
10
click to copy
What is the output: int a=10; { int a=20; cout<<a; } cout<<a;
2010
click to copy
The output of: int a=5; { int a=10; cout<<::a<<a; } cout<<a;
5105
click to copy
What is the output: class C{public:static int n; C(){n++;}}; int C::n=0; C a,b,c; cout<<C::n;
3
click to copy
The output of: class C{int x; public: void set(int a){x=a;} int get(){return x;} }; C c; c.set(42); cout<<c.get();
42
click to copy
The output of: class A{public:int f(){return 1;}}; class B:public A{public:int f(){return 2;}}; B b; cout<<b.f()<<b.A::f();
21
click to copy
The output of: struct Point{int x,y;}; Point p={3,4}; cout<<p.x<<" "<<p.y;
3 4
click to copy
The output of: union U{int i; float f; }; U u; u.i=10; cout<<u.i;
10
click to copy
The output of: enum Color{Red=1,Green=2,Blue=4}; cout<<(Red|Blue);
5
click to copy
The output of: enum class Day{Mon=1,Tue,Wed}; cout<<(int)Day::Wed;
3
click to copy
The output of: int x=5; auto f=[x]()mutable{return ++x;}; cout<<f()<<f()<<x;
675
click to copy
The output of: auto v=vector{1,2,3,4,5}; auto sum=reduce(v.begin(),v.end()); cout<<sum;
15
click to copy
The output of: vector<int> v={1,2,3,4,5}; auto r=v|views::filter([](int x){return x>3;})| views::transform([](int x){return x*x;}); for(auto x:r)cout<<x<<" ";
16 25
click to copy
The output of: using namespace std; auto gcd=[](int a,int b){while(b){a%=b; swap(a,b);} return a;}; cout<<gcd(56,98);
14
click to copy
The output of: int dp[6]={0}; dp[0]=1; for(int i=1;i<=5;i++) for(int j=i;j<=5;j++) dp[j]+=dp[j-i]; cout<<dp[5];
7
click to copy
The output of: string s="Hello World"; cout<<s.size()-s.find(" ")-1;
5
click to copy
The output of: vector<int> v={1,2,3,4,5}; int prod=1; for_each(v.begin(),v.end(),[&](int x){prod*=x;}); cout<<prod;
120
click to copy
The output of: int n=100; cout<<(int)log2(n)+1;
7
click to copy
The output of: int x=0b10110; cout<<__builtin_popcount(x);
3
click to copy
What is the time complexity to find if a number is prime (trial division)?
O(sqrt(n))
click to copy
Sieve of Eratosthenes finds all primes up to n in:
O(n log log n)
click to copy
The output of: int n=36; cout<<(int)sqrt(n)*(int)sqrt(n)==n?"Perfect Square":"Not";
Perfect Square
click to copy
The number of digits in integer n is:
floor(log10(n))+1
click to copy
The output of: cout<<(int)(log10(12345))+1;
5
click to copy
What is Eulers totient function phi(6)?
2
click to copy
The output of: int a=2,b=5,m=7; int result=1; while(b>0){if(b&1)result=result*a%m; a=a*a%m; b>>=1;} cout<<result;
4
click to copy
Modular arithmetic: (a+b) mod m equals:
(a mod m + b mod m) mod m
click to copy