Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
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) mainThe 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) KnowProgramQ6) 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-0By 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) mainThe 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-0Both 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-threadIf 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!