Any2Quiz
Quizzes / Computer Science / Languages / C++

Multithreading Quiz - Languages C++

Free practice quiz: Multithreading (Languages C++). 51 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

This Languages C++ practice quiz has 51 multiple-choice questions on Multithreading, and 51 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.

Start quiz: 51 questions, free

Sample questions

  1. Which C++ standard library header provides classes and functions for multithreading support?

    Show answer

    Answer: A. <thread>

    The `<thread>` header in C++11 and later provides the `std::thread` class, which is the fundamental building block for creating and managing threads.

  2. What is `std::future` used for in C++ multithreading?

    Show answer

    Answer: B. To represent the result of an asynchronous operation that may not have completed yet.

    `std::future` objects are used to retrieve values from asynchronous operations (like those launched by `std::async`) or from promises. The `get()` method of a future will block until the result is available.

  3. What is the primary role of `std::this_thread::sleep_for()`?

    Show answer

    Answer: B. To pause the current thread for a specified duration.

    `std::this_thread::sleep_for()` causes the currently executing thread to sleep (become dormant) for a specified duration. This is often used to limit CPU usage or introduce delays in concurrent operations.

  4. What does the `std::once_flag` object do when used with `std::call_once`?

    Show answer

    Answer: B. It is a synchronization mechanism to track whether the associated function has already been executed.

    The `std::once_flag` is passed to `std::call_once` to ensure that the target function is executed exactly one time across all threads. The `once_flag` internally manages the state to achieve this guarantee.

  5. What is the primary purpose of `std::packaged_task` when it is invoked?

    Show answer

    Answer: B. To execute the wrapped callable and store its result/exception in a shared state accessible via a future.

    When a `std::packaged_task` is invoked (called like a function), it executes the task it wraps. The result (or exception thrown) is then stored in an internal shared state, which can be retrieved by another thread using the `std::future` obtained from `get_future()` on the `packaged_task`.

Take the full quiz

More questions in this quiz

Answers and explanations are in the full quiz.

  1. What is the primary purpose of `std::thread::join()`?
  2. What happens if you call `std::thread::detach()` on a thread?
  3. What is a race condition in multithreading?
  4. Which C++ synchronization primitive is used to prevent multiple threads from accessing a shared resource concurrently?
  5. What is the purpose of `std::lock_guard`?
  6. What is `std::unique_lock` primarily used for?
  7. Which C++ concurrency feature allows threads to wait for a specific condition to become true?
  8. What is a deadlock in the context of multithreading?
  9. What is the main advantage of using `std::async` over directly creating `std::thread`?
  10. What is the purpose of `std::promise`?
  11. What does the `std::call_once` mechanism ensure?
  12. What is a thread-local storage (TLS) variable?
  13. Which C++ concurrency primitive is suitable for implementing a producer-consumer pattern?
  14. What is the potential issue with using `std::mutex` for protecting a shared resource?
  15. What is the difference between `std::mutex` and `std::recursive_mutex`?
  16. What is the purpose of `std::scoped_lock` (C++17)?
  17. What is a task-based parallelism approach in C++?
  18. What does `std::thread::hardware_concurrency()` typically return?
  19. Which of the following is an example of a potential deadlock scenario?
  20. What is the purpose of a `std::atomic` variable?
  21. When might you choose `std::shared_mutex` over `std::mutex`?
  22. What is a thread pool?
  23. What is the purpose of a barrier synchronization primitive?
  24. Which C++ concurrency primitive is used to signal an event and wake up waiting threads?
  25. What is the overhead associated with `std::thread` creation?
  26. What is the function of `std::this_thread::yield()`?
  27. What is a major challenge when using `std::async` with `std::launch::async`?
  28. What is a potential pitfall of using `std::thread` without proper synchronization?
  29. What is `std::packaged_task` used for?
  30. What is a key difference between `std::lock_guard` and `std::unique_lock` regarding flexibility?
  31. What is the potential issue with busy-waiting (e.g., using a `while` loop to check a condition without sleeping)?
  32. What does `std::memory_order` control in atomic operations?
  33. What is the purpose of `std::packaged_task` and `std::promise` in relation to `std::future`?
  34. What is the primary difference between `std::thread` and `std::async` in terms of return value handling?
  35. Which of the following is an example of a fine-grained locking strategy?
  36. What is the purpose of `std::lock` (C++11) when used with multiple mutexes?
  37. What is a significant risk of having too many threads in a system?
  38. What is the role of `std::timed_mutex`?
  39. What is the primary purpose of `std::once_flag`?
  40. What is the purpose of the `std::thread::hardware_concurrency()` function?
  41. What is the difference between `std::lock_guard` and `std::scoped_lock`?
  42. What does `std::atomic_flag` provide that `std::atomic<bool>` does not directly support for basic operations?
  43. What is the purpose of `std::promise::set_value()`?
  44. When using `std::async` with the default launch policy (`std::launch::async | std::launch::deferred`), what determines whether the task runs immediately or lazily?
  45. What is a key advantage of using `std::async` with `std::launch::deferred`?
  46. What does it mean for a `std::thread` object to be 'joinable'?

More quizzes

Browse all C++ quizzes

More to play

Daily ChallengeA new quiz every day. Keep your streak.HangmanGuess the word, letter by letter.Story QuestLearn through short interactive stories.All quizzesCBSE, ICSE, competitive exams and more.