Machine-translated by Google. Questions are authored in English.
Top

Threads - Pointing out the correct statements Questions and Answers

51.

Starting a thread, joining it, then starting it again - which of the following is true?

Thread t = new Thread(() -> System.out.println("working"));
t.start();
t.join();
t.start();

Answer: C

A thread may not be started twice, and cannot be restarted even after run() has finished. The first call succeeds and prints; the second throws IllegalThreadStateException in the main thread. Note the failure is at runtime, not compile time: nothing in the type system tracks whether start() has already happened.

Please log in or register to report this question.

52.

Which of these statements about invoking run() on a thread that is never started is correct?

Thread t = new Thread(() -> System.out.println(
        Thread.currentThread().getName()));
t.run();

Answer: D

Invoking run() directly is an ordinary method call, so nothing is scheduled and the body executes on whichever thread made the call, here main. The Thread object is created but never started, so it stays in the NEW state for the whole program. Replacing run() with start() is what makes it concurrent.

Please log in or register to report this question.

53.

Which one of the following statements about calling wait() outside any synchronized region is true?

Object lock = new Object();
lock.wait();

Answer: A

wait, notify and notifyAll may only be called by a thread that owns the object's monitor, and this call sits outside any synchronized region. The result is IllegalMonitorStateException at runtime rather than a hang. Wrapping the call in synchronized (lock) is what makes it legal.

Please log in or register to report this question.

54.

Consider a notify that arrives before the matching wait. Which statement is correct?

// thread A                     // thread B
synchronized (lock) {           synchronized (lock) {
    lock.notify();                  lock.wait();
}                               }

Answer: B

A monitor keeps no record of notifications. If notify() runs before the other thread reaches wait(), the signal is simply discarded and the later wait blocks with nothing left to wake it. This is the lost-wakeup problem, and it is why the condition is tested inside a synchronized region before waiting at all.

Please log in or register to report this question.

55.

A synchronized method that sleeps while holding its lock - which of the following is true?

class Store
{
    synchronized void hold() throws InterruptedException
    {
        Thread.sleep(1000);
    }

    synchronized void other() { }
}

Answer: C

sleep does not lose ownership of any monitor, so the lock on the instance is held for the whole second and both synchronized methods stay closed to other threads. Using wait(1000) instead would release the monitor and let the other method proceed, which is the practical difference between the two.

Please log in or register to report this question.

Answer: D

Thread defines these three as 1, 5 and 10, and they are fixed constants rather than platform values. A value outside 1 to 10 causes IllegalArgumentException. What is platform-dependent is the effect of a priority, not the number itself.

Please log in or register to report this question.

Answer: A

The constructors that take no name generate one by appending a counter, giving Thread-0 for the first. The name is only a label used in traces and dumps, so it need not be unique, but naming threads meaningfully makes a thread dump far easier to read.

Please log in or register to report this question.

Answer: B

wait returns void, so on return the thread knows only that it is running again, whether from a notify, a timeout, an interrupt or a spurious wakeup. That is precisely why the condition must be re-tested in a loop; the return itself carries no information.

Please log in or register to report this question.

Answer: C

The atomic classes exist because volatile alone cannot make a read-modify-write safe. incrementAndGet performs the whole update as one indivisible step, typically with a compare-and-swap instruction rather than a lock, so no update is lost and no monitor is acquired.

Please log in or register to report this question.

Answer: D

Without volatile, another thread can observe a non-null reference before the constructor's writes are visible, and use a partly built object. Declaring the field volatile supplies the happens-before edge that makes the idiom sound. The second check inside the lock does nothing about visibility.

Please log in or register to report this question.

Loading…

Topics

    No topic!
Tags: Threads - Pointing out the correct statements Questions and Answers || Threads - Pointing out the correct statements MCQ Questions and Answers || Threads - Pointing out the correct statements GK Questions and Answers || Threads - Pointing out the correct statements GK MCQ Questions || Threads - Pointing out the correct statements Multiple Choice Questions and Answers || Threads - Pointing out the correct statements GK || GK on Threads - Pointing out the correct statements || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming