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

A God class (class that does everything) violates:
Single Responsibility Principle
click to copy
What is composition over inheritance principle?
Prefer building objects from components rather than inheriting
click to copy
Law of Demeter (Principle of Least Knowledge) says:
Object should only talk to immediate friends (not friends of friends)
click to copy
Which is an anti-pattern?
Singleton overuse, God class, Copy-paste programming
click to copy
The output of: class C{int x; public: C():x(0){} C(int x):x(x){} void print(){cout<<x;} }; C c1,c2(5); c1.print(); c2.print();
05
click to copy
The output of: class A{public:int x; A(int x):x(x){}}; A a(5); cout<<a.x;
5
click to copy
The output of: int f(int n){return n<=1 ? 1 : n*f(n-1);} cout<<f(5);
120
click to copy
The output of: int f(int n){return n<=1 ? n : f(n-1)+f(n-2);} cout<<f(7);
13
click to copy
The output of: int gcd(int a,int b){return b==0?a:gcd(b,a%b);} cout<<gcd(18);
6
click to copy
The output of: int power(int b,int e){return e==0?1:b*power(b,e-1);} cout<<power(8);
256
click to copy
Fast power (exponentiation by squaring) computes a^n in:
O(log n)
click to copy
The output of: int arr[]={5,3,8,1,9,2}; sort(arr,arr+6); cout<<arr[0]<<" "<<arr[5];
1 9
click to copy
The output of: int arr[]={1,2,3,4,5}; cout<<*max_element(arr,arr+5)<<" "<<*min_element(arr,arr+5);
5 1
click to copy
The output of: string s="Hello World"; transform(s.begin(),s.end(),s.begin(),toupper); cout<<s;
HELLO WORLD
click to copy
The output of: vector<int> v={1,2,3}; v.resize(5); cout<<v[4];
0
click to copy
The output of: string s="abcabc"; cout<<count(s.begin(),s.end(),a);
2
click to copy
The output of: cout<<boolalpha<<(3>2)<<" "<<(2>3);
true false
click to copy
What is the output: int x=42; cout<<hex<<x<<" "<<dec<<x;
2a 42
click to copy
The output of: int a[]={1,2,3,4,5}; cout<<sizeof(a)/sizeof(*a);
5
click to copy
The output of: vector<int> v={3,1,4,1,5,9}; sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); cout<<v.size();
5
click to copy
What is the output: map<int,int> m={{1,10},{2,20},{3,30}}; for(auto [k,v]:m) cout<<k<<v;
102030
click to copy
The output of: auto v=vector<int>(5); iota(v.begin(),v.end(),1); cout<<v[3];
4
click to copy
The output of: int n=10; int sum=accumulate(begin({1,2,3,4,5,6,7,8,9,10}),end({1,2,3,4,5,6,7,8,9,10}),0); cout<<sum;
55
click to copy
The output of: string s="Race Car"; remove_if(s.begin(),s.end(),::isspace); cout<<s;
Race Car
click to copy
The output of: auto s=set<int>{5,3,1,4,2}; cout<<distance(s.begin(),s.end());
5
click to copy
The output of: int x=7; cout<<(x%2==0?"Even":"Odd")<<" "<<(x>0?"Pos":"NonPos");
Odd Pos
click to copy
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