This set of C# Interview Questions and Answers for freshers focuses on “Multithreaded Programming II”.
1. Which of these keywords are used to implement synchronization?
2. Which keyword is used for using the synchronization features defined by the Monitor class?
Explanation: The C# keyword lock is really just shorthand for using the synchronization features defined by the Monitor class, which is defined in the System.Threading namespace.
3. What is synchronization in reference to a thread?
a) Its a process of handling situations when two or more threads need access to a shared resource
b) Its a process by which many thread are able to access same shared resource simultaneously
c) Its a process by which a method is able to access many different threads simultaneously
d) Its a method that allow to many threads to access any information the require
Explanation: When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization.
4. Which method is called when a thread is blocked from running temporarily?
d) Both Pulse() & Wait()
Explanation: When a thread is temporarily blocked from running, it calls Wait( ). This causes the thread to go to sleep and the lock for that object to be released, allowing another thread to acquire the lock.
5. What kind of exception is being thrown if Wait(),Pulse() or PulseAll() iscalled from code that is not within synchronized code?
a) System I/O Exception
b) DivideByZero Exception
d) All of the mentioned
Explanation: A SynchronizationLockException will be thrown if Wait(), Pulse(), or PulseAll() is called from code that is not within synchronized code, such as a lock block.
6. What is mutex?
a) a mutually exclusive synchronization object
b) can be acquired by more than one thread at a time
c) helps in sharing of resource which can be used by one thread
d) all of the mentioned
Explanation: A mutex is a mutually exclusive synchronization object. This means it can be acquired by one and only one thread at a time. The mutex is designed for those situations in which a shared resource can be used by only one thread at a time.
7. What is Semaphore?
a) Grant more than one thread access to a shared resource at the same time
b) Useful when a collection of resources is being synchronized
c) Make use of a counter to control access to a shared resource
d) All of the mentioned
Explanation: A semaphore is similar to a mutex except that it can grant more than one thread access to a shared resource at the same time. Thus, the semaphore is useful when a collection of resources is being synchronized. A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, access is denied.
8. Which method is used to abort thread prior to it’s normal execution?
Explanation: To terminate a thread prior to its normal conclusion, use Thread.Abort( ). Its simplest form is shown here:
public void Abort()
Abort() causes a ThreadAbortException to be thrown to the thread on which Abort() is called. This exception causes the thread to terminate.
9. Which of these statements is incorrect?
a) By multithreading CPU’s idle time is minimized, and we can take maximum use of it
b) By multitasking CPU’s idle time is minimized, and we can take maximum use of it
c) Two thread in Csharp can have same priority
d) A thread can exist only in two states, running and blocked
Explanation: Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.
10. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously
b) It’s a process in which two or more parts of same process run simultaneously
c) Its a process in which many different process are able to access same information
d) Its a process in which a single process can access information from many sources
Explanation: Multithreaded programming a process in which two or more parts of same process run simultaneously.
Sanfoundry Global Education & Learning Series – C# Programming Language.
To practice all areas of C# for Interviews, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.