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

Threads - Finding the output Questions and Answers

81.

Sample81 is run - what will be the output?

public class Sample81
{
    public static void main(String[] args) throws InterruptedException
    {
        for (int i = 0; i < 3; i++)
        {
            final int n = i;
            Thread t = new Thread(() -> System.out.print(n));
            t.start();
            t.join();
        }
    }
}

Answer: A

Each thread is joined inside the loop before the next is created, so the three run strictly in turn and print in order. Copying the counter into a final local is what lets the lambda capture it; the loop variable itself is not effectively final.

Please log in or register to report this question.

82.

Sample82 is run - what will be the output?

public class Sample82
{
    public static void main(String[] args) throws InterruptedException
    {
        Runnable job = () -> System.out.print(Thread.currentThread().getName() + " ");
        Thread a = new Thread(job, "one");
        Thread b = new Thread(job, "two");
        a.start(); a.join();
        b.start(); b.join();
    }
}

Answer: B

Correct answer: one two

A name passed to the Thread constructor replaces the default Thread-N, and getName called inside run returns the name of the thread executing it - not of main. The join after each start makes the order deterministic: thread “one” runs to completion before “two” is started, so the output is one two every time.

Please log in or register to report this question.

83.

Sample83 is run - what will be the output?

public class Sample83
{
    public static void main(String[] args) throws InterruptedException
    {
        System.out.print("before ");
        Thread.sleep(50);
        System.out.print("after");
    }
}

Answer: C

sleep is static and pauses whichever thread calls it, which here is main, so the program simply waits between its two prints. Nothing interrupts it, so no exception arises; the throws clause is required because sleep declares a checked exception.

Please log in or register to report this question.

84.

Sample84 is run - what will be the output?

public class Sample84 extends Thread
{
    private final String tag;

    Sample84(String tag)
    {
        this.tag = tag;
    }

    public void run()
    {
        System.out.print(tag + " ");
    }

    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Sample84("sub");
        t.start();
        t.join();
        System.out.print("main");
    }
}

Answer: D

Correct answer: sub main

t.start() begins the subclass’s run, which prints sub, and t.join() blocks main until it has finished. Only then does main print. Without the join the order would be genuinely indeterminate, which is what the “either order” option describes - and it is exactly what join exists to prevent.

Please log in or register to report this question.

85.

Sample85 is run - what will be the output?

public class Sample85
{
    static Runnable greeter()
    {
        return new Runnable()
        {
            public void run()
            {
                System.out.print("anon ");
            }
        };
    }

    public static void main(String[] args) throws InterruptedException
    {
        Thread worker = new Thread(greeter());
        worker.start();
        worker.join();
        System.out.print("end");
    }
}

Answer: A

The factory method returns an anonymous class that supplies Runnable's single method, which is exactly what a lambda expresses more briefly. Runnable itself is never instantiated; the anonymous subclass is. The join fixes the order of the two prints.

Please log in or register to report this question.

86.

Sample86 is run - what will be the output?

public class Sample86
{
    static int total;

    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() -> { for (int i = 1; i <= 4; i++) total += i; });
        t.start();
        t.join();
        System.out.println(total);
    }
}

Answer: B

Correct answer: 10

The loop adds 1 through 4, giving 10. The visibility concern the fourth option raises is real for an unsynchronised static field, but join establishes a happens-before relationship: everything the joined thread did is guaranteed visible to the thread that joins it. So main is certain to see 10, and no volatile or synchronisation is needed here.

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