Systems CourseC/C++ · Linux · Memory
Question bank293 questions · 44 topics

Question bank

Write your answer to each question from memory. When a topic is done, press Compare my answers to see yours next to the model answers, then mark how you did. Answers auto-save in this browser.

Jump to a topic

C++ Core

01 Control Flow · 6 Qs · lesson

Question 101 Control Flow
What does this code print?
C++
int x = 1;
switch (x) {
  case 1: cout << "A";
  case 2: cout << "B"; break;
  case 3: cout << "C";
}
Question 201 Control Flow
When should you use a do-while loop instead of a while loop?
Question 301 Control Flow
Does this compile, and what is the value of x after the if?
C++
int x = 0;
if (x = 5) {
    cout << "entered";
}
// what prints? what is x now?
Question 401 Control Flow
When is switch fall-through intentional rather than a mistake?
Question 5Predict the output01 Control Flow
What does this print?
C++
int i = 0;
do { std::cout << i; } while (i++ < 2);
Question 6True / False01 Control Flow
True or false: a while loop always runs its body at least once.
Choose one
0 / 6 answered

02 Pointers · 7 Qs · lesson

Question 702 Pointers
What is the difference between *p and &x?
Question 802 Pointers
For a heap pointer p, what does printing p give you versus printing &p?
C++
int* p = new int(5);
cout << p;    // the heap address
cout << &p;   // where p itself lives (stack)
Question 902 Pointers
What is the difference between a null pointer and a dangling pointer?
Question 1002 Pointers
Can a pointer be changed to point somewhere else after it is created?
Question 1102 Pointers
Where does a pointer variable itself live in memory?
Question 12Multiple choice02 Pointers
After int x = 5; int* p = &x; *p = 9; what is the value of x?
Choose one
Question 13Predict the output02 Pointers
What does this print?
C++
int a = 1, b = 2;
int* p = &a;
*p = 10;
p = &b;
*p = 20;
std::cout << a << " " << b;
0 / 7 answered

03 Stack & Heap Memory · 8 Qs · lesson

Question 1603 Stack & Heap Memory
What happens when the caller dereferences the pointer this function returns?
C++
int* bad() {
    int x = 42;
    return &x;   // returning the address of a local
}
Question 1703 Stack & Heap Memory
What happens if you free new[] memory with plain delete (instead of delete[])?
Question 20Multiple choice03 Stack & Heap Memory
In void f() { int a = 1; int* p = new int(2); }, what lives on the heap?
Choose one
Question 21True / False03 Stack & Heap Memory
True or false: the stack grows toward lower addresses, so the most recently pushed value sits at the lowest address.
Choose one
0 / 8 answered

04 References · 6 Qs · lesson

Question 2204 References
Name two things a reference cannot do that a pointer can.
Question 2304 References
What does this print, and why?
C++
void byVal(int x){ x = 99; }
void byRef(int& x){ x = 99; }
int n = 1;
byVal(n); byRef(n);
cout << n;
Question 2404 References
Why do we often pass large objects as const T& instead of by value?
Question 2504 References
Why must a reference be initialized at the point where it is declared?
Question 26Predict the output04 References
What does this print?
C++
void f(int a, int& b) { a = 10; b = 20; }

int main() {
    int x = 1, y = 2;
    f(x, y);
    std::cout << x << " " << y;
}
Question 27True / False04 References
True or false: in well-formed code, a reference can be null.
Choose one
0 / 6 answered

05 Classes & Encapsulation · 5 Qs · lesson

Question 2805 Classes & Encapsulation
If private gives no runtime security, what is the real point of making data private?
Question 31Predict the output05 Classes & Encapsulation
What does this print?
C++
class Account {
    int balance = 0;
public:
    void deposit(int a)  { if (a > 0) balance += a; }
    bool withdraw(int a) { if (a > balance) return false; balance -= a; return true; }
    int  get() const     { return balance; }
};

int main() {
    Account acc;
    acc.deposit(100);
    acc.withdraw(150);
    acc.withdraw(30);
    std::cout << acc.get();
}
Question 32True / False05 Classes & Encapsulation
True or false: making a member private stops other code from reading that object's memory at runtime.
Choose one
0 / 5 answered

06 Classes vs Structs · 5 Qs · lesson

Question 3306 Classes vs Structs
What are the ONLY two language-level differences between a class and a struct?
Question 3406 Classes vs Structs
Is x accessible from outside here?
C++
class C { int x; };
int main(){ C c; c.x = 5; }
Question 36Multiple answers06 Classes vs Structs
Which of these are real language differences between class and struct? Select all that apply.
Select all that apply
Question 37Multiple choice06 Classes vs Structs
struct B { int x; }; and class D : B {};. Does D d; d.x = 1; compile?
Choose one
0 / 5 answered

07 How to Write a Class · 6 Qs · lesson

Question 3807 How to Write a Class
Why does this compile but fail to link with 'undefined reference to Entity::GetX()'?
C++
// entity.h
class Entity { int x; public: int GetX(); };
// entity.cpp
int GetX() { return x; }   // note: no Entity::
Question 4007 How to Write a Class
What happens if you put a non-inline function's full body in a header that multiple .cpp files include?
Question 42Multiple choice07 How to Write a Class
Entity declares int GetX() const;. Which out-of-class definition is correct?
Choose one
Question 43True / False07 How to Write a Class
True or false: a member function defined inside the class body is implicitly inline.
Choose one
0 / 6 answered

08 Static (outside classes) · 6 Qs · lesson

Question 4408 Static (outside classes)
What does calling counter() three times in a row print?
C++
int counter(){ static int n = 0; return ++n; }
Question 4608 Static (outside classes)
Why does a static local variable initialize safely regardless of other globals' init order?
What is this question asking?

This refers to the 'static initialization order fiasco' — a named C++ problem where globals defined in different .cpp files are constructed in an unspecified order across files. The question asks why a function-local static is immune to that ordering problem.

Question 48Predict the output08 Static (outside classes)
What does this print?
C++
void f() {
    static int s = 0;
    int n = 0;
    ++s; ++n;
    std::cout << s << n << " ";
}

int main() { f(); f(); f(); }
Question 49Multiple choice08 Static (outside classes)
a.cpp has static int helper = 5; at file scope. b.cpp says extern int helper; and uses it. What happens?
Choose one
0 / 6 answered

09 Static (for Classes & Structs) · 5 Qs · lesson

Question 5109 Static (for Classes & Structs)
Why does this fail to link with 'undefined reference to Entity::count'?
C++
struct Entity { static int count; };
// no definition anywhere
int main(){ Entity::count++; }
Question 53Predict the output09 Static (for Classes & Structs)
On x86-64, what does this print?
C++
struct Entity {
    static int count;
    int id;
    Entity() : id(++count) {}
};
int Entity::count = 0;

int main() {
    Entity a, b, c;
    std::cout << Entity::count << " " << b.id << " " << sizeof(Entity);
}
0 / 5 answered

10 Enums · 6 Qs · lesson

Question 5510 Enums
In enum E { A = 3, B, C };, what are the values of B and C?
Question 5610 Enums
Give two problems with a plain enum that enum class fixes.
Question 5710 Enums
How do you make an enum occupy only 1 byte instead of the default 4?
Question 5810 Enums
How do you convert an enum class value to an int?
Question 59Predict the output10 Enums
What does this print?
C++
enum Level { Low, Mid = 5, High };
std::cout << Low << " " << Mid << " " << High;
Question 60Multiple choice10 Enums
enum class Color { Red, Green }; What does int x = Color::Green; do?
Choose one
0 / 6 answered

11 Constructors · 6 Qs · lesson

Question 6111 Constructors
Why does Entity e; stop compiling once you add a constructor that takes arguments?
C++
class Entity {
public:
    Entity(int x) { /*...*/ }   // now Entity e; fails
};
Question 6211 Constructors
Name two cases where a member initializer list is required, not merely preferred.
Question 6311 Constructors
Why prefer an initializer list over assigning members inside the constructor body?
Question 6411 Constructors
What does Entity e(); actually declare?
Question 65Multiple choice11 Constructors
Which line creates a default-constructed Entity object named e?
Choose one
Question 66Multiple answers11 Constructors
Which members must be initialized in the member initializer list? Select all that apply.
Select all that apply
0 / 6 answered

12 Destructors & RAII · 6 Qs · lesson

