Java MCQ Questions - Threads

This section focuses on the "Threads" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.

1. What is the name of the method used to start a thread execution?

A. run();
B. init();
C. start();
D. resume();

View Answer


2. Which cannot directly cause a thread to stop executing?

A. Calling the SetPriority() method on a Thread object.
B. Calling read() method on an InputStream object.
C. Calling notify() method on an object.
D. Calling the wait() method on an object.

View Answer


3. Which of the following will directly stop the execution of a Thread?

A. notify()
B. notifyall()
C. wait()
D. exits synchronized code

View Answer


4. Which function of pre defined class Thread is used to check weather current thread being checked is still running?

A. isAlive()
B. Alive()
C. isRunning()
D. Join()

View Answer


5. Which method must be defined by a class implementing the java.lang.Runnable interface?

A. public void run()
B. void run()
C. void run(int priority)
D. public void start()

View Answer


6. Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?

A. After thread A is notified, or after two seconds.
B. Two seconds after thread A is notified.
C. After the lock on B is released, or after two seconds.
D. Two seconds after lock B is released.

View Answer


7. Which will contain the body of the thread?

A. main();
B. stop();
C. start();
D. run();

View Answer


8. Which class or interface defines the wait(), notify(),and notifyAll() methods?

A. Object
B. Class
C. Runnable
D. Thread

View Answer


9. Which of these method of Thread class is used to find out the priority given to a thread?

A. ThreadPriority()
B. get()
C. getPriority()
D. getThreadPriority()

View Answer


10. Which of these method of Thread class is used to Suspend a thread for a period of time?

A. stop()
B. sleep()
C. terminate()
D. suspend()

View Answer


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() {}
}

A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
C. X run = new X(); Thread t = new Thread(run); t.start();
D. Thread t = new Thread(); x.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);
	}
}

A. Thread[5,main].
B. Thread[New Thread,5].
C. Thread[main,5,main].
D. Thread[New Thread,5,main]

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();
	}
}

A. 0
B. 1
C. 2
D. 3

View Answer


14. which of these will create and start this thread?

  
public class MyRunnable implements Runnable
{
	public void run()
	{
		// some code here
	}
}

A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
C. new Thread(new MyRunnable()).start();
D. new MyRunnable().start();

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());
	}
}

A. main
B. Thread
C. New Thread
D. Thread[New Thread,5,main].

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 ");
	}
}

A. Compilation fails
B. An exception occurs at runtime.
C. It prints "Thread one. Thread two."
D. The output cannot be determined.

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());
	}
}

A. 1
B. 4
C. 0
D. 5

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());
	}
}

A. 1
B. 0
C. TRUE
D. FALSE

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);

A. public class MyRunnable extends Object{public void run(){}}
B. public class MyRunnable implements Runnable{void run(){}}
C. public class MyRunnable implements Runnable{public void run(){}}
D. public class MyRunnable extends Runnable{public void run(){}}

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);
		}
	}
}

A. Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
B. Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
C. Each String in the array lines will output, with a 1-second pause.
D. This code will not compile.

View Answer


21. What is multithreaded programming?

A. It is a process in which two different processes run simultaneously
B. It’s a process in which a single process can access information from many sources
C. It is a process in which two or more parts of same process run simultaneously
D. It is a process in which many different process are able to access same information

View Answer


22. Which of these are types of multitasking?

A. Process based
B. Thread based
C. Process and Thread based
D. None of the mentioned

View Answer


23. Thread priority in Java is?

A. Integer
B. Float
C. double
D. long

View Answer


24. What will happen if two thread of the same priority are called to be processed simultaneously?

A. Anyone will be executed first lexographically
B. Both of them will be executed simultaneously
C. None of them will be executed
D. It is dependent on the operating system

View Answer


25. Which of these statements is incorrect?

A. By multithreading CPU idle time is minimized, and we can take maximum use of it
B. By multitasking CPU idle time is minimized, and we can take maximum use of it
C. Two thread in Java can have the same priority
D. A thread can exist only in two states, running and blocked

View Answer


26. What requires less resources?

A. Thread
B. Process
C. Thread and Process
D. Neither Thread nor Process

View Answer


27. What does not prevent JVM from terminating?

A. Process
B. Daemon Thread
C. User Thread
D. JVM Thread

View Answer


28. What decides thread priority?

A. Process
B. Process scheduler
C. Thread
D. Thread scheduler

View Answer


29. What is true about time slicing?

A. Time slicing is OS service that allocates CPU time to available runnable thread
B. Time slicing is the process to divide the available CPU time to available runnable thread
C. Time slicing depends on its implementation in OS
D. Time slicing allocates more resources to thread

View Answer


30. Deadlock is a situation when thread is waiting for other thread to release acquired object.

A. TRUE
B. FALSE
C. Can be true or false
D. can not say

View Answer





Also check :

Discussion



* You must be logged in to add comment.