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 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(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
The output of: int a=INT_MIN; cout<<(a<0?"negative":"positive");
negative
click to copy
What is the output: cout<<numeric_limits<int>::max();
All correct
click to copy
The output of: double x=1.0/3.0; cout<<fixed<<setprecision(15)<<x;
0.333333333333333
click to copy
What is epsilon comparison for floating point?
abs(a-b)<epsilon (for small epsilon)
click to copy
The output of: cout<<(0.1+0.2==0.3);
0
click to copy
Which is the output: double a=0.1+0.2; cout<<fixed<<setprecision(20)<<a;
0.30000000000000004440
click to copy
The output of: int n=5; string s(n,*); cout<<s;
*****
click to copy
The output of: string s="Hello"; s.insert(" World"); cout<<s;
Hello World
click to copy
The output of: string s="Hello World"; s.replace(5,"C++"); cout<<s;
Hello C++
click to copy
What does string::npos equal?
-1 as size_t (maximum size_t value)
click to copy
The output of: string s="hello"; for(auto &c:s) c=toupper(c); cout<<s;
HELLO
click to copy
The output of: string s="12345"; int n=stoi(s); cout<<n*2;
24690
click to copy
The output of: int n=42; string s=to_string(n); cout<<s.length();
2
click to copy
The output of: vector<string> v={"Hello","World"}; string result=accumulate(v.begin(),v.end(),string("")); cout<<result;
HelloWorld
click to copy