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: 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
The output of: format("{:x}", 255):
ff
click to copy
The output of: format("{:o}", 8):
10
click to copy
What is the output: string s=format("Hello, {}!", "World"); cout<<s;
Hello, World!
click to copy
The output of: auto r=views::iota(0,5); cout<<ranges::distance(r);
5
click to copy
The output of: vector<int> v={1,2,3,4,5}; auto sum=ranges::fold_left(v,0,plus<>{}); cout<<sum;
15
click to copy
Which C++ keyword marks a variable as thread-local storage?
thread_local
click to copy
The output of: constexpr int sq(int n){return n*n;} static_assert(sq(5)==25); cout<<"OK";
OK
click to copy
static_assert in C++ checks:
Compile-time assertion; program fails to compile if condition is false
click to copy
The output of: template<class T> void f(T&&x){cout<<is_lvalue_reference<T>::value;} int a=5; f(a); f(5);
10
click to copy
What is the output: int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}}; cout<<arr[1][2];
6
click to copy
The output of: int** p; int a[2][2]={{1,2},{3,4}}; p=new int*[2]; for(int i=0;i<2;i++) p[i]=a[i]; cout<<p[1][0];
3
click to copy
The output of: char* s="Hello"; cout<<strlen(s);
5
click to copy
The output of: char a[]="Hello"; char b[]="World"; strcat(a,b); cout<<a;
HelloWorld
click to copy
The output of: char a[]="Hello"; char b[]="Hello"; cout<<strcmp(a,b);
0
click to copy
The output of: char a[]="Apple"; char b[]="Banana"; cout<<(strcmp(a,b)<0?"a<b":"a>=b");
a<b
click to copy
The output of: char s[]="Hello World"; char* p=strtok(s," "); cout<<p;
Hello
click to copy
memcpy vs memmove difference:
memmove handles overlapping source/dest; memcpy does not
click to copy
The output of: int arr[5]={1,2,3,4,5}; memset(arr,0,sizeof(arr)); cout<<arr[2];
0
click to copy
The output of: int a=1,b=2; memcpy(&a,&b,sizeof(int)); cout<<a;
2
click to copy
What is the output: printf("%d %d %d",1,2,3);
1 2 3
click to copy
What is the output: scanf("%d",&n) with input 42; printf("%d",n);
42
click to copy
What does perror("msg") do in C++?
Print msg + colon + system error message for errno
click to copy
The output of: FILE* f=tmpfile(); fprintf(f,"test"); rewind(f); char buf[10]; fscanf(f,"%s",buf); cout<<buf;
test
click to copy
What is the output: jmp_buf jb; if(!setjmp(jb)){cout<<"A"; longjmp(jb,1);} cout<<"B";
AB
click to copy
longjmp in C++ is:
Dangerous - does not call destructors (use C++ exceptions instead)
click to copy