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

Threads - Finding the output Questions and Answers

1.

Sample01 is run - what will be the output?

public class Sample01
{
    public static void main(String[] args)
    {
        Thread t = new Thread(() -> System.out.println(
                Thread.currentThread().getName()));
        t.run();
    }
}

Answer: A

run() is called directly, so it is an ordinary method call and no new thread is created. The body therefore executes on the thread that made the call, which is main, and currentThread() reports that name. Replacing run() with start() would print Thread-0 instead.

Please log in or register to report this question.

2.

Sample02 is run - what will be the output?

public class Sample02
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() -> System.out.println("worker"));
        t.start();
        t.join();
        System.out.println("main");
    }
}

Answer: B

join() blocks the calling thread until the target has died, so the print inside the worker is guaranteed to complete before main reaches its own. This is the one reliable way to order output across threads; without the join either order would be possible.

Please log in or register to report this question.

3.

Sample03 is run - what will be the output?

public class Sample03 extends Thread
{
    public void run()
    {
        System.out.println("cycle");
    }

    public static void main(String[] args) throws InterruptedException
    {
        Sample03 job = new Sample03();
        job.start();
        job.join();
        job.start();
    }
}

Answer: C

The first start() succeeds and the join guarantees its output appears. A thread may never be started twice and cannot be restarted once it has completed, so the second call throws IllegalThreadStateException in main. Running the work again needs a new Thread object.

Please log in or register to report this question.

4.

Sample04 is run - what will be the output?

public class Sample04
{
    public static void main(String[] args)
    {
        Thread t = new Thread(() -> System.out.println("work"));
        t.start();
        t.setDaemon(true);
    }
}

Answer: D

setDaemon must be called before the thread is started and throws IllegalThreadStateException if the thread is alive. The worker has already been started, so it runs and prints while main fails on the next line. The two threads are independent, so both effects occur.

Please log in or register to report this question.

5.

Sample05 is run - what will be the output?

public class Sample05
{
    public static void main(String[] args)
    {
        Object lock = new Object();
        synchronized (lock)
        {
            System.out.println("in");
        }
        lock.notify();
    }
}

Answer: A

The synchronized block prints and then releases the monitor, so by the time notify() is reached the thread no longer owns it. notify requires ownership just as wait does, so it throws IllegalMonitorStateException. Moving the call inside the block would make it legal.

Please log in or register to report this question.

6.

Sample06 is run - what will be the output?

public class Sample06
{
    static final Runnable NOTHING = new Runnable()
    {
        public void run() { }
    };

    public static void main(String[] args) throws InterruptedException
    {
        Thread probe = new Thread(NOTHING);
        System.out.print(probe.getState());
        probe.start();
        probe.join();
        System.out.print(" " + probe.getState());
    }
}

Answer: B

A constructed but unstarted thread is NEW, and after join() has returned the thread has certainly died, so it reports TERMINATED. Those are the two points in a thread's life where the state is predictable. There is no DEAD constant; that name belongs to older documentation.

Please log in or register to report this question.

7.

Sample07 is run - what will be the output?

public class Sample07 extends Thread
{
    public void run() { }

    public static void main(String[] args) throws InterruptedException
    {
        Sample07 job = new Sample07();
        boolean before = job.isAlive();
        job.start();
        job.join();
        System.out.println(before + " " + job.isAlive());
    }
}

Answer: C

A thread is alive only between being started and dying. Before start() it is NEW and not alive, and once join() has returned it has finished, so it is not alive either. The value would be true only if sampled while the thread was running, which is not a moment this program can name.

Please log in or register to report this question.

8.

Sample08 is run - what will be the output?

public class Sample08
{
    public static void main(String[] args)
    {
        Thread.currentThread().interrupt();
        System.out.print(Thread.interrupted() + " ");
        System.out.print(Thread.interrupted());
    }
}

Answer: D

Thread.interrupted() is the static form that tests and clears the status of the current thread. The first call finds the flag set and clears it, so the second finds nothing. That clearing is the whole difference from isInterrupted().

Please log in or register to report this question.

9.

Sample09 is run - what will be the output?

public class Sample09
{
    static void report()
    {
        System.out.print(Thread.currentThread().isInterrupted() + " ");
    }

    public static void main(String[] args)
    {
        Thread.currentThread().interrupt();
        report();
        report();
    }
}

Answer: A

isInterrupted() reports the status without changing it, so both calls answer true and the flag is still set afterwards. Swapping it for the static Thread.interrupted() would give true then false, which is the trap this pair of questions exists to separate.

Please log in or register to report this question.

10.

Sample10 is run - what will be the output?

public class Sample10
{
    public static void main(String[] args)
    {
        Thread.currentThread().interrupt();
        try
        {
            Thread.sleep(1000);
            System.out.println("slept");
        }
        catch (InterruptedException e)
        {
            System.out.println("caught " + Thread.currentThread().isInterrupted());
        }
    }
}

Answer: B

The interrupt status is already set, so sleep throws InterruptedException immediately rather than waiting. Delivering that exception clears the status, so by the time the catch block runs isInterrupted reports false. Code that needs to pass the interruption on must call interrupt() again.

Please log in or register to report this question.

Loading…

Topics

    No topic!
Tags: Threads - Finding the output Questions and Answers || Threads - Finding the output MCQ Questions and Answers || Threads - Finding the output GK Questions and Answers || Threads - Finding the output GK MCQ Questions || Threads - Finding the output Multiple Choice Questions and Answers || Threads - Finding the output GK || GK on Threads - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming