CommerceThreads MCQs
Practice Threads MCQs for competitive exams.
Threads MCQs
Practice questions from this topic.
In Java, can you directly access the run() method of a Thread object?
- A. Only if the thread is marked as "final"
- B. Yes, but it will execute in the current thread, not as a separate thread
- C. Only if the thread is marked as "static"
- D. None of These
Correct Answer: B
What is the purpose of the setPriority() method in Java threads?
- A. It starts a new thread
- B. It resumes a paused thread
- C. It sets the priority of a thread
- D. It synchronizes threads
Correct Answer: C
In Java, which class is used to create and manage thread groups?
- A. GroupManager
- B. ThreadController
- C. ThreadManager
- D. ThreadGroup
Correct Answer: D
How can you handle exceptions in Java threads?
- A. By using try-catch blocks within the run() method
- B. By using the stop() method
- C. By ignoring exceptions in threads
- D. By using the yield() method
Correct Answer: A
In Java, what is the purpose of the isInterrupted() method in threads?
- A. It checks if a thread is a daemon thread
- B. It checks if a thread is waiting
- C. It checks if a thread is running
- D. It checks if a thread has been interrupted
Correct Answer: D
What is the result of calling the setDaemon(true) method on a running thread in Java?
- A. It resumes the thread
- B. It pauses the thread
- C. It sets the thread as a daemon thread
- D. It stops the thread immediately
Correct Answer: C
In Java, what happens when a thread encounters an uncaught exception and terminates abruptly?
- A. The thread is paused indefinitely
- B. The JVM prints the exception trace and terminates the thread
- C. The thread resumes execution without any impact
- D. None of These
Correct Answer: B
What is the purpose of the isDaemon() method in Java threads?
- A. It checks if a thread is currently running
- B. It checks if a thread is sleeping
- C. It checks if a thread is a daemon thread
- D. It checks if a thread is waiting
Correct Answer: C
How can you prevent multiple threads from accessing a critical section of code simultaneously in Java?
- A. By using the wait() method
- B. By using the start() method multiple times
- C. By creating more threads to access the critical section
- D. By using synchronized blocks or methods
Correct Answer: D
In Java, what is a daemon thread?
- A. A thread that runs in the background and does not prevent the program from exiting
- B. A thread that is always in a waiting state
- C. A thread that cannot be interrupted
- D. A thread that runs on a separate CPU
Correct Answer: A
Which keyword is used to declare a synchronized method in Java?
- A. sync
- B. lock
- C. thread-safe
- D. synchronized
Correct Answer: D
In Java, what is the purpose of the interrupt() method in threads?
- A. It resumes the thread
- B. It pauses the thread
- C. It interrupts the normal execution of a thread, causing it to throw an InterruptedException
- D. It stops the thread permanently
Correct Answer: C