0

Java Thread Quiz

Description: Java Thread Quiz
Number of Questions: 8
Created by:
Tags: java
Attempted 0/8 Correct 0 Score 0

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

  1. init();

  2. start();

  3. run();

  4. resume();


Correct Option: B
Explanation:

The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Which cannot directly cause a thread to stop executing?

  1. Calling the SetPriority() method on a Thread object.

  2. Calling the wait() method on an object.

  3. Calling notify() method on an object.

  4. Calling read() method on an InputStream object.


Correct Option: C
Explanation:

notify() - wakes up a single thread that is waiting on this object's monitor.

  1. wait()

  2. notify()

  3. notifyall()

  4. exits synchronized code


Correct Option: A

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

  1. void run()

  2. public void run()

  3. public void start()

  4. void run(int priority)


Correct Option: B

AI Explanation

To answer this question, you need to understand the java.lang.Runnable interface.

The java.lang.Runnable interface is used to define a task that can be executed by a thread. It contains only one method that must be implemented by any class that implements this interface.

The method that must be defined by a class implementing the java.lang.Runnable interface is:

Option B) public void run()

This method represents the task that will be executed when a thread is started. It does not take any arguments and does not return any value. The implementation of this method should contain the code that defines the task to be performed by the thread.

Option A) void run() - This option is incorrect because the run() method must be defined as public when implementing the java.lang.Runnable interface.

Option C) public void start() - This option is incorrect because the start() method is not part of the java.lang.Runnable interface. The start() method is a method of the Thread class, which is used to start a new thread.

Option D) void run(int priority) - This option is incorrect because the run() method in the java.lang.Runnable interface does not take any arguments.

Which will contain the body of the thread?

  1. run();

  2. start();

  3. stop();

  4. main();


Correct Option: A

AI Explanation

To answer this question, we need to understand the concepts related to threads.

In computer programming, a thread is a sequence of instructions that can be executed independently of the main program. Each thread has its own stack and execution context, allowing multiple threads to run concurrently.

In the given options, the correct answer is A) run().

The run() method is the entry point for a thread when it is executed. It contains the body of the thread and defines the code that will be executed by the thread. The run() method should be overridden in a class that extends the Thread class or implements the Runnable interface.

Option B) start() is used to start a thread by invoking its run() method. It does not contain the body of the thread.

Option C) stop() is used to stop a thread, but it is considered unsafe and deprecated in most programming languages.

Option D) main() is the entry point for the main program, not for a thread. It is executed by the main thread.

Therefore, the correct answer is A) run(). This option is correct because it contains the body of the thread.

Please select the most appropriate option.

Which method registers a thread in a thread scheduler?

  1. run();

  2. construct();

  3. start();

  4. register();


Correct Option: C

AI Explanation

To answer this question, you need to understand how threads are registered in a thread scheduler.

Option A) run() - This option is incorrect because the run() method is responsible for the code that will be executed when the thread is started, but it does not register the thread in the thread scheduler.

Option B) construct() - This option is incorrect because there is no specific method named construct() for registering a thread in a thread scheduler.

Option C) start() - This option is correct because the start() method is used to register a thread in a thread scheduler. When the start() method is called, the thread is added to the thread scheduler's queue and will be executed by the scheduler when its turn comes.

Option D) register() - This option is incorrect because there is no specific method named register() for registering a thread in a thread scheduler.

The correct answer is C) start(). This option is correct because the start() method is used to register a thread in a thread scheduler.

Which of the following will not directly cause a thread to stop?

  1. notify()

  2. wait()

  3. InputStream access

  4. sleep()


Correct Option: A

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

  1. Object

  2. Thread

  3. Runnable

  4. Class


Correct Option: A

AI Explanation

To answer this question, you need to understand the concept of multi-threading in Java. The wait(), notify(), and notifyAll() methods are used for inter-thread communication and are defined in the Object class.

Let's go through each option to understand why it is correct or incorrect:

Option A) Object - This option is correct because the wait(), notify(), and notifyAll() methods are defined in the Object class. Every class in Java is a subclass of the Object class, and therefore, these methods are available in all classes.

Option B) Thread - This option is incorrect because the Thread class is responsible for creating and managing threads, but it does not define the wait(), notify(), and notifyAll() methods.

Option C) Runnable - This option is incorrect because the Runnable interface is used to define a task that can be executed by a thread. It does not define the wait(), notify(), and notifyAll() methods.

Option D) Class - This option is incorrect because the Class class is used to represent classes and interfaces in Java. It does not define the wait(), notify(), and notifyAll() methods.

The correct answer is Option A) Object. This option is correct because the wait(), notify(), and notifyAll() methods are defined in the Object class.

Therefore, the correct answer is Option A) Object.

- Hide questions