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 25 interactive knowledge checks designed to test your understanding of the materials covered in Chapter 3. The questions span C++ history, fundamental types, pointers, control flow, string manipulation, bitwise operations, and exception handling.
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.
Which of the following statements accurately describe the evolution and standardisation of the C++ programming language? derekmolloy.ie
The ISO committee enforces strict backward compatibility, meaning code written for older compilers is guaranteed to compile flawlessly on modern ones.
The C++ language is purely object-oriented, meaning that procedural programming styles (like standard C functions) are strictly prohibited.
C++ was developed by Bjarne Stroustrup as 'C with Classes' to integrate object-oriented features with the low-level memory efficiency of C.
Modern standardisation efforts, such as C++11, C++14, and C++23, continuously update the language to introduce safer and more efficient features. Submit Answer
Complete the Modern C++ Constants and Types derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 #include < ····· >
2
3 // Platform-independent 8-bit unsigned integer
4 ····· hardware_register = 0xFF;
5
6 // Inform the compiler this value might change externally
7 ····· int sensor_reading = 0;
Available Snippets
cstdint
const
stdint
unsigned char
iostream
static
volatile
uint8_t
Submit
1 // Ensure a constant is evaluated at compile time
2 // select an option below
const int MAX_BUFFER_SIZE = 1024;
constexpr int MAX_BUFFER_SIZE = 1024;
volatile int MAX_BUFFER_SIZE = 1024;
static int MAX_BUFFER_SIZE = 1024;
When configuring a C++ compiler like GCC for an embedded project, how do the optimisation flags (e.g., -O0, -O3, -Os) affect the resulting code? derekmolloy.ie
The '-Ofast' flag strictly enforces all ANSI/ISO C++ standards to guarantee mathematical precision, automatically slowing down execution.
The '-Os' flag is specifically designed to reduce the physical size of the compiled machine code, making it ideal for flash-constrained microcontrollers.
The '-O3' flag aggressively applies inlining and loop unrolling to maximize execution speed, though it may increase the final compiled binary size.
The '-O0' flag entirely disables compiler optimisations, ensuring the generated machine code exactly matches the source code for straightforward debugging. Submit Answer
Predict the output of Type Conversion and Division derekmolloy.ie Read the code below, then choose the terminal output it produces and click Submit.
1 #include <iostream>
2
3 int main() {
4 int x = 7;
5 int y = 2;
6 float z = x / y;
7
8 std::cout << z << "\n";
9 std::cout << static_cast<float>(x) / y << "\n";
10 return 0;
11 }
Submit Answer
Concept Match
Match the Modern C++ Construct to its Benefit 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 modern, highly readable alternative to typedef that supports template aliasing (e.g., Name = Value).
A non-arithmetic type representing raw memory bits, preventing accidental numeric calculations.
A scoped enumeration that prevents name collisions and provides strict type safety.
A compiler attribute that issues a warning if the programmer ignores a function's returned value.
Submit
Which of the following statements about C++ Pointers and Memory are correct? derekmolloy.ie
The address-of operator (&) returns the specific physical or virtual memory location where a given variable is stored.
A void* pointer can safely be dereferenced directly to access data, as the compiler automatically determines the correct underlying data type.
The 'nullptr' keyword (introduced in C++11) represents a type-safe null pointer and should always be preferred over the legacy 'NULL' macro.
The dereference operator (*) retrieves the actual data value stored at the memory address currently held by the pointer. Submit Answer
1 struct Sensor {
2 void initialize() {}
3 };
4
5 int main() {
6 Sensor* s = new Sensor();
7 // Access the initialize method via the pointer
8 // select an option below
9 delete s;
10 return 0;
11 }
Complete the Pointer Arithmetic Logic derekmolloy.ie Drag snippets from the pool into the blanks so the program produces the output shown, then click Submit.
1 int data[3] = {10, 20, 30};
2 int *ptr = &data[0];
3
4 // Increment the pointer to point to the next element
5 ptr ····· ;
6
7 // Dereference the pointer to retrieve the current value
8 int current_value = ····· ptr;
9
10 // Safely assign a null state when finished
11 ptr = ····· ;
Submit
Predict the output: Pointer Precedence derekmolloy.ie Read the code below, then choose the terminal output it produces and click Submit.
1 #include <iostream>
2
3 int main() {
4 int arr[] = {5, 10, 15};
5 int *p = arr;
6
7 std::cout << *p++ << " ";
8 std::cout << (*p)++ << " ";
9 std::cout << *p;
10
11 return 0;
12 }
Submit Answer
Why is 'Pass by Const Reference' (e.g., void process(const std::string& data)) considered a modern C++ best practice for large objects? derekmolloy.ie
It automatically converts the object into a C-style char array before passing it into the function block.
The 'const' qualifier strictly guarantees that the function cannot accidentally alter the original data, ensuring program safety.
It passes a lightweight reference rather than creating a full memory copy of the object, significantly improving runtime efficiency.
It allows the function to freely modify the original object in memory, bypassing any encapsulation restrictions applied by the class. Submit Answer
Concept Match
Match the Memory or Scope 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
The designated area within a program (often defined by curly braces {}) where a variable is active and accessible.
A pointer that references a memory location that has already been deallocated or destroyed.
An alias to the original variable is passed, allowing the function to modify the source data directly.
A complete copy of the variable is sent to the function; changes do not affect the original data.
Submit
Which of the following describes the differences between C-style strings (<cstring>) and modern C++ strings (<string>)? derekmolloy.ie
Modern std::string objects automatically manage their own underlying memory and can be safely concatenated using the standard '+' operator.
The modern 'std::string_view' provides a highly efficient, read-only view of a string without triggering costly memory allocations.
C-style strings are fundamental arrays of characters that must be manually terminated with a special null character ('\0').
The C standard library function 'strcmp()' is used to intuitively combine two C-style strings together without memory overflow risks. Submit Answer
Predict the output: Loops and Break/Continue derekmolloy.ie Read the code below, then choose the terminal output it produces and click Submit.
1 #include <iostream>
2
3 int main() {
4 for(int i = 1; i <= 5; ++i) {
5 if (i == 3) { continue; }
6 if (i == 5) { break; }
7 std::cout << i;
8 }
9 return 0;
10 }
Submit Answer
Efficient String Handling derekmolloy.ie 1 #include <iostream>
2 #include <string_view>
3
4 // Pass a read-only view of a string without copying
5 // select an option below
6 std::cout << "Status: " << message << std::endl;
7 }
void print_status(std::string message)
void print_status(const char* message)
void print_status(std::string& message)
void print_status(std::string_view message)
Modern Range-Based For Loop 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 <vector>
3
4 int main() {
5 std::vector<int> numbers = {10, 20, 30};
6
7 // Safely iterate through the vector without copying elements
8 for ( ····· auto ····· num : numbers) {
9 std::cout << num << " ";
10 }
11 return 0;
12 }
Submit
Based on the 'Robot Arena' Virtual Lab, why is introducing a mutable variable (like 'ammo') significantly different from pure control flow (like a 'while' loop)? derekmolloy.ie
A mutable variable introduces persistent state, meaning the program must actively track and update a value that changes dynamically over time.
It eliminates the need for any block scope braces in the source code, as the variable handles all logic branching automatically.
It forces the programmer to read the variable's current value inside a conditional (e.g., if ammo == 0) to make real-time operational decisions.
Mutable variables automatically pause the execution of the CPU, giving the hardware time to synchronise with external moving targets. Submit Answer
Concept Match
Match the String Function to its Behavior derekmolloy.ie Drag each definition into its matching concept slot, then click Submit. Tap × to return a placed card to the pool.
Definition Pool
Accesses a character in a C++ string safely by performing runtime bounds checking.
A C-style function that calculates string length by counting bytes until it hits the null terminator.
A C++ function used with cin to read an entire line of text, including spaces and tabs.
A C-style function that appends one char array to another, which can cause buffer overflows if not careful.
Submit
Why are bitwise operations considered essential for low-level embedded edge programming? derekmolloy.ie
They automatically translate human-readable string commands into secure, encrypted binary machine code payloads.
They allow multiple floating-point values to be securely compressed into a single 8-bit character array.
They allow developers to set, clear, or toggle specific individual bits within a hardware configuration register without disturbing adjacent bits.
They provide an extremely fast, computationally efficient method to perform multiplication and division by powers of two using shift operators. Submit Answer
Predict the output: Bitwise Shifting derekmolloy.ie Read the code below, then choose the terminal output it produces and click Submit.
1 #include <iostream>
2 #include <cstdint>
3
4 int main() {
5 uint8_t a = 5; // Binary: 00000101
6 uint8_t b = a << 2;
7 uint8_t c = b >> 1;
8
9 std::cout << (int)b << " " << (int)c;
10 return 0;
11 }
Submit Answer
1 uint8_t flags = 0b00000001;
2 // Use bitwise XOR to toggle the state of the first bit
3 // select an option below
Structure a C++ Exception Handler 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 <vector>
3 #include <stdexcept>
4
5 int main() {
6 std::vector<int> data = {1, 2, 3};
7 ····· {
8 int value = data.at(10); // Will throw out_of_range
9 }
10 ····· (const std:: ····· & e) {
11 std::cerr << "Error: " << e. ····· () << "\n";
12 }
13 return 0;
14 }
Available Snippets
message
what
fail
throw
catch
exception
out_of_range
error
try
Submit
Concept Match
Match the Operator or Exception 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
Compares two numbers bit-by-bit; the resulting bit is 1 only if the corresponding bits are different.
The process where the runtime system safely destroys local objects as it searches upward for an exception handler.
A keyword promising the compiler that a specific function will absolutely never throw an exception.
A unary operator that inverts all the bits of a number, turning 1s to 0s and vice versa.
Submit
Catching Exceptions by Reference derekmolloy.ie 1 try {
2 risky_operation();
3 }
4 // Catch the standard exception by constant reference
5 // select an option below
6 std::cerr << "Caught standard exception";
7 }
catch (const std::exception& e)
Regarding operator precedence in C++, why is it heavily recommended to wrap bitwise operations in parentheses when evaluating conditions? derekmolloy.ie
Because bitwise operators (like &, |) often have a lower precedence than equality operators (like ==), leading to unintended evaluation order.
Because the compiler will immediately crash and refuse to compile any bitwise logic that lacks enclosing parentheses.
Because relying on obscure precedence rules makes code incredibly difficult for other developers to read and maintain.
Because parentheses physically instruct the CPU's arithmetic logic unit (ALU) to cache the values in faster SRAM. Submit Answer
Congratulations on completing the Chapter 3 Knowledge Test!
Review any questions you missed to ensure a solid foundation in the “C of C++” before moving on to the advanced Object-Oriented implementations in Chapter 5.