Getting & Setting Name of Thread MCQ

Java Multithreading MCQ Part-3 | Getting & Setting Name of Thread MCQ | Also see:- Define Thread in Java Multithreading Interview Questions

Q1) How can we get an instance of the current thread?

a) currentThread()
b) Thread.currentThread()
c) getInstance()
d) Thread.getInstance()

View Answer Answer:- b) Thread.currentThread()

In the Thread class, currentThread() method is a static method which returns an instance of current Thread.

Q2) Which method is used to get the name of a thread?

a) getName()
b) getThreadName()
c) name()
d) None of these

View Answer Answer:- a) getName()

Q3) Which method is used to set the name of a thread?

a) setName()
b) setThreadName()
c) name()
d) None of these

View Answer Answer:- a) setName()

Q4) Find the output of the below program?

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

a) Main
b) main
c) Main thread
d) main thread

View Answer Answer:- b) main

The main() method is executed by the “main” thread.

Q5) Find the output of the below program?

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

a) main
b) KnowProgram
c) Compile time error
d) Exception

View Answer Answer:- b) KnowProgram

Q6) Find the output of the below program?

class MyThread extends Thread { }
public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.start();
      System.out.println(mt.getName());
   }
}

a) thread-1
b) thread-0
c) thread-N
d) main

View Answer Answer:- b) thread-0

By default JVM gives the name of the thread as thread-0. The next thread name will be thread-1, thread-2, and so on.

Q7) Find the output of the below program?

class MyThread extends Thread { }
public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.start();
      System.out.println(Thread.currentThread().getName());
   }
}

a) thread-1
b) thread-0
c) thread-N
d) main

View Answer Answer:- d) main

The getName() method is called on the current thread object not on the “mt” object.

Q8) What will be the output of the below program?

class MyThread extends Thread {
   public void run() {
      System.out.print(Thread.currentThread().getName()+" ");
      System.out.print(this.getName());  
   }
}

public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.start();
   }
}

a) thread-0 main
b) thread-0 thread-0
c) Compile time error
d) Run-time Exception

View Answer Answer:- b) thread-0 thread-0

Both Thread.currentThread().getName() & this.getName() give the same result.

Q9) What is the possible output of the below program?

class MyThread extends Thread {
   public void run() {
      System.out.println(Thread.currentThread().getName());
   }
}

public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.start();
      int count = 0;
      for (int i=0; i<8000; i++) {
         count++;         
      }
      mt.setName("Child-thread");
   }
}

a) thread-0
b) Child-thread
c) Sometimes (a) or Sometimes (b)
d) Error or Exception

View Answer Answer:- c) Sometimes (a) or Sometimes (b)

Q10) Find the output of the below program?

class MyThread extends Thread {
   public void run() {
      try {
         sleep(2000);
      } catch(Exception e) { }
      System.out.println(Thread.currentThread().getName());
   }
}

public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.start();
      mt.setName("Child-thread");
   }
}

a) thread-0
b) Child-thread
c) Sometimes (a) or Sometimes (b)
d) Error or Exception

View Answer Answer:- b) Child-thread

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *