QUIZ: Topic 5 - Threads in Java Total Questions: 10 Instructions: Choose the most correct option (A, B, C or D) for each question. Check your answers using the Answer Key at the end. 1. Which of the following are the two ways to create a thread in Java? A) By using the new operator only, and by using the clone() method B) By extending java.lang.Thread class and by implementing java.lang.Runnable interface C) By using the Runnable constructor and by using the finalize() method D) By extending the Object class and by implementing the Callable interface 2. What is the main difference between calling start() and calling run() directly on a thread object? A) start() invokes run() on the thread object, beginning execution on a separate path of execution B) start() is used only for daemon threads, while run() is used only for user threads C) start() runs the code in the same thread, while run() starts a new thread D) start() can be called only once, while run() can never be called 3. Implementing the Runnable interface is the preferred way to create threads when: A) the class must subclass some other class, since Java does not allow multiple inheritance B) the thread must always be a daemon thread C) the class does not contain a run() method D) the thread needs a higher priority than a thread created with the Thread class 4. A thread that is waiting for resources is said to be in which state? A) New state B) Suspended state C) Blocked state D) Terminated state 5. Which statement about daemon threads and user threads is correct? A) A Java program will not finish execution until one of the user threads is live B) The JVM waits for all daemon threads to finish before the program closes C) Daemon threads are high priority threads that perform the main tasks of the program D) A user thread cannot be given a lower priority than a daemon thread 6. Which method causes the currently executing thread to suspend execution for a specified number of milliseconds? A) yield() B) sleep(long millis) C) join() D) interrupt() 7. What does yield() do to the currently executing thread? A) It causes the thread to temporarily pause and allow other threads to execute B) It permanently stops the thread's execution C) It makes the thread wait until notify() is called D) It blocks the thread until a required resource is available 8. If two threads of the same priority are waiting for the CPU, the thread scheduler normally chooses between them using which method? A) First-in-first-out with a stack B) A round-robin method, so each thread is guaranteed a turn at the CPU C) Strict priority order, so the first thread always runs first D) Random selection without any guarantee of a turn 9. Why must a method that affects the state of a shared object be declared synchronized? A) So that only one thread can execute it at a time, preventing simultaneous conflicting updates B) So that the thread runs faster by using multiple CPUs at once C) So that the method can be called without creating an object D) So that the method automatically becomes a daemon thread 10. Which statement about the wait(), notify() and notifyAll() methods is correct? A) They are static methods of the Thread class and can be called from any method B) notify() wakes only one waiting thread, while notifyAll() alerts every thread waiting for the condition C) wait() causes the thread to sleep for a fixed number of milliseconds D) They may be called only from a class that extends Thread and never from a synchronized block ANSWER KEY 1. B) The slides state there are two ways to create a thread: extending java.lang.Thread and implementing java.lang.Runnable. 2. A) start() starts the thread on a separate path of execution and then invokes run() on the thread object. 3. A) Implementing Runnable is preferred when the class has to subclass some other class, since only one superclass is allowed. 4. C) The Blocked state applies when a thread is waiting for resources; the scheduler may then clear the queue of unwanted threads. 5. A) User (non-daemon) threads do specific tasks, and the program does not finish while any user thread is still live, even if a daemon thread runs. 6. B) sleep(long millis) causes the currently executing thread to sleep for the specified number of milliseconds. 7. A) yield() makes the current thread temporarily pause so that other threads get a chance to execute. 8. B) With equal priorities the scheduler uses a round-robin approach, guaranteeing each thread a turn within each time period. 9. A) Synchronization allows only a single thread inside the body of the method at a time; a second call blocks until the first completes or wait() is called. 10. B) notify() informs one waiting thread, while notifyAll() alerts all threads waiting for the condition; both are Object methods used from synchronized code.