Question 6712 Destructors & RAII
Base has a non-virtual destructor — what happens when delete b; runs?
C++
struct Base { ~Base(){} };            // non-virtual!
struct Derived : Base { /* owns memory */ };
Base* b = new Derived();
delete b;
Question 6812 Destructors & RAII
Why does RAII make resource cleanup automatic and exception-safe?
Question 7012 Destructors & RAII
In what order are members and base/derived subobjects destroyed?
Question 71Predict the output12 Destructors & RAII
What does this print?
C++
struct T {
    char name;
    T(char n) : name(n) { std::cout << "+" << name; }
    ~T() { std::cout << "-" << name; }
};

int main() {
    T a('A');
    {
        T b('B');
    }
    T c('C');
}
Question 72True / False12 Destructors & RAII
True or false: deleting a derived object through a base pointer is safe even when the base destructor is not virtual.
Choose one
0 / 6 answered

13 Inheritance · 6 Qs · lesson

Question 7413 Inheritance
Who can access a protected member?
Question 7513 Inheritance
In what order are the base and derived parts constructed and destroyed?
Question 7613 Inheritance
What is the difference between an 'is-a' and a 'has-a' relationship?
Question 77Predict the output13 Inheritance
What does this print?
C++
struct Base {
    Base()  { std::cout << "B"; }
    ~Base() { std::cout << "~B"; }
};
struct Derived : Base {
    Derived()  { std::cout << "D"; }
    ~Derived() { std::cout << "~D"; }
};

int main() { Derived d; }
Question 78Predict the output13 Inheritance
What does this print?
C++
struct Animal { virtual std::string name() const { return "animal"; } };
struct Dog : Animal { std::string name() const override { return "dog"; } };

void byValue(Animal a)      { std::cout << a.name(); }
void byRef(const Animal& a) { std::cout << a.name(); }

int main() {
    Dog d;
    byValue(d);
    std::cout << " ";
    byRef(d);
}
0 / 6 answered

14 Virtual Functions · 7 Qs · lesson

Question 7914 Virtual Functions
What does a->speak() print here, and why?
C++
struct Animal { void speak(){cout<<"Animal";} };  // not virtual
struct Dog : Animal { void speak(){cout<<"Dog";} };
Animal* a = new Dog();
a->speak();
Question 8014 Virtual Functions
How does a virtual call select the right function at runtime, using the vtable and vptr?
Question 8214 Virtual Functions
What does the override specifier do, and why is it worth adding?
Question 84Predict the output14 Virtual Functions
What does this print?
C++
struct Base {
    void a()         { std::cout << "Ba "; }
    virtual void b() { std::cout << "Bb "; }
};
struct Der : Base {
    void a()          { std::cout << "Da "; }
    void b() override { std::cout << "Db "; }
};

int main() {
    Der d;
    Base* p = &d;
    p->a();
    p->b();
}
Question 85Multiple choice14 Virtual Functions
A class has no virtual functions. How much does a vptr add to each object?
Choose one
0 / 7 answered

15 Interfaces / Abstract Classes · 7 Qs · lesson

Question 91Multiple choice15 Interfaces / Abstract Classes
Given struct Shape { virtual double area() const = 0; };, which line does not compile?
Choose one
Question 92True / False15 Interfaces / Abstract Classes
True or false: a derived class that overrides only some of its base's pure virtual functions is still abstract.
Choose one
0 / 7 answered

C++ Language & Safety

16 const, constexpr & volatile · 6 Qs · lesson

Question 9316 const, constexpr & volatile
Which of these compile, and which is an error?
C++
int n = getInput();
const int a = n;
constexpr int b = n;
constexpr int c = 42;
Question 97Multiple answers16 const, constexpr & volatile
n is read at runtime (int n = getInput();). Which of these lines compile? Select all that apply.
Select all that apply
Question 98True / False16 const, constexpr & volatile
True or false: marking a shared counter volatile makes ++counter safe to call from two threads.
Choose one
0 / 6 answered

17 Compile-time Constants & VLAs · 7 Qs · lesson

Question 9917 Compile-time Constants & VLAs
Two of these are standard C++ and one is not — which one, and what is it called?
C++
constexpr int s1 = 5*4; int a[s1];
const     int s2 = 5*4; int b[s2];
          int s3 = 5*4; int c[s3];
