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.

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

  1. wait()

  2. notify()

  3. notifyall()

  4. exits synchronized code


Correct Option: A

AI Explanation

To answer this question, we need to understand the concepts of thread synchronization and the methods wait(), notify(), and notifyAll().

Option A) wait() - This option is correct. The wait() method is used to suspend the execution of a thread and releases the lock it holds. It allows other threads to acquire the lock and continue their execution. The thread will remain in a suspended state until another thread calls the notify() or notifyAll() method.

Option B) notify() - This option is incorrect. The notify() method is used to wake up a single thread that is waiting on the same object. It does not stop the execution of a thread, but rather notifies one waiting thread to resume execution.

Option C) notifyAll() - This option is incorrect. The notifyAll() method is used to wake up all threads that are waiting on the same object. It also does not stop the execution of a thread but rather notifies all waiting threads to resume execution.

Option D) exits synchronized code - This option is incorrect. Exiting synchronized code does not directly stop the execution of a thread. It simply releases the lock it holds and allows other threads to acquire the lock and continue their execution.

Therefore, the correct answer is option A) wait(). This option directly stops the execution of a thread by suspending it until another thread calls the notify() or notifyAll() method.

Please let me know if you need any further clarification.

  1. void run()

  2. public void run()

  3. public void start()

  4. void run(int priority)


Correct Option: B
  1. run();

  2. start();

  3. stop();

  4. main();


Correct Option: A
  1. run();

  2. construct();

  3. start();

  4. register();


Correct Option: C
  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