Explore foundational concepts of multithreading and task parallelism in C# with these easy questions tailored for beginners. This quiz covers thread creation, synchronization, task management, and key related terminology important for efficient and safe concurrent programming in C#.
Which method is commonly used to start a new thread in C# for concurrent execution of code?
Explanation: Thread.Start() is the standard method to begin execution of a new thread in C#. Thread.Launch() and Task.BeginInvoke() are not valid methods for thread initiation. Task.Begin() does not exist either; the correct methods with the Task class involve Task.Run or Task.Start. Always ensure you use accurate method names to avoid errors.
In C#, which class is primarily used to represent an asynchronous operation that can be awaited?
Explanation: Task is the main class in C# used for representing asynchronous operations and supports being awaited for completion. Thread is used for lower-level management of threads but isn't directly awaitable. Processor and Queue are unrelated to asynchronous operation representation. Using Task helps simplify and manage parallel operations effectively.
What is a race condition in the context of multithreading in C#?
Explanation: A race condition occurs when two or more threads attempt to access and modify shared data at the same time, potentially leading to corrupted results. A single thread taking too long is just a performance issue, not a race condition. Processing tasks on a queue relates to task scheduling, not thread interference. A suspended thread isn't specifically indicative of a race condition.
Which keyword in C# is used to prevent multiple threads from entering a critical section at the same time?
Explanation: The lock keyword in C# ensures that only one thread can enter a critical section, preventing data corruption. threadsafe, protect, and secure are not recognized C# keywords for this purpose. They may sound related to security or safety but do not provide synchronization by themselves.
What is the main advantage of using a thread pool in C# compared to creating new threads for each task?
Explanation: Thread pools reduce the overhead of creating and destroying threads by reusing existing ones. Pool threads do not inherently run faster than regular threads; they just avoid startup costs. Pools still respect system resource limitations, and threads can run on multiple cores. Efficient reuse is the key benefit.
Which namespace in C# contains core types for working with tasks and parallelism?
Explanation: System.Threading.Tasks holds classes such as Task, Parallel, and related constructs for parallel programming. System.Concurrency and System.Async are not valid C# namespaces, and System.Threads.Parallel does not exist. Always use System.Threading.Tasks for task parallelism.
Which collection should be used in C# when multiple threads need to safely add or remove items concurrently?
Explanation: ConcurrentQueue is designed for safe access when multiple threads perform operations at the same time. List, ArrayList, and HashSet are not thread-safe and can cause issues if modified from multiple threads. Using ConcurrentQueue helps avoid data corruption during concurrent access.
Which C# object can be used to request cancellation of one or more running tasks?
Explanation: CancellationTokenSource issues tokens that tasks can observe for cancellation requests. TaskKiller and ThreadTerminator are not actual C# classes or standard concepts, and Stopwatch is used for measuring elapsed time. Proper task cancellation is enabled by CancellationTokenSource.
What is the primary purpose of using Thread.Sleep(milliseconds) in C# multithreading?
Explanation: Thread.Sleep pauses execution of the current thread for the given number of milliseconds. It does not end a thread, switch processor contexts directly, or adjust thread priority. Using Thread.Sleep can sometimes help simulate delays or give other threads a chance to run.
Which method is used to define an action that runs when a C# Task completes?
Explanation: ContinueWith() is used to specify an action that should run after a Task has finished. RunLater, Chain, and AddAfter do not exist in task programming in C#. Continuations allow workflows to be chained cleanly after asynchronous operations.