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

Threads - Finding the output Questions and Answers

11.

Sample11 is run - what will be the output?

public class Sample11
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() ->
        {
            try
            {
                Thread.sleep(5000);
                System.out.println("finished");
            }
            catch (InterruptedException e)
            {
                System.out.println("interrupted");
            }
        });
        t.start();
        t.interrupt();
        t.join();
    }
}

Answer: C

The worker spends essentially all its life inside sleep, and an interrupt delivered to a thread blocked there throws InterruptedException at once. So the catch block runs almost immediately and the program ends without waiting. The tiny race before sleep begins is closed by sleep noticing an already-set status.

Please log in or register to report this question.

12.

Sample12 is run - what will be the output?

public class Sample12
{
    public static void main(String[] args)
    {
        System.out.println(Thread.currentThread().getName());
    }
}

Answer: D

The thread the JVM creates to run main is named main, and currentThread() returns whichever thread is executing the call. Thread-0 is the default name given to the first Thread object a program constructs, which this program never does.

Please log in or register to report this question.

13.

Sample13 is run - what will be the output?

public class Sample13
{
    public static void main(String[] args)
    {
        Thread a = new Thread();
        Thread b = new Thread();
        System.out.println(a.getName() + " " + b.getName());
    }
}

Answer: A

A thread constructed without a name is given one by appending a counter that starts at zero, so the first two are Thread-0 and Thread-1. Neither is started here, which does not matter: the name is assigned by the constructor.

Please log in or register to report this question.

14.

Sample14 is run - what will be the output?

public class Sample14 extends Thread
{
    Sample14()
    {
        super("loader");
    }

    public void run()
    {
        System.out.println(getName());
    }

    public static void main(String[] args) throws InterruptedException
    {
        new Sample14().start();
    }
}

Answer: B

Passing a name to Thread's constructor through super replaces the generated one, and getName inside run reports the thread that is executing, which is the worker itself. No counter is appended to a name that was supplied, so it is not loader-0.

Please log in or register to report this question.

15.

Sample15 is run - what will be the output?

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

Answer: C

An exception that escapes run() kills only that thread; the default handler reports it and join() then returns normally. main is unaffected and prints. The exception cannot be caught around start() or join(), because it happened on another thread.

Please log in or register to report this question.

16.

Sample16 is run - what will be the output?

public class Sample16
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() -> { throw new RuntimeException("x"); });
        try
        {
            t.start();
            t.join();
        }
        catch (RuntimeException e)
        {
            System.out.println("caught");
        }
        System.out.println("end");
    }
}

Answer: D

The try block surrounds start() and join(), neither of which throws the worker's exception: it is delivered on the worker's own stack. So the catch never runs, the default handler prints the trace, and main continues to end. Catching a worker's failure needs an UncaughtExceptionHandler, or a Callable and a Future.

Please log in or register to report this question.

17.

Sample17 is run - what will be the output?

public class Sample17
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() -> System.out.println(10 / 0));
        t.setUncaughtExceptionHandler(
                (thread, ex) -> System.out.println("handled " + ex.getClass().getSimpleName()));
        t.start();
        t.join();
    }
}

Answer: A

The division throws before println can be reached, and the thread's own handler takes precedence over the group's and the default one. getSimpleName reports the runtime class, ArithmeticException, rather than the supertype the parameter is declared as.

Please log in or register to report this question.

18.

Sample18 is run - what will be the output?

public class Sample18
{
    public static void main(String[] args)
    {
        new Thread(() -> System.out.println("A")).start();
        new Thread(() -> System.out.println("B")).start();
    }
}

Answer: B

Starting a thread only makes it eligible to run; the scheduler decides when, so neither ordering is promised and the answer is genuinely both. main returning does not stop them, because both are non-daemon threads and the JVM waits for them. Ordering requires join, a lock or a queue.

Please log in or register to report this question.

19.

Sample19 is run - what will be the output?

public class Sample19
{
    static int count = 0;

    public static void main(String[] args) throws InterruptedException
    {
        Runnable r = () -> { for (int i = 0; i < 10000; i++) count++; };
        Thread a = new Thread(r), b = new Thread(r);
        a.start(); b.start();
        a.join(); b.join();
        System.out.println(count);
    }
}

Answer: C

The joins guarantee both threads have finished, so the print is not the problem; the increments are. count++ reads, adds and writes, and two threads can interleave those steps and lose updates, so the total can be anything from 10000 up to 20000. Only synchronization or an AtomicInteger makes 20000 certain.

Please log in or register to report this question.

20.

Sample20 is run - what will be the output?

public class Sample20
{
    static volatile long total = 0;

    static class Adder extends Thread
    {
        public void run()
        {
            for (int i = 0; i < 50000; i++) total++;
        }
    }

    public static void main(String[] args) throws InterruptedException
    {
        Adder x = new Adder(), y = new Adder();
        x.start(); y.start();
        x.join(); y.join();
        System.out.println(total);
    }
}

Answer: D

volatile makes each read and each write atomic and visible, which is not enough: the increment is three operations and another thread can act between them, so updates are still lost. Declaring the field long does not help either; volatile only guarantees that a wide value is never seen half-written. This is the single most common misuse of the keyword.

Please log in or register to report this question.

Loading…
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