Java Thread Methods and Daemon Threads

Last Updated :
Discuss
Comments

Question 1

Which method is used to set the name of a thread in Java?

  • A

    setName()

  • B

    setThreadName()

  • C

    setThread()

  • D

    assignName()

Question 2

What will be the output of the following code?

Java
class MyThread extends Thread {
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
    
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        t1.setName("CustomThread");
        t1.start();
    }
}


  • A

    Thread-0

  • B

    MyThread

  • C

    CustomThread

  • D

    Compilation Error

Question 3

What is the key difference between start() and run() in Java threads?

  • A

    start() creates a new thread, but run() executes in the current thread

  • B

    Both methods create a new thread

  • C

    run() is an internal method that should not be called

  • D

    start() directly calls run() without creating a new thread

Question 4

What will happen if start() is called twice on the same thread?

  • A

    The thread runs twice

  • B

    A new thread is created each time

  • C

    It throws an IllegalThreadStateException

  • D

    It runs normally without any issues

Question 5

What is the effect of Thread.sleep(5000) inside a thread?

  • A

    The thread is terminated

  • B

    The thread is paused for 5 seconds but remains in the RUNNABLE state

  • C

    The thread is paused for 5 seconds and enters the TIMED_WAITING state

  • D

    The thread releases its lock and resumes immediately

Question 6

What will be the output of this program?

Java
class TestThread extends Thread {
    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println("Thread Running");
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
    }

    public static void main(String[] args) {
        TestThread t = new TestThread();
        t.start();
        t.interrupt();
    }
}


  • A

    Thread Running

  • B

    Interrupted

  • C

    No Output

  • D

    Both A and B, depending on timing

Question 7

Which of the following is true about a daemon thread in Java?

  • A

    A daemon thread runs in the background and stops when all user threads finish execution

  • B

    A daemon thread continues running even if all user threads finish

  • C

    A daemon thread must be manually stopped

  • D

    The JVM waits for daemon threads to finish before terminating

Question 8

What happens when setDaemon(true) is called after start()?

  • A

    The thread becomes a daemon thread

  • B

    Compilation error

  • C

    Runtime exception (IllegalThreadStateException)

  • D

    No effect

Question 9

How can you check if a thread is a daemon thread?

  • A

    Using isDaemon() method

  • B

    Using isDaemonThread() method

  • C

    Using isBackgroundThread() method

  • D

    Using getThreadType() method

Question 10

Which of the following correctly creates and starts a daemon thread?

  • A
    Java
    Thread t = new Thread(() -> System.out.println("Daemon Running"));
    t.start();
    t.setDaemon(true);
    


  • B
    Java
    Thread t = new Thread(() -> System.out.println("Daemon Running"));
    t.setDaemon(true);
    t.start();
    


  • C
    Java
    Thread t = new Thread(() -> System.out.println("Daemon Running"));
    t.setDaemon(false);
    t.start();
    


  • D

    None of the above

There are 10 questions to complete.

Take a part in the ongoing discussion