Java Programming MCQs - Threads
11. Which of the following line of code is suitable to start a thread ?
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}
View Answer
12. What will be the output of the program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t);
}
}
View Answer
13. Number of threads in below java program is:
public class ThreadExtended extends Thread {
public void run() {
System.out.println("Thread is running no");
}
public static void main(String[] args)
{
ThreadExtended threadE = new ThreadExtended();
threadE.start();
}
}
View Answer
14. which of these will create and start this thread?
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
View Answer
15. What is the priority of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
}
View Answer
16. What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run()
{
System.out.print("Thread ");
}
}
View Answer
17. What is the name of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
}
}
View Answer
18. What is the name of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}
View Answer
19. The following block of code creates a Thread using a Runnable target:Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
View Answer
20. The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
class Test
{
public static void main(String [] args)
{
printAll(args);
}
public static void printAll(String[] lines)
{
for(int i = 0; i < lines.length; i++)
{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
View Answer
Also check :