katherinemohr.github.io
A silly little C++ quiz
Published April 20, 2026
!!! This is a work in progress !!!
Getting the answer formatting right took embarrassingly long, so I’ll stop using this to procrastinate other things and finish the explanations another day.
For all the code snippets,
- assume they are being run on an
x86_64machine withg++and-std=c++23and - assume all necessary headers have been included
Hint: the code snippets might not all compile.
- What is the value of
xafter the following code is executed?int x = 0; x = x++;Answer/Explanation
0.First,
x++evaluates to0(returns old value).
Then the increment side effect makesxbecome1, but assignment writes the old0back, so the finalxis0. - What is the value of
xafter the following code is executed?int x[2] = {0,1}; x[0], x[1] = x[1], x[0];Answer/Explanation
- Given the following function
display, what is the result of passing each of the followingvector<string>s intodisplay?void display(const vector<string> &str_vec) { cout << str_vec.size() << endl; for (const string &str : str_vec) { cout << str << endl; } }vector<string> foo{{"hello", ",", "world"}}; vector<string> bar{{"hello", "world"}}; vector<string> baz{{"hello", "hello"}};Answer/Explanation
- What must the type/value of
foobe forfoo["bar"] == 'a'to be true?Answer/Explanation
- What is the output of the following?
struct Bit { int b : 2 = 1; }; int main() { Bit bit; cout << ++bit.b << endl; }Answer/Explanation
- What does
constmodify in the following: theint, or the pointer to theint?int const *ptr;Answer/Explanation
- What is the output of the following? Why?
void print(int &x) { cout << 1; } void print(int &&x) { cout << 2; } void print(const int &x) { cout << 3; } void print(const int &&x) { cout << 4; } int main() { int a = 3; print((const int)a); print((const int)1); print(4); }Answer/Explanation
- Given the empty struct
struct foo {}, what is the size offoo?Answer/Explanation
- Given the struct below, what is the size of
foo?struct foo { uint8_t a; uint16_t b; uint8_t c; uint32_t d; uint8_t e; };Answer/Explanation
- What is the output of the following code?
#include <iostream> void f() { auto counter = []() { static int b = 1; return b++; }; std::cout << counter(); } int main() { f(); f(); }Answer/Explanation
- What is the output of the following code?
#include <iostream> struct A { A() { std::cout << "A"; } ~A() { std::cout << "a"; } }; int main() { return sizeof new A; }Answer/Explanation
- What is the output of the following code?
#include <iostream> bool false() { std::cout << "false"; return false; } bool true() { std::cout << "true"; return true; } int bar() { std::cout << "bar"; return 0; } char baz() { std::cout << "baz"; return 0; } int main() { return false() && true() ? bar() : baz(); }Answer/Explanation
- In the following code:
- How many times is
foo’s copy constructor called? - How many times is
foo’s parameterized constructor called?
struct foo { int x; // Parameterized constructor foo(int _x) : x(_x) {} // Copy constructor foo(foo &other) : x(other.x) {} }; struct bar { foo x; foo y; bar(foo _x, foo _y) : x(_x), y(_y) {} }; int main() { foo x{1}; foo y{2}; bar{x,y}; return 0; }Answer/Explanation
- How many times is
- Identify each stack and heap allocation in the following:
#include <memory> int main() { int x = 42; int *y = new int(8); auto z = std::make_shared<int>(-50); int result = x + *y + *z; delete y; return result; }Answer/Explanation
- What does the following print out?
int x = 010; std::cout << x << std::endl;Answer/Explanation
- When does each variable get allocated and deallocated?
int a = 0; void foo() { int b = 1; static int c = 2; { int d = 3; static int e = 4; const int f = 5; } } int main() { foo(); return 0; }Answer/Explanation
- What does the following code evaluate to? Why might we choose to write code this way?
#include <iostream> template<typename T> auto f(T t) -> decltype(t.foo(), int{}) { return 1; } template<typename T> auto f(T t) -> decltype(t + 1, int{}) { return 2; } int f(...) { return 3; } struct A { void foo(); }; struct B {}; int main() { std::cout << f(A{}) << f(B{}) << f("hi"); }Answer/Explanation