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

Threads - Finding the output Questions and Answers

71.

Sample71 is run - what will be the output?

import java.util.*;
import java.util.concurrent.*;

public class Sample71
{
    public static void main(String[] args)
    {
        List<String> names = new CopyOnWriteArrayList<>(Arrays.asList("a", "b"));
        for (String s : names)
        {
            names.add("c");
            System.out.print(s);
        }
        System.out.print(" " + names.size());
    }
}

Answer: C

The iterator works from a snapshot taken when iteration began, so it visits only a and b however many elements are added, and no ConcurrentModificationException is possible. The two additions do take effect, so the final size is four.

Please log in or register to report this question.

72.

Sample72 is run - what will be the output?

import java.util.*;

public class Sample72
{
    public static void main(String[] args)
    {
        List<String> names = new ArrayList<>(Arrays.asList("a", "b"));
        for (String s : names)
        {
            names.add("c");
            System.out.print(s);
        }
    }
}

Answer: D

An ArrayList's iterator is fail-fast: it notices the structural change on its next step, so the first element prints and the exception follows. Adding through the collection during a for-each is exactly the case the check exists to catch; the iterator's own methods are the supported route.

Please log in or register to report this question.

73.

Sample73 is run - what will be the output?

public class Sample73
{
    static final InheritableThreadLocal<String> TAG = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException
    {
        TAG.set("parent");
        Thread child = new Thread(() -> System.out.println(TAG.get()));
        child.start();
        child.join();
    }
}

Answer: A

An InheritableThreadLocal passes the creating thread's value to each thread it creates, so the child sees parent. A plain ThreadLocal would give null here, because each thread would have its own unset value. Inheritance is a copy taken at construction, not a live link.

Please log in or register to report this question.

74.

Sample74 is run - what will be the output?

public class Sample74 extends Thread
{
    static final ThreadLocal<String> SCOPE = new ThreadLocal<>();

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

    public static void main(String[] args) throws InterruptedException
    {
        SCOPE.set("outer");
        Sample74 worker = new Sample74();
        worker.start();
        worker.join();
    }
}

Answer: B

A ThreadLocal holds an independent value per thread and does not pass it on, so the worker has never set one and get returns null, which println renders as the four characters null. InheritableThreadLocal is the variant that copies the creating thread's value.

Please log in or register to report this question.

75.

Sample75 is run - what will be the output?

public class Sample75
{
    public static void main(String[] args)
    {
        Thread t = new Thread();
        t.start();
        System.out.println("done");
    }
}

Answer: C

A Thread built with no Runnable inherits Thread's own run(), which the javadoc says does nothing and returns. So the thread starts, finds no work and dies quietly, leaving only main's output. Nothing is thrown, because a missing Runnable is a legitimate case.

Please log in or register to report this question.

76.

Sample76 is run - what will be the output?

public class Sample76 extends Thread
{
    public void run()
    {
        System.out.print("body ");
    }

    public static void main(String[] args)
    {
        Sample76 t = new Sample76();
        t.run();
        t.run();
        System.out.print("end");
    }
}

Answer: D

run() is being called as an ordinary method, so it may be called as often as you like and each call executes on main. No thread is ever started, which is why the restriction on calling start() twice does not apply here.

Please log in or register to report this question.

77.

Sample77 is run - what will be the output?

public class Sample77
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread first = new Thread(() -> System.out.print("1"));
        Thread second = new Thread(() -> System.out.print("2"));
        first.start();
        first.join();
        second.start();
        second.join();
        System.out.print("3");
    }
}

Answer: A

Each thread is joined before the next is started, so only one is ever running and the order is fully determined. Starting both before joining either would leave the first two characters in any order. Sequencing threads this way gives up the concurrency, which is sometimes exactly what a test wants.

Please log in or register to report this question.

78.

Sample78 is run - what will be the output?

public class Sample78
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(new Runnable()
        {
            public void run()
            {
                System.out.print("x");
            }
        });
        t.setName("printer");
        t.start();
        t.join();
        t.setName("printer");
        System.out.print(" " + t.getName());
    }
}

Answer: B

Correct answer: x printer

The thread runs and prints x, and join guarantees it has finished before main continues. setName may be called at any point in a thread’s life, including after it has terminated - it is setDaemon and setPriority before start that carry restrictions, and only start on an already-started thread throws IllegalThreadStateException. So the name is “printer” and the output is x printer.

Please log in or register to report this question.

79.

Sample79 is run - what will be the output?

public class Sample79
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() -> System.out.print(
                Thread.currentThread().getPriority()));
        t.start();
        t.join();
    }
}

Answer: C

A new thread takes its initial priority from the thread that created it, and the main thread runs at Thread.NORM_PRIORITY, which is 5. The constants are 1 for MIN_PRIORITY and 10 for MAX_PRIORITY, and the value is fixed by the class rather than the platform.

Please log in or register to report this question.

80.

Sample80 is run - what will be the output?

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

Answer: D

Only one thread touches the builder at a time here, so there is no race, and the join gives main a happens-before edge that makes the worker's write visible. The variable is effectively final, which is all a lambda requires.

Please log in or register to report this question.

Loading…

Other Subjects

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