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: cout<<(0b1010 ^ 0b1100);
6
click to copy
The output of: cout<<~0;
-1
click to copy
What is the output: int x=0x1234; cout<<((x>>8)&0xFF)<<" "<<(x&0xFF);
18 52
click to copy
The output of: float f=3.14f; int i=*(int*)&f; is:
A specific integer bit pattern (type punning)
click to copy
Which is correct for checking if number is even without %?
All equivalent
click to copy
The output of: int a=3,b=3; cout<<(a==b && !(a^b));
1
click to copy
To multiply by 4 using bit shift:
n<<2 (left shift by 2 = multiply by 2^2=4)
click to copy
To divide by 8 using bit shift:
n>>3 (right shift by 3 = divide by 2^3=8)
click to copy
The output of: int x=100; cout<<(x/10)<<(x%10);
100
click to copy
The output of: string s=""; for(int i=65;i<70;i++) s+=(char)i; cout<<s;
ABCDE
click to copy
Which OOP concept is violated when a class has public data members?
Encapsulation (data hiding)
click to copy
What is cohesion in software design?
Degree to which a class/module has a single, focused purpose
click to copy
What is coupling in software design?
Degree of interdependence between modules (lower is better)
click to copy
High cohesion and low coupling is:
Good design (focused modules, minimal interdependence)
click to copy
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(48,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(2,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