🌍 C++ Programming · flashcards

C++ Programming Advanced Topics Flashcards

62 question-and-answer cards covering Advanced Topics as it is examined in C++ Programming. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.

62Cards in deck
24Free preview
17Syllabus topics
~254Chars per answer
FreePrice

24 sample cards from the Advanced Topics deck

Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.

  1. Name two standard predefined function objects from `<functional>` and their use.

    Examples: `std::less<T>` (returns `a < b`, the default comparator for sorted containers and `sort`), `std::greater<T>` (returns `a > b`, for descending order), `std::plus<T>`, `std::multiplies<T>` used with `accumulate`/`transform`.

  2. What are the three main file stream classes in C++ and the header they come from?

    From `<fstream>`: `std::ifstream` (input/read from file), `std::ofstream` (output/write to file), and `std::fstream` (both input and output).

  3. List the common file open modes (`std::ios` flags) and what each does.

    `ios::in` (read), `ios::out` (write), `ios::app` (append to end), `ios::ate` (seek to end on open), `ios::trunc` (truncate/discard existing contents), `ios::binary` (binary mode). Flags can be combined with the bitwise OR operator `|`.

  4. How do you check whether a file was opened successfully in C++?

    Test the stream in a boolean context or call `is_open()`: e.g. `std::ifstream f("data.txt"); if (!f) { /* failed */ }` or `if (!f.is_open()) { ... }`.

  5. What is RAII with respect to file streams, and when is a file closed automatically?

    File stream objects follow RAII: the file is opened in the constructor and closed automatically in the destructor when the stream object goes out of scope, so an explicit `close()` is often unnecessary (though useful to release the resource early).

  6. Give two common ways to read text from a file line by line versus token by token.

    Line by line: `std::getline(inFile, line)` reads a whole line into a `std::string`. Token by token: the extraction operator `inFile >> token` reads whitespace-delimited tokens. Both are typically used in a `while` loop that continues while the stream stays in a good state.

  7. How do you detect end-of-file correctly when reading in a loop?

    Test the read operation itself in the loop condition, e.g. `while (inFile >> x)` or `while (std::getline(inFile, line))`. Do NOT loop on `while (!inFile.eof())`, because `eof()` is only set after a failed read, causing the last item to be processed twice.

  8. How do you write formatted text to a file, and how do you flush it?

    Use the insertion operator: `outFile << value << '\n';`. Output is flushed automatically on close/destruction, or manually with `outFile.flush()` or by inserting `std::endl` (which writes a newline AND flushes).

  9. What is the difference between text mode and binary mode file I/O?

    Text mode may translate characters (e.g. newline conversions on Windows) and is meant for human-readable data. Binary mode (`ios::binary`) writes/reads raw bytes with no translation, preserving the exact byte representation—required for non-text data and reliable fixed-size records.

  10. Which member functions read and write raw binary data, and what are their parameter types?

    `ostream::write(const char* buffer, std::streamsize count)` writes raw bytes, and `istream::read(char* buffer, std::streamsize count)` reads raw bytes. The object pointer is typically cast with `reinterpret_cast<char*>(&obj)` and `count` is `sizeof(obj)`.

  11. What functions reposition the read/write position in a file, and what does the tell-family return?

    `seekg(pos)`/`seekp(pos)` set the get (read) and put (write) positions; `tellg()`/`tellp()` return the current get/put positions. `seekg` also accepts an offset and a base such as `ios::beg`, `ios::cur`, or `ios::end`.

  12. What header and class launch a new thread in C++11, and how is the thread function supplied?

    The `<thread>` header provides `std::thread`. A thread is launched by constructing a `std::thread` object with a callable (function, lambda, or functor) and its arguments, e.g. `std::thread t(func, arg1);`, which begins execution immediately.

  13. What is the difference between `join()` and `detach()` on a `std::thread`?

    `join()` blocks the calling thread until the target thread finishes, then releases its resources. `detach()` separates the thread to run independently in the background. Exactly one of the two must be called before the `std::thread` is destroyed, or `std::terminate` is invoked.

  14. What is a data race, and what is the general rule for avoiding one?

    A data race occurs when two or more threads access the same memory location concurrently, at least one access is a write, and there is no synchronization; the result is undefined behavior. The rule: protect shared mutable data with synchronization (mutexes, atomics) so accesses do not overlap.

  15. What is a `std::mutex` and what are its two core operations?

    A `std::mutex` (from `<mutex>`) is a mutual-exclusion primitive that lets only one thread hold it at a time, protecting a critical section. Its core operations are `lock()` (acquire, blocking until available) and `unlock()` (release).

  16. Why prefer `std::lock_guard` or `std::unique_lock` over calling `mutex.lock()`/`unlock()` directly?

    They are RAII wrappers that lock the mutex on construction and automatically unlock on destruction (including during exception unwinding), preventing forgotten unlocks and deadlocks from early returns or thrown exceptions.

  17. How does `std::unique_lock` differ from `std::lock_guard`?

    `std::lock_guard` is minimal: it locks on construction and unlocks on destruction with no other operations. `std::unique_lock` is more flexible—it supports deferred locking, manual `lock()`/`unlock()`, timed locking, ownership transfer (movable), and is required for use with condition variables.

  18. What is a deadlock, and name one strategy to prevent it when locking multiple mutexes.

    A deadlock is a state where two or more threads each wait forever for a lock held by another. Prevention strategies include always acquiring multiple locks in a consistent global order, or using `std::lock(m1, m2)` / `std::scoped_lock(m1, m2)` which lock several mutexes atomically without deadlock.

  19. What synchronization primitive is used for thread communication where one thread waits for a condition set by another?

    `std::condition_variable` (from `<condition_variable>`). One thread waits on it (releasing an associated `std::unique_lock`), and another thread signals with `notify_one()` or `notify_all()` when the condition changes.

  20. Why must `condition_variable::wait` be used with a predicate (or in a loop), and how is the predicate form written?

    To guard against spurious wakeups and lost/early notifications, `wait` should re-check a condition. The predicate form `cv.wait(lock, []{ return ready; });` atomically releases the lock, sleeps, and only returns when both notified and the predicate is true—equivalent to a `while (!pred()) cv.wait(lock);` loop.

  21. What is the difference between `notify_one()` and `notify_all()` on a condition variable?

    `notify_one()` wakes a single thread waiting on the condition variable (if any). `notify_all()` wakes all waiting threads, which then re-contend for the lock and re-check their predicates; use it when a state change could satisfy multiple waiters (e.g. a broadcast).

  22. How does `std::future` and `std::promise` enable thread communication for returning a value?

    A `std::promise<T>` is set by the producing thread via `set_value()` (or `set_exception()`), and the consuming thread retrieves the result through the associated `std::future<T>` using `get()`, which blocks until the value is ready. This provides a one-time, thread-safe channel for a result or exception.

  23. What does `std::atomic<T>` provide, and how does it differ from using a mutex?

    `std::atomic<T>` (from `<atomic>`) makes individual read-modify-write operations on a variable indivisible and race-free without an explicit lock, typically using hardware atomic instructions. It is lighter-weight than a mutex for simple shared variables (e.g. counters/flags) but does not protect multi-variable invariants, which still require a mutex.

  24. What is the purpose of `std::this_thread::sleep_for` and `std::this_thread::get_id`?

    `std::this_thread::sleep_for(duration)` blocks the current thread for at least the given time span (e.g. `std::chrono::milliseconds(100)`). `std::this_thread::get_id()` returns the unique `std::thread::id` of the currently executing thread, useful for logging and identification.

What this deck covers

The Advanced Topics deck follows the C++ Programming Advanced Topics syllabus — 5 chapters and 17 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 12.4 cards per chapter.

Answers are written to be recallable, not just readable — averaging about 254 characters, which is long enough to carry the reasoning and short enough to say out loud.

A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.

Advanced Topics flashcards FAQ

How many Advanced Topics flashcards are in this C++ Programming deck?

62 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.

Are these C++ Programming flashcards free?

Yes. The preview here is free to read with no signup, and the full 62-card deck is free inside the Examius app.

What do the Advanced Topics cards cover?

They follow the C++ Programming Advanced Topics syllabus — 5 chapters and 17 topics — so the questions track what is actually examinable.

How should I use these flashcards?

Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.