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

C++ placement new syntax:
new(ptr) T(args);
click to copy
What is the output: int* arr=new int[5]{1,2,3,4,5}; cout<<arr[2]; delete[] arr;
3
click to copy
The output of: auto ptr=make_unique<int[]>(5); ptr[2]=42; cout<<ptr[2];
42
click to copy
The output of: auto sp=make_shared<int>(10); auto sp2=sp; *sp=20; cout<<*sp2;
20
click to copy
The output of: auto sp=make_shared<int>(5); weak_ptr<int> wp=sp; sp.reset(); cout<<wp.expired();
1
click to copy
What is the output: auto sp=make_shared<int>(42); cout<<sp.use_count();
1
click to copy
The output of: auto sp=make_shared<int>(42); auto sp2=sp; cout<<sp.use_count();
2
click to copy
The output of: thread t1([]{cout<<"A";}); thread t2([]{cout<<"B";}); t1.join(); t2.join(); output is:
AB or BA (non-deterministic)
click to copy
What is the output: atomic<int> x=0; thread t1([&]{for(int i=0;i<1000;i++)x++;}); thread t2([&]{for(int i=0;i<1000;i++)x++;}); t1.join(); t2.join(); cout<<x;
2000
click to copy
Without atomic, same code above would give:
Undefined behavior (race condition on non-atomic x)
click to copy
std::mutex::lock() blocks until:
Mutex is acquired (other thread releases it)
click to copy
std::try_lock() differs from lock() in:
try_lock returns immediately (true if acquired, false if not)
click to copy
The output of: mutex m; int x=0; thread t([&]{lock_guard<mutex> lg(m); x=42;}); t.join(); cout<<x;
42
click to copy
Condition variable wait(lock, predicate) returns when:
Predicate returns true (spurious wakeups handled)
click to copy
std::call_once guarantees:
Function called exactly once across all threads
click to copy
What is the output: promise<int> p; future<int> f=p.get_future(); thread t([&p]{p.set_value(42);}); t.join(); cout<<f.get();
42
click to copy
The output of: auto f=async(launch::async,[]{return 42;}); cout<<f.get();
42
click to copy
The output of: packaged_task<int()> pt([](){return 99;}); auto f=pt.get_future(); pt(); cout<<f.get();
99
click to copy
What is the output: int x=5; auto f=[x=x*2]{return x;}; cout<<f();
10
click to copy
Init captures in C++14 lambda [y=expr]:
Capture and initialize new variable y with expr
click to copy
The output of: auto f=[i=0]()mutable{return i++;}; cout<<f()<<f()<<f();
012
click to copy
What is the output: vector<int> v(10); iota(v.begin(),v.end(),1); auto sum=reduce(execution::par,v.begin(),v.end()); cout<<sum;
55
click to copy
The output of: auto v=views::iota(6)|views::filter([](int x){return x%2!=0;}) |views::transform([](int x){return x*x;}); for(auto x:v)cout<<x<<" ";
1 9 25
click to copy
The output of: namespace A{int x=1;} namespace B{int x=2;} cout<<A::x<<B::x;
12
click to copy
The output of: namespace A{void f(){cout<<"A";}} namespace B{void f(){cout<<"B";}} using namespace A; f();
A
click to copy
What is the output: template<int N> struct Factorial{enum{value=N*Factorial<N-1>::value};}; template<> struct Factorial<0>{enum{value=1};}; cout<<Factorial<5>::value;
120
click to copy
Template metaprogramming Fibonacci: Fib<7>::value =
13
click to copy
What is the output: template<class T> struct TypeName{static const char* get(){return "unknown";}}; template<> struct TypeName<int>{static const char* get(){return "int";}}; cout<<TypeName<int>::get();
int
click to copy
The output of: int a=1,b=2; auto [x,y]={a,b}; cout<<x<<y; (C++17 structured binding):
12
click to copy
The output of: optional<int> o=5; cout<<o.value_or(0);
5
click to copy
The output of: optional<int> o; cout<<o.value_or(99);
99
click to copy
The output of: variant<int,string> v=42; cout<<get<int>(v);
42
click to copy
The output of: variant<int,double> v=3.14; cout<<get<double>(v);
3.14
click to copy
The output of: any a=42; cout<<any_cast<int>(a);
42
click to copy
The output of: any a=string("hello"); cout<<any_cast<string>(a);
hello
click to copy
What is the output: format("{0} {1} {0}", "hello", "world"):
hello world hello
click to copy
The output of: format("{:>10}", "hi"):
hi
click to copy
The output of: format("{:.2f}", 3.14159):
3.14
click to copy
The output of: format("{:05d}", 42):
00042
click to copy
The output of: format("{:b}", 10):
1010
click to copy