Knowledge Test Score Board
— · 0%
Work through the questions below — your score updates as you go.
Keep going · grade bands mapped to DCU's honours scale
Only your first attempt at each question counts toward this score. Reload the page to reset.
This section contains 20 interactive knowledge checks designed to test your understanding of the materials covered in Chapter 6. The questions cover generic programming with templates, the Standard Template Library (STL), robust memory management using smart pointers, and modern C++ features like move semantics, lambdas, and std::optional.
Please remember that many of the quizzes have multiple correct answers , and you must select all applicable options to succeed. I have carefully balanced the lengths of the answers, so take your time to evaluate each option thoroughly.
Good luck!
Derek.
Defining a Function Template derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 // A generic function to add two values together
2 template < ····· T>
3 T add( ····· a, ····· b) {
4 return a + b;
5 }
Available Snippets
T
Template
typename
class
type
auto
T
Submit
Why is it common practice to place complete C++ template definitions directly within header files, rather than splitting them into separate source files? derekmolloy.ie
Because placing them in header files allows the compiler to automatically deduce the required C++20 Concepts.
Because the linker automatically rejects template instantiations that span multiple translation units to avoid memory fragmentation.
Because the compiler must have access to the full template definition to generate type-specific code upon instantiation.
Because templates bypass the standard compilation phase and are interpreted dynamically at program execution time. Submit Answer
Concept Match
Match the Template Terminology derekmolloy.ie Drag each definition into its matching concept slot, then click Submit. Tap × to return a placed card to the pool.
Definition Pool
Providing a completely customised implementation of a template for one specific concrete type (e.g., bool).
Customising a class template for a subset or category of types, such as all pointer types.
Explicitly specifying requirements on template parameters to ensure proper usage and clearer compile-time errors.
The primary template definition that serves as a fallback for types that do not match any specialisation.
Submit
Predict the output: Templates and Static Members derekmolloy.ie Read the code below, then choose the terminal output it produces and click Submit.
1 #include <iostream>
2
3 template<typename T>
4 class Config {
5 public:
6 static int loadCount;
7 Config() { loadCount++; }
8 };
9
10 // Initialise static member
11 template<typename T>
12 int Config<T>::loadCount = 0;
13
14 int main() {
15 Config<int> a;
16 Config<int> b;
17 Config<float> c;
18
19 std::cout << Config<int>::loadCount << " " << Config<float>::loadCount;
20 return 0;
21 }
Submit Answer
Which of the following statements accurately describe the benefits of C++20 Concepts? derekmolloy.ie
They provide explicit requirements on template parameters, leading to clearer code and more readable compiler errors.
They force class templates to be allocated strictly on the stack, preventing dynamic memory fragmentation.
They completely eliminate the need for template specialisation by dynamically adjusting data types during program execution.
They allow developers to constrain a template so it only accepts specific types, like integral or floating-point numbers. Submit Answer
Concept Match
Match the STL Container to its Description derekmolloy.ie Drag each definition into its matching concept slot, then click Submit. Tap × to return a placed card to the pool.
Definition Pool
A non-owning view over a contiguous sequence, avoiding unnecessary copies (C++20).
A dynamic array providing fast random access and contiguous memory allocation.
A collection of sorted key-value pairs where keys must be strictly unique.
A doubly linked list ideal for frequent insertions and removals at any position.
Submit
Using C++20 Ranges for Sorting derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 #include <vector>
2 #include <algorithm>
3 #include < ····· >
4
5 int main() {
6 std::vector<int> data = {5, 2, 8, 1, 9};
7
8 // Modern C++20 approach to sorting
9 std:: ····· :: ····· (data);
10 }
Available Snippets
vector
ranges
algorithm
ranges
sort
begin
std
Submit
Which of the following statements correctly contrast Iterator categories in the C++ STL? derekmolloy.ie
A bidirectional iterator cannot be used to modify elements, whereas a random access iterator is strictly read/write only.
A bidirectional iterator moves one step at a time (forward or backward), whereas a random access iterator can jump directly to any index in constant time.
Standard vectors and arrays provide random access iterators, whereas standard lists only provide bidirectional iterators.
A forward iterator can only be dereferenced once per position, whereas an input iterator allows multiple passes over the same data. Submit Answer
In the context of the STL, what exactly is a 'functor' (function object)? derekmolloy.ie
A specialized smart pointer that automatically manages the memory of lambda expressions on the heap.
A class or struct that has overloaded the 'operator()' to allow instantiated objects to be called like a standard function.
An internal compiler mechanism used to enforce C++20 Concepts across multiple translation units.
A keyword used to declare that a particular algorithm has exclusive access to a container's private data. Submit Answer
Predict the output: std::for_each with a Lambda derekmolloy.ie Read the code below, then choose the terminal output it produces and click Submit.
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4
5 int main() {
6 std::vector<int> v = {2, 4, 6};
7 int total = 0;
8
9 std::for_each(v.begin(), v.end(), [&total](int x) {
10 total += x * 2;
11 });
12
13 std::cout << total;
14 return 0;
15 }
Submit Answer
Concept Match
Match the Smart Pointer Concept derekmolloy.ie Drag each definition into its matching concept slot, then click Submit. Tap × to return a placed card to the pool.
Definition Pool
Observes a shared object without increasing its strong reference count; used to break cycles.
Enforces strict single ownership; is exactly 8 bytes on the stack and non-copyable.
Allows multiple owners via reference counting; requires 16 bytes on the stack.
A small heap allocation that stores the strong count, weak count, and custom deleter.
Submit
Why is using 'std::make_shared' generally preferred over directly constructing a shared pointer with 'std::shared_ptr<T>(new T)'? derekmolloy.ie
Because std::make_shared performs a single 'fused' heap allocation for both the object and the control block, improving cache locality.
Because std::make_shared is guaranteed to be completely immune to throwing exceptions during memory allocation.
Because std::make_shared forces the resulting pointer to be non-copyable, actively preventing circular references.
Because std::shared_ptr(new T) is deprecated in C++11 and has been entirely removed from the C++20 standard. Submit Answer
Safely Accessing a Weak Pointer derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 #include <iostream>
2 #include <memory>
3
4 void checkSensor(std::weak_ptr<Sensor> weakSensor) {
5 // Attempt to promote the weak_ptr to a shared_ptr
6 if (auto temp = weakSensor. ····· ()) {
7 std::cout << "Sensor is alive. Value: " << temp-> ····· () << std::endl;
8 } else {
9 std::cout << "Sensor has been destroyed." << std::endl;
10 }
11 }
Available Snippets
acquire
getValue
->
lock
get
value
Submit
Which of the following statements are TRUE regarding custom deleters and smart pointers? derekmolloy.ie
You cannot use std::make_shared or std::make_unique when a custom deleter is required.
Custom deleters automatically disable the smart pointer's internal reference counting mechanism.
You must explicitly call 'delete' (or equivalent cleanup) within the custom deleter to prevent memory leaks.
Custom deleters are only permitted for managing file handles, not dynamically allocated heap memory. Submit Answer
Transferring Ownership of a Unique Pointer derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 #include <memory>
2
3 int main() {
4 // Create the unique pointer safely
5 auto s1 = std:: ····· <Sensor>(101);
6
7 // Transfer ownership to s2; s1 becomes empty
8 std::unique_ptr<Sensor> s2 = std:: ····· ( ····· );
9
10 return 0;
11 }
Available Snippets
s2
copy
make_shared
make_unique
move
transfer
s1
Submit
Implementing a Move Constructor derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 class Buffer {
2 int* data;
3 public:
4 // Move Constructor takes an rvalue reference
5 Buffer(Buffer ····· source) ····· {
6 data = source.data; // Steal the pointer
7 source.data = ····· ; // Prevent double deletion
8 }
9 };
Available Snippets
&
noexcept
nullptr
move
NULL
&&
const
Submit
What are the primary performance and safety benefits of using Move Semantics over traditional deep copying? derekmolloy.ie
It prevents the expensive overhead of allocating new heap memory when returning large objects from functions.
It entirely eliminates the need for constructors and destructors to be executed during the object lifecycle.
It allows containers like std::vector to resize efficiently by transferring resource ownership rather than duplicating data.
It guarantees that source objects are permanently locked in read-only memory after their resources are moved. Submit Answer
In a C++ lambda expression, what does the capture list '[=]' signify? derekmolloy.ie
It captures all variables used inside the lambda from the surrounding scope by reference.
It automatically assigns default integer values to all uninitialised variables used within the lambda.
It captures all variables used inside the lambda from the surrounding scope by value, creating local copies.
It restricts the lambda function, preventing it from accessing any external variables from the surrounding scope. Submit Answer
Why is it considered dangerous (and undefined behaviour) to return a std::string_view that refers to a local std::string variable? derekmolloy.ie
Because std::string_view only supports read-only access and cannot copy the string data before destruction.
Because string_view objects are always heap-allocated, so they cannot safely reference stack-based strings.
Because the compiler strictly enforces that string_view objects must outlive their source strings at compile time.
Because the local std::string is destroyed when the function returns, leaving the string_view pointing at freed memory. Submit Answer
Why is std::optional (C++17) generally considered a robust, 'zero-overhead' error handling choice for Edge Programming? derekmolloy.ie
Because the wrapped value is stored inline directly within the optional object on the stack, requiring no dynamic heap allocation.
Because it explicitly models the absence of a value in the type system, replacing error-prone sentinel values like -1 or nullptr.
Because the compiler automatically offloads the optional validation logic to a dedicated hardware coprocessor during runtime.
Because it guarantees that operations will always succeed, completely eliminating the need for the CPU to perform error-checking. Submit Answer
Congratulations on completing the Chapter 6 Knowledge Test!
Review any questions you missed to ensure a solid grasp of Templates, the STL, and Smart Pointers before moving forward.