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,

  1. assume they are being run on an x86_64 machine with g++ and -std=c++23 and
  2. assume all necessary headers have been included

Hint: the code snippets might not all compile.


  1. What is the value of x after the following code is executed?
     int x = 0;
     x = x++;
    
    Answer/Explanation

    0.

    First, x++ evaluates to 0 (returns old value).
    Then the increment side effect makes x become 1, but assignment writes the old 0 back, so the final x is 0.

    godbolt link


  2. What is the value of x after the following code is executed?
     int x[2] = {0,1};
     x[0], x[1] = x[1], x[0];
    
    Answer/Explanation


  3. Given the following function display, what is the result of passing each of the following vector<string>s into display?
     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


  4. What must the type/value of foo be for foo["bar"] == 'a' to be true?
    Answer/Explanation


  5. What is the output of the following?
     struct Bit {
         int b : 2 = 1;
     };
    
     int main() {
         Bit bit;
         cout << ++bit.b << endl;
     }
    
    Answer/Explanation


  6. What does const modify in the following: the int, or the pointer to the int?
     int const *ptr;
    
    Answer/Explanation


  7. 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


  8. Given the empty struct struct foo {}, what is the size of foo?
    Answer/Explanation


  9. 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


  10. 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


  11. 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


  12. 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


  13. In the following code:
    1. How many times is foo’s copy constructor called?
    2. 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


  14. 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


  15. What does the following print out?
     int x = 010;
     std::cout << x << std::endl;
    
    Answer/Explanation


  16. 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


  17. 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