Q.1 What is the main advantage of multithreading in a program?
Increases program size
Improves performance by parallel execution
Reduces the need for memory
Simplifies sequential code
Explanation - Multithreading allows multiple threads to run concurrently, improving CPU utilization and program performance.
Correct answer is: Improves performance by parallel execution
Q.2 Which Java keyword is used to create a thread by extending the Thread class?
implement
extends
thread
synchronized
Explanation - In Java, a thread can be created by extending the Thread class using the 'extends' keyword and overriding the run() method.
Correct answer is: extends
Q.3 What is the purpose of the 'synchronized' keyword in Java?
To declare a variable
To prevent thread interference and ensure atomicity
To extend a thread class
To create a new thread
Explanation - The 'synchronized' keyword in Java is used to control access to critical sections and prevent multiple threads from modifying shared data concurrently.
Correct answer is: To prevent thread interference and ensure atomicity
Q.4 Which of the following is a common problem in multithreading?
Deadlock
Recursion
Inheritance
Encapsulation
Explanation - Deadlock occurs when two or more threads are waiting indefinitely for each other to release resources, causing the program to freeze.
Correct answer is: Deadlock
Q.5 What is the difference between a process and a thread?
A process is lighter than a thread
Threads share memory space while processes have separate memory
Threads cannot run concurrently
Processes share memory while threads have separate memory
Explanation - Threads are lightweight and share the same memory of a process, whereas processes have independent memory spaces.
Correct answer is: Threads share memory space while processes have separate memory
Q.6 Which method in Java is used to start a thread?
init()
start()
run()
execute()
Explanation - The start() method is called to begin execution of a thread and it internally calls the run() method.
Correct answer is: start()
Q.7 What does the term 'race condition' refer to in multithreading?
Threads competing to finish first
Multiple threads accessing shared data leading to inconsistent results
A thread running faster than others
Deadlock between threads
Explanation - A race condition occurs when two or more threads access shared data simultaneously and the final outcome depends on the sequence of execution.
Correct answer is: Multiple threads accessing shared data leading to inconsistent results
Q.8 Which method in Java can be used to pause a thread for a specific time?
sleep()
wait()
pause()
stop()
Explanation - The sleep() method pauses the thread execution for a given time without releasing locks.
Correct answer is: sleep()
Q.9 Which of the following is true about thread priorities in Java?
Higher priority threads always execute first
Thread priority guarantees execution order
Thread priority is a suggestion to the scheduler
All threads have the same priority
Explanation - Thread priorities indicate preference, but the actual scheduling depends on the JVM and OS scheduler.
Correct answer is: Thread priority is a suggestion to the scheduler
Q.10 What happens if two synchronized methods of different objects are called by two threads?
Deadlock occurs always
Threads can execute methods concurrently
Threads will wait indefinitely
Only one thread can execute any method in the class
Explanation - Synchronized methods lock on the object instance. If methods are in different objects, threads can execute them concurrently.
Correct answer is: Threads can execute methods concurrently
Q.11 Which interface can be implemented in Java to create a thread?
Runnable
Serializable
Cloneable
Comparable
Explanation - Implementing the Runnable interface allows creating threads by defining the run() method.
Correct answer is: Runnable
Q.12 What is the main difference between wait() and sleep() in Java?
wait() releases lock, sleep() does not
sleep() releases lock, wait() does not
Both release locks
Neither releases locks
Explanation - The wait() method releases the object's lock and pauses execution until notified, whereas sleep() pauses the thread without releasing any lock.
Correct answer is: wait() releases lock, sleep() does not
Q.13 What is 'livelock' in multithreading?
Threads are blocked indefinitely
Threads are active but cannot make progress
Threads execute sequentially
Threads crash the system
Explanation - Livelock occurs when threads keep responding to each other but fail to make any progress, unlike deadlock where they are blocked.
Correct answer is: Threads are active but cannot make progress
Q.14 Which of the following is a benefit of concurrency?
Improves throughput
Reduces program size
Removes bugs automatically
Eliminates memory leaks
Explanation - Concurrency allows multiple tasks to progress simultaneously, improving throughput and resource utilization.
Correct answer is: Improves throughput
Q.15 What is a daemon thread in Java?
A thread that runs only once
A thread that supports other threads and terminates when all user threads finish
A thread with high priority
A thread that cannot be stopped
Explanation - Daemon threads are background threads that provide services to user threads and automatically terminate when no user threads remain.
Correct answer is: A thread that supports other threads and terminates when all user threads finish
Q.16 Which mechanism ensures mutual exclusion in multithreading?
Semaphores
Variables
Inheritance
Polymorphism
Explanation - Semaphores are used to control access to shared resources and ensure that only one thread accesses critical sections at a time.
Correct answer is: Semaphores
Q.17 What is 'thread starvation'?
Threads never start
Threads do not get CPU time due to lower priority
Threads deadlock
Threads execute faster than others
Explanation - Thread starvation occurs when lower-priority threads are unable to execute because higher-priority threads monopolize CPU time.
Correct answer is: Threads do not get CPU time due to lower priority
Q.18 Which of the following is an atomic operation in Java?
Incrementing a shared variable without synchronization
Reading or writing a volatile variable
Calling a synchronized method
All of the above
Explanation - Reading or writing a volatile variable is atomic for all variable types except long and double in Java.
Correct answer is: Reading or writing a volatile variable
Q.19 Which of these Java classes are used for thread pooling?
ExecutorService
ThreadGroup
Runnable
ThreadLocal
Explanation - ExecutorService provides a high-level API for managing a pool of threads and executing tasks concurrently.
Correct answer is: ExecutorService
Q.20 Which method wakes up a single thread waiting on an object's monitor?
notify()
notifyAll()
wake()
signal()
Explanation - The notify() method wakes up one of the threads waiting on the object's monitor, whereas notifyAll() wakes up all waiting threads.
Correct answer is: notify()
Q.21 Which Java class is used to implement reentrant locks?
ReentrantLock
Semaphore
Thread
Runnable
Explanation - ReentrantLock from java.util.concurrent provides explicit locking with features like fairness and reentrancy.
Correct answer is: ReentrantLock
Q.22 What is the difference between process synchronization and thread synchronization?
Thread synchronization is slower
Process synchronization uses shared memory, thread synchronization is within the same process
Thread synchronization prevents deadlock
There is no difference
Explanation - Processes have separate memory spaces, so synchronization often uses inter-process communication, whereas threads share memory within the same process.
Correct answer is: Process synchronization uses shared memory, thread synchronization is within the same process
Q.23 Which of the following is a non-blocking synchronization technique?
Spinlock
Synchronized block
Mutex
Semaphore
Explanation - A spinlock repeatedly checks for resource availability instead of blocking, making it a non-blocking synchronization technique.
Correct answer is: Spinlock
Q.24 What is the output if two threads increment a shared non-synchronized counter variable 1000 times each?
2000
1000
Depends on thread scheduling, may be less than 2000
0
Explanation - Without synchronization, increments can be lost due to race conditions, leading to a final value less than the expected 2000.
Correct answer is: Depends on thread scheduling, may be less than 2000
