C++ Threads Fundamentals Quiz Quiz

Explore essential concepts of threads in C++ with these beginner-level questions, covering thread creation, usage, synchronization, and behavior. Perfect for those new to multithreading in C++ and wanting to test their grasp of thread basics, parallel tasks, and related programming practices.

  1. Thread Library Inclusion

    Which header file must you include in a C++ program to use threads like std::thread?

    1. #include <thread>
    2. #include <stdio>
    3. #include <functools>
    4. #include <tasks>

    Explanation: The correct header to include for thread support in modern C++ is <thread>. <stdio> is for standard input/output, not threading. <functools> and <tasks> are not valid standard C++ headers, so they will not allow you to use std::thread.

  2. Function Used in Thread

    When creating a thread to run a function named taskOne(), how do you pass it to std::thread?

    1. std::thread t1(taskOne);
    2. std::thread t1 = taskOne();
    3. std::thread t1->taskOne;
    4. std::thread t1.taskOne();

    Explanation: You pass the function to the constructor of std::thread by writing std::thread t1(taskOne);. Assigning with = would try to invoke the function instead. The arrows and dots used in the other options are not valid syntax for std::thread initialization.

  3. Main Purpose of Threads

    What is the fundamental benefit of using threads in a C++ program, such as the kitchen example with multiple cooks?

    1. To run multiple tasks simultaneously
    2. To reduce the code size
    3. To remove syntax errors
    4. To speed up compilation

    Explanation: Threads allow a program to execute multiple tasks at the same time, making use of multi-core processors. Reducing code size and speeding up compilation are unrelated to threads. Removing syntax errors is not a function of threading.

  4. Thread Join Purpose

    What does calling t1.join() do when used after starting a thread in C++?

    1. Waits for the thread to finish before continuing
    2. Stops the thread immediately
    3. Ignores the thread's work
    4. Repeats the thread's function

    Explanation: The join() function pauses the main thread until the thread represented by t1 completes its task. Stopping, ignoring, or repeating the thread's function are not performed by join(). The other options are incorrect or misleading regarding thread synchronization.

  5. Consequence of Omitting Join

    What may happen if you forget to call join() or detach() on a std::thread before main() ends?

    1. The program may crash or terminate abnormally
    2. The thread automatically finishes its work
    3. It has no effect on the program
    4. The thread is paused indefinitely

    Explanation: If a thread is neither joined nor detached before main() ends, the program risks crashing or producing undefined behavior. Threads do not automatically finish; ignoring them can leave the process in a bad state. The paused and no-effect options are incorrect.

  6. Thread Detach Behavior

    What is the result of calling t1.detach() after starting a thread in C++?

    1. The thread runs independently in the background
    2. The thread is deleted instantly
    3. The thread runs twice
    4. The main thread waits for its completion

    Explanation: detach() allows the thread to execute independently, separating it from the main thread's lifetime. The thread is not deleted, nor does it run twice. The main thread does not wait for the detached thread's completion; that's what join() would do.

  7. Output Order Example

    If you detach a thread running taskOne() that prints 'Cooking rice' and immediately print 'Dinner is ready' in main, which output may appear first?

    1. Dinner is ready
    2. Cooking rice
    3. An error message
    4. No output will appear

    Explanation: Since the detached thread runs asynchronously, 'Dinner is ready' in main could print first if main finishes before the detached thread. 'Cooking rice' might appear second or potentially not at all if the thread doesn't run in time. Error and no output are incorrect in this expected scenario.

  8. Appropriate Thread Function Signature

    Which of the following is a valid function signature for a thread function in C++ if it takes no parameters and returns nothing?

    1. void taskOne()
    2. int taskOne(int x)
    3. taskOne()
    4. float taskOne()

    Explanation: A thread function that returns nothing and takes no parameters should have the signature void taskOne(). The other options either have parameters, return a different type, or omit the return type and are thus unsuitable.

  9. Thread Object Naming

    In the code std::thread t1(taskOne);, what does t1 refer to?

    1. The thread object
    2. The function result
    3. Input data
    4. A file pointer

    Explanation: t1 is the name of the thread object created to manage and control the new thread. It is not the function result, input data, or a file pointer. These distractor options are unrelated to thread objects.

  10. Order of Execution Without Join

    If t1.join() is commented out and main() finishes quickly, what happens to the thread running taskOne()?

    1. It may be terminated before completion
    2. It always finishes normally
    3. It starts after main ends
    4. It runs inside main() only

    Explanation: If main finishes first, the program may end before the thread completes, causing it to be terminated. It is not guaranteed to finish, will not start after main ends, and does not run inside main. Thus, the other options are incorrect for thread lifecycle.

  11. Correct Thread Ending Procedure

    Before the main() function ends in a C++ program using threads, what must be done for each std::thread?

    1. Call either join() or detach()
    2. Delete the thread pointer
    3. Call run()
    4. Set the thread to null

    Explanation: To avoid abnormal termination or resource leaks, each std::thread must be either joined or detached before main returns. Deleting thread pointers, calling run(), or setting to null are not valid or necessary procedures for thread management.

  12. Threaded Function Arguments

    If you want to run a function with arguments in a new C++ thread, what is a valid syntax?

    1. std::thread t1(myFunction, arg1, arg2);
    2. std::thread t1 = myFunction(arg1, arg2);
    3. myFunction.thread(arg1, arg2);
    4. std::thread t1(myFunction[arg1, arg2]);

    Explanation: Arguments are passed after the function in the thread constructor. Assigning with = would call the function instead. The other options use invalid syntax, such as using dot notation or brackets, which do not create threads properly.

  13. Synchronized Output with Join

    Why does calling join() on a thread before printing 'Dinner is ready' ensure correct message order?

    1. It waits for the thread to finish before proceeding
    2. It speeds up the thread
    3. It prints both lines at the same time
    4. It blocks the thread from running

    Explanation: join() guarantees that the main thread will not print 'Dinner is ready' until the thread task is complete. It does not speed up or block the thread, nor does it cause simultaneous prints. Only waiting for completion is correct here.

  14. Threading Parallelism Analogy

    In the kitchen example, why does having five cooks illustrate multithreading?

    1. Multiple people can work on different dishes at once
    2. The kitchen is smaller
    3. It uses only one recipe
    4. Only one person can cook

    Explanation: Having five cooks demonstrates multiple parallel tasks, which matches how threads allow simultaneous operations. The other options either reference a single worker or unrelated kitchen traits that do not metaphorically reflect threads.

  15. Thread Functionality Limitation

    Which of the following is NOT directly achieved by using C++ threads?

    1. Preventing syntax errors in code
    2. Running functions in parallel
    3. Better CPU utilization
    4. Asynchronous task execution

    Explanation: Threads are about executing tasks in parallel and using resources efficiently, not fixing syntax errors. Running functions in parallel, resource utilization, and asynchronous task execution are all direct benefits, so only syntax errors are unrelated.

  16. Printing in a Thread Function

    In a thread function printing 'Cooking ricen', why is std::endl or n included after the message?

    1. To start a new line in the output
    2. To delay the output
    3. To clear the screen
    4. To end the program

    Explanation: Using std::endl or the newline character adds a new line after the printed message for readability. It does not delay output, clear the screen, or end the program. These other options misrepresent the purpose of line-breaks in output.