Question 104Multiple choice17 Compile-time Constants & VLAs
int a[n]; with a runtime n compiles silently on g++. Which flag makes g++ warn that it is not standard C++?
Choose one
Question 105True / False17 Compile-time Constants & VLAs
True or false: switching from -std=c++11 to -std=c++20 makes g++ reject int a[n]; with a runtime n.
Choose one
0 / 7 answered

18 Arrays & Memory Contiguity · 6 Qs · lesson

Question 10618 Arrays & Memory Contiguity
An array must be laid out contiguously — is that guaranteed in virtual memory or in physical RAM?
Question 110Predict the output18 Arrays & Memory Contiguity
What does this print?
C++
int arr[5] = {10, 20, 30, 40, 50};
int* p = arr;
std::cout << *(p + 3) << " " << (&arr[4] - &arr[1]);
Question 111Multiple choice18 Arrays & Memory Contiguity
An array must be contiguous. Where is that actually guaranteed?
Choose one
0 / 6 answered

19 Endianness · 7 Qs · lesson

Question 11219 Endianness
On a little-endian machine, how is the int 500 (0x000001F4) laid out byte by byte in memory?
Question 11319 Endianness
If you cout << somePointer;, do you see the reversed little-endian bytes or the real address?
Question 11419 Endianness
Is a pointer affected by endianness?
Question 11519 Endianness
How do you detect at runtime whether a machine is little- or big-endian?
Question 11619 Endianness
Which values are immune to endianness?
Question 117Predict the output19 Endianness
On a little-endian machine (x86-64), what does this print?
C++
int x = 0x01020304;
unsigned char* b = (unsigned char*)&x;
std::cout << (int)b[0];
Question 118True / False19 Endianness
True or false: on a little-endian machine, std::cout << x; prints the value of x with its bytes reversed.
Choose one
0 / 7 answered

20 Undefined Behavior & Bounds Checking · 7 Qs · lesson

Question 124Multiple choice20 Undefined Behavior & Bounds Checking
std::array<int, 5> a{}; a[10] = 4; What does the C++ standard say happens?
Choose one
Question 125Predict the output20 Undefined Behavior & Bounds Checking
What does this print?
C++
std::vector<int> v = {1, 2, 3};
try {
    v.at(5) = 9;
    std::cout << "ok";
} catch (const std::out_of_range&) {
    std::cout << "caught";
}
0 / 7 answered

21 std::array vs std::vector · 6 Qs · lesson

Question 130Multiple choice21 std::array vs std::vector
Which of these can grow at runtime with push_back?
Choose one
Question 131Predict the output21 std::array vs std::vector
What does this print?
C++
std::vector<int> v;
v.reserve(10);
v.push_back(1);
v.push_back(2);
std::cout << v.size() << " " << (v.capacity() >= 10);
0 / 6 answered

22 Exceptions & try/catch · 6 Qs · lesson

Question 13222 Exceptions & try/catch
Calling a.at(10) on a size-5 array printed 'Aborted (core dumped)'. Whose fault is the crash?
Question 136Predict the output22 Exceptions & try/catch
What does this print?
C++
try {
    std::cout << "A";
    throw std::runtime_error("boom");
    std::cout << "B";
} catch (const std::exception& e) {
    std::cout << "C" << e.what();
}
std::cout << "D";
Question 137Multiple choice22 Exceptions & try/catch
Which is the recommended way to catch a standard exception?
Choose one
0 / 6 answered

23 AddressSanitizer & Debugging Tools · 6 Qs · lesson

Question 142Multiple choice23 AddressSanitizer & Debugging Tools
Which flags build a program with AddressSanitizer and the line numbers it needs for a readable report?
Choose one
Question 143True / False23 AddressSanitizer & Debugging Tools
True or false: if a program built with -fsanitize=address compiles without errors, it has no memory bugs.
Choose one
0 / 6 answered

OS Memory & Systems

24 Paging & Virtual Memory · 7 Qs · lesson

Question 14724 Paging & Virtual Memory
If a program's pages are scattered all over physical RAM, how does it still behave as if memory were one contiguous block?
Question 149True / False24 Paging & Virtual Memory
True or false: one physical frame can hold pieces of several pages at the same time.
Choose one
Question 150Multiple choice24 Paging & Virtual Memory
Which problem does paging solve?
Choose one
0 / 7 answered

25 MMU, TLB & Multi-level Paging · 7 Qs · lesson

Question 156Multiple choice25 MMU, TLB & Multi-level Paging
With 4 KB (4096-byte) pages, virtual address 12290 is in which page, and at what offset?
Choose one
Question 157Multiple choice25 MMU, TLB & Multi-level Paging
With 4 KB pages, how many bits of a virtual address are the offset inside the page?
Choose one
0 / 7 answered

26 Allocation Methods & Fragmentation · 7 Qs · lesson

Question 163Multiple choice26 Allocation Methods & Fragmentation
A process needs 10 KB and pages are 4 KB. How many frames does it use, and how much internal fragmentation is there?
Choose one
0 / 7 answered

27 Demand Paging, Page Faults & Swapping · 8 Qs · lesson

Question 171Multiple answers27 Demand Paging, Page Faults & Swapping
Which of these are true of a page fault? Select all that apply.
Select all that apply
Question 172Multiple choice27 Demand Paging, Page Faults & Swapping
The running programs need more memory than RAM, so pages are constantly swapped in and out and the system crawls. What is this called?
Choose one
0 / 8 answered

Linux

28 Filesystem & Navigation · 6 Qs · lesson

Question 17328 Filesystem & Navigation
Your program does open("config.txt"); it works from one directory but fails with 'file not found' from another. Why?
Question 177Predict the output28 Filesystem & Navigation
Both directories exist. What does this print?
Bash
cd /home/anup/project/src
cd ../build
pwd
Question 178Multiple choice28 Filesystem & Navigation
Which of these paths is absolute?
Choose one
0 / 6 answered

29 Permissions (rwx) · 6 Qs · lesson

Question 17929 Permissions (rwx)
What numeric permission mode does -rwxr-xr-x correspond to?
Question 18029 Permissions (rwx)
You compiled a program but ./prog says 'Permission denied' — what's the most likely cause?
Question 18129 Permissions (rwx)
When you access a file, how does the kernel decide which of the three rwx triplets to apply?
Question 18229 Permissions (rwx)
On a directory, what do r, w, and x each mean (they differ from a file)?
Question 183Multiple choice29 Permissions (rwx)
What is -rw-r--r-- in numeric form?
Choose one
Question 184Multiple choice29 Permissions (rwx)
ls -l shows drwxr-x---. What is it, and can others list its contents?
Choose one
0 / 6 answered

30 chmod · 5 Qs · lesson

Question 18530 chmod
What is the difference between chmod 755 f and chmod +x f?
Question 18630 chmod
In symbolic chmod, how do you give execute permission to ONLY the owner?
Question 18730 chmod
Which permission bit is the usual culprit behind 'Permission denied' when running ./script.sh?
Question 188Multiple choice30 chmod
A file is -rw-r--r-- (644). What does ls -l show after chmod u+x f?
Choose one
Question 189Multiple choice30 chmod
Which command sets a file to exactly rwxr-xr-x, whatever its bits were before?
Choose one
0 / 5 answered

31 chown & Ownership · 6 Qs · lesson

Question 19031 chown & Ownership
Why can't a normal (non-root) user give their own file to another user with chown?
Question 19131 chown & Ownership
Give the chown syntax for: owner+group, owner only, group only, and recursive.
Question 19231 chown & Ownership
How do ownership and the rwx bits work together to decide access?
Question 194True / False31 chown & Ownership
True or false: a regular (non-root) user can give one of their own files to another user with chown.
Choose one
Question 195Multiple choice31 chown & Ownership
Which command changes only the group of data.txt to devs?
Choose one
0 / 6 answered

C++ Core (continued)

32 Member Initializer List · 8 Qs · lesson

Question 19632 Member Initializer List
What is the difference between initialization (in the list) and assignment (in the body)?
Question 20132 Member Initializer List
If C++11 lets you write int a = 0; at the declaration, why is the init list still needed?
Question 202Predict the output32 Member Initializer List
What does this print?
C++
struct Log { Log(char c) { std::cout << c; } };

struct S {
    Log x, y;
    S() : y('Y'), x('X') {}
};

int main() { S s; }
Question 203True / False32 Member Initializer List
True or false: if a member has a default initializer (int a = 0;) and is also in the member initializer list, the list's value is used.
Choose one
0 / 8 answered

33 Ternary Operator · 8 Qs · lesson

Question 20433 Ternary Operator
Is the ternary a statement or an expression, and why does that matter?
Question 20533 Ternary Operator
Why can a ternary initialize a const when if/else can't?
Question 20733 Ternary Operator
When do the two branches make a ternary fail to compile?
Question 210Predict the output33 Ternary Operator
What does this print?
C++
int x = 7;
std::cout << (x % 2 == 0 ? "even" : "odd") << " " << (x > 5 ? x * 2 : x);
Question 211Multiple choice33 Ternary Operator
auto r = true ? 1 : 2.5; What is the type of r?
Choose one
0 / 8 answered

34 Creating / Instantiating Objects · 8 Qs · lesson

Question 218Multiple choice34 Creating / Instantiating Objects
Entity* e = new Entity; How do you call its print() method?
Choose one
Question 219Multiple answers34 Creating / Instantiating Objects
Which of these leak memory or use a dangling pointer? Select all that apply.
Select all that apply
0 / 8 answered

35 The new Keyword · 8 Qs · lesson

Question 22235 The new Keyword
Why must new[] be paired with delete[] and not plain delete?
Question 22435 The new Keyword
free/delete are given only p — how do they know where the header is and how big the block is?
Question 22535 The new Keyword
What is the difference between new int; and new int();?
Question 226Predict the output35 The new Keyword
What does this print?
C++
struct C {
    C()  { std::cout << "c"; }
    ~C() { std::cout << "d"; }
};

int main() {
    C* p = new C[3];
    delete[] p;
}
Question 227Multiple answers35 The new Keyword
Which of these are true of new compared with malloc? Select all that apply.
Select all that apply
0 / 8 answered

36 Implicit Conversion & explicit · 7 Qs · lesson

Question 233Multiple answers36 Implicit Conversion & explicit
Given struct Entity { explicit Entity(int a); }; and void f(Entity);, which lines compile? Select all that apply.
Select all that apply
Question 234Predict the output36 Implicit Conversion & explicit
What does this print?
C++
struct Meters {
    double v;
    Meters(double x) : v(x) {}
};

double twice(Meters m) { return m.v * 2; }

int main() { std::cout << twice(3.5); }
0 / 7 answered

37 Operators & Operator Overloading · 7 Qs · lesson

Question 240Predict the output37 Operators & Operator Overloading
What does this print?
C++
struct V {
    int x, y;
    V operator+(const V& o) const { return {x + o.x, y + o.y}; }
    V& operator+=(const V& o) { x += o.x; y += o.y; return *this; }
};
std::ostream& operator<<(std::ostream& os, const V& v) {
    return os << "(" << v.x << "," << v.y << ")";
}

int main() {
    V a{1, 2}, b{3, 4};
    V c = a + b;
    a += b;
    a += b;
    std::cout << c << " " << a;
}
0 / 7 answered

38 The this Keyword · 7 Qs · lesson

Question 24338 The this Keyword
What is the type of this in a non-const vs a const member function?
Question 247Predict the output38 The this Keyword
What does this print?
C++
struct Counter {
    int n = 0;
    Counter& add(int k) { n += k; return *this; }
};

int main() {
    Counter c;
    c.add(1).add(2).add(3);
    std::cout << c.n;
}
Question 248Predict the output38 The this Keyword
This version returns Counter by value instead of by reference. What does it print now?
C++
struct Counter {
    int n = 0;
    Counter add(int k) { n += k; return *this; }   // returns a copy
};

int main() {
    Counter c;
    c.add(1).add(2).add(3);
    std::cout << c.n;
}
0 / 7 answered

39 Object Lifetime · 7 Qs · lesson

Question 24939 Object Lifetime
Name the three storage durations and what determines each object's lifetime.
Question 25039 Object Lifetime
Why is returning the address of a local variable a bug?
Question 25139 Object Lifetime
Give two correct ways to return data that outlives a function.
Question 25339 Object Lifetime
Why does a static local variable keep its value between calls?
Question 254Predict the output39 Object Lifetime
What does this print? (Each destructor prints its id.)
C++
struct T {
    int id;
    T(int i) : id(i) {}
    ~T() { std::cout << id; }
};

int main() {
    T a(1);
    {
        T b(2);
        static T s(9);
    }
    T c(3);
}
Question 255Multiple choice39 Object Lifetime
Which function returns a dangling reference?
Choose one
0 / 7 answered

40 Smart Pointers · 8 Qs · lesson

Question 25640 Smart Pointers
What problem do smart pointers solve, and which mechanism makes them work?
Question 25740 Smart Pointers
What is the difference between unique_ptr and shared_ptr, and how does shared_ptr know when to delete?
Question 25840 Smart Pointers
What is weak_ptr for, and why doesn't it affect the reference count?
Question 25940 Smart Pointers
Why prefer make_unique/make_shared over raw new?
Question 26040 Smart Pointers
What's the difference between release(), reset(), and std::move on a unique_ptr?
Question 26140 Smart Pointers
When is a raw pointer the right choice over a smart pointer?
Question 262Predict the output40 Smart Pointers
What does this print?
C++
auto a = std::make_shared<int>(5);
std::cout << a.use_count();
{
    auto b = a;
    std::cout << a.use_count();
}
std::weak_ptr<int> w = a;
std::cout << a.use_count();
Question 263Multiple choice40 Smart Pointers
auto p = std::make_unique<int>(1); auto q = p; What happens?
Choose one
0 / 8 answered

41 Copying & Copy Constructors · 7 Qs · lesson

Question 26541 Copying & Copy Constructors
What does the default copy constructor do, and why is that dangerous for a class with a pointer member?
Question 269Predict the output41 Copying & Copy Constructors
What does this print? (D = default constructor, C = copy constructor, A = copy assignment.)
C++
struct S {
    S()                    { std::cout << "D"; }
    S(const S&)            { std::cout << "C"; }
    S& operator=(const S&) { std::cout << "A"; return *this; }
};
void byValue(S) {}
void byRef(const S&) {}

int main() {
    S a;
    S b = a;
    S c;
    c = a;
    byValue(a);
    byRef(a);
}
Question 270True / False41 Copying & Copy Constructors
True or false: the compiler-generated copy constructor copies the data that a pointer member points to.
Choose one
0 / 7 answered

42 The Arrow Operator · 6 Qs · lesson

Question 27242 The Arrow Operator
For a raw pointer, is -> built-in or overloaded — and when does overloading come in?
Question 27342 The Arrow Operator
What does an overloaded operator-> return, and what does the compiler do with it?
Question 27442 The Arrow Operator
Why can you use -> on a smart pointer even though it's an object, not a raw pointer?
Question 275Predict the output42 The Arrow Operator
What does this print?
C++
struct P { int x = 3; };

int main() {
    P obj;
    P* raw = &obj;
    auto sp = std::make_unique<P>();
    sp->x = 7;
    raw->x += 1;
    std::cout << obj.x << " " << (*sp).x;
}
Question 276True / False42 The Arrow Operator
True or false: an overloaded operator-> must return a pointer, or an object that itself has operator->.
Choose one
0 / 6 answered

43 std::vector (Dynamic Array) · 8 Qs · lesson

Question 283Predict the output43 std::vector (Dynamic Array)
What does this print?
C++
std::vector<int> v = {10, 20, 30};
v.push_back(40);
v.emplace_back(50);
std::cout << v.size() << " " << v[4] << " " << v.front() + v.back();
Question 284Multiple choice43 std::vector (Dynamic Array)
You saved int* p = &v[0];, then a push_back made the vector reallocate. What is p now?
Choose one
0 / 8 answered

44 Local Static Variables · 9 Qs · lesson

Question 28544 Local Static Variables
What changes when you put static on a local variable, and what stays the same?
Question 28744 Local Static Variables
What does this code print, and why?
C++
int counter() { static int n = 0; return ++n; }

int main() {
    for (int i = 0; i < 3; i++) std::cout << counter() << ' ';
}
Question 28944 Local Static Variables
A function with static Logger L; is never called. Is the constructor ever run? Is the destructor?
Question 29044 Local Static Variables
Two threads call a function containing static Logger L; at the same time. What is safe and what is not?
Question 29144 Local Static Variables
What is the difference between static Logger L; and static Logger* P = new Logger(); at program exit?
Question 292Predict the output44 Local Static Variables
What does this print?
C++
int next() { static int n = 10; n += 5; return n; }

int main() {
    std::cout << next() << " ";
    std::cout << next() << " ";
    std::cout << next();
}
Question 293True / False44 Local Static Variables
True or false: other functions in the same file can use a static local variable by name.
Choose one
0 / 9 answered