In Java, a thread is a lightweight process that allows for concurrent execution within a program.
2. How do you start a thread in Java?
a) By calling the run() method
b) By calling the start() method
c) By creating an instance of Thread
d) By using a loop
Answer:
b) By calling the start() method
Explanation:
To start a thread in Java, you create an instance of the Thread class and call its start() method.
3. What does the Runnable interface contain?
a) Multiple methods
b) Only the run() method
c) A variable
d) Several constructors
Answer:
b) Only the run() method
Explanation:
The Runnable interface in Java contains only one method, run(), which is implemented by classes that intend to execute code on a thread.
4. What is the purpose of the run() method in a thread?
a) To initialize thread properties
b) To define the code executed by the thread
c) To stop the thread
d) To pause the thread execution
Answer:
b) To define the code executed by the thread
Explanation:
The run() method defines the code that is executed when the thread starts running.
5. Which method is used to check if a thread is alive?
a) isRunning()
b) isAlive()
c) checkStatus()
d) getStatus()
Answer:
b) isAlive()
Explanation:
The isAlive() method in the Thread class is used to check if a thread is still running.
6. Can we call the run() method directly to start a thread?
a) Yes
b) No
c) Only in certain conditions
d) Only after calling start()
Answer:
b) No
Explanation:
Calling the run() method directly does not start a new thread; it just executes the run() method in the current thread.
7. What is thread priority in Java?
a) The order in which threads are created
b) The order in which threads are executed
c) A measure of how often a thread should be executed
d) A setting that determines thread behavior
Answer:
c) A measure of how often a thread should be executed
Explanation:
Thread priority in Java is a measure that indicates how often a thread should be given time to execute, relative to other threads.
8. What happens when you call the sleep() method on a thread?
a) The thread starts executing immediately
b) The thread stops permanently
c) The thread execution is paused for a specified duration
d) The thread's execution is permanently terminated
Answer:
c) The thread execution is paused for a specified duration
Explanation:
The sleep() method pauses the execution of the current thread for a specified duration.
9. Can two threads share the same instance of a variable?
a) Yes
b) No
c) Only static variables
d) Only final variables
Answer:
a) Yes
Explanation:
Threads can share the same instance of a variable, especially if the variable is a member of an object accessed by multiple threads.
10. What is synchronization in the context of Java threads?
a) Running multiple threads simultaneously
b) Ensuring only one thread accesses a resource at a time
c) Speeding up thread execution
d) Creating multiple instances of a thread
Answer:
b) Ensuring only one thread accesses a resource at a time
Explanation:
Synchronization is a mechanism that ensures that only one thread at a time accesses a shared resource or critical section.
11. What keyword is used to synchronize a method or block in Java?
a) sync
b) synchronized
c) lock
d) atomic
Answer:
b) synchronized
Explanation:
The synchronized keyword is used to lock a method or a block so that only one thread can access it at a time.
12. What is a deadlock in multithreading?
a) When a thread is terminated unexpectedly
b) When a thread runs indefinitely
c) When two or more threads are waiting forever for each other to release resources
d) When a thread is paused
Answer:
c) When two or more threads are waiting forever for each other to release resources
Explanation:
A deadlock is a situation in multithreading where two or more threads are blocked forever, waiting for each other to release the resources they need.
13. What is the Thread class's join() method used for?
a) To start a thread
b) To pause a thread
c) To make a thread wait for another thread's completion
d) To check the status of a thread
Answer:
c) To make a thread wait for another thread's completion
Explanation:
The join() method is used in a thread to make the current thread wait until the specified thread completes its execution.
14. Which method is used to yield execution to another thread?
a) yield()
b) sleep()
c) stop()
d) wait()
Answer:
a) yield()
Explanation:
The yield() method in the Thread class causes the currently executing thread to pause and allow other threads to execute.
15. What does thread safety mean in Java?
a) Executing threads in a sequential manner
b) Ensuring a thread runs without interruption
c) Protecting data from corruption when accessed by multiple threads
d) Locking a thread until its execution is complete
Answer:
c) Protecting data from corruption when accessed by multiple threads
Explanation:
Thread safety refers to the protection of data from corruption or inconsistent results when it is accessed by multiple threads simultaneously.
16. What is a daemon thread in Java?
a) A high-priority thread
b) A low-priority thread
c) A thread that runs in the background
d) The main thread of an application
Answer:
c) A thread that runs in the background
Explanation:
Daemon threads in Java are background threads that provide services to user threads. They do not prevent the JVM from exiting when all user threads have finished executing.
17. How do you set a thread as a daemon thread in Java?
a) Using the setDaemon() method
b) By calling the start() method
c) Using the daemon keyword
d) By inheriting from the Daemon class
Answer:
a) Using the setDaemon() method
Explanation:
The setDaemon() method of the Thread class is used to mark a thread as a daemon thread.
18. In Java, what happens when a thread's start() method is called more than once?
a) The thread is executed multiple times
b) The thread's priority is increased
c) An IllegalThreadStateException is thrown
d) The thread's execution is paused
Answer:
c) An IllegalThreadStateException is thrown
Explanation:
Calling the start() method more than once on a thread in Java results in an IllegalThreadStateException.
19. What is the initial state of a thread when it is created?
a) Running
b) Runnable
c) New
d) Waiting
Answer:
c) New
Explanation:
When a thread is created in Java, its initial state is 'New'. It has not yet started running.
20. What is the purpose of the interrupt() method in Java threads?
a) To stop a thread immediately
b) To pause a thread's execution
c) To request a thread to stop
d) To increase a thread's priority
Answer:
c) To request a thread to stop
Explanation:
The interrupt() method is used to send an interrupt signal to a thread, which can be used to request the thread to stop its execution.
21. How can you check if a thread has been interrupted?
a) Using the isInterrupted() method
b) By checking the thread's state
c) Using the hasStopped() method
d) By catching an InterruptedException
Answer:
a) Using the isInterrupted() method
Explanation:
The isInterrupted() method in the Thread class is used to check if a thread has been interrupted.
22. Can you access local variables of a method in a thread's run() method?
a) Yes, always
b) No, never
c) Only if they are final
d) Only if they are static
Answer:
c) Only if they are final
Explanation:
Local variables of a method can be accessed in a thread's run() method only if they are declared final.
23. What is the difference between the wait() and sleep() methods in Java threads?
a) wait() releases the lock, sleep() does not
b) sleep() releases the lock, wait() does not
c) There is no difference
d) wait() can be interrupted, sleep() cannot
Answer:
a) wait() releases the lock, sleep() does not
Explanation:
The wait() method releases the monitor's lock held by the thread, whereas sleep() keeps the monitor's lock until it wakes up or is interrupted.
24. What is the primary use of the notify() and notifyAll() methods in Java threads?
a) To terminate threads
b) To resume threads from waiting state
c) To create new threads
d) To change thread priorities
Answer:
b) To resume threads from waiting state
Explanation:
The notify() and notifyAll() methods are used to signal one or all threads that are waiting on the object's monitor to resume execution.
25. What does it mean for a method to be thread-safe?
a) The method can only be executed by one thread at a time
b) The method can be safely ignored by threads
c) The method can be executed by multiple threads simultaneously without causing issues
d) The method prioritizes certain threads over others
Answer:
c) The method can be executed by multiple threads simultaneously without causing issues
Explanation:
A thread-safe method is one that can be safely invoked by multiple threads at the same time without leading to problems such as data corruption or inconsistent results.
Comments
Post a Comment