Tag: programming languages

Questions Related to programming languages

  1. erase

  2. copy

  3. push_back

  4. swap


Correct Option: C

Return size of allocated storage

  1. length

  2. size

  3. max_size

  4. capacity


Correct Option: D

Which of the following statements regarding threads is true?

  1. A thread dies as soon as the execution of the start() method ends.

  2. A thread's priority can be specified as an argument to its constructor

  3. A sleeping thread can be made runnable by invoking notify().

  4. A thread can call notify() on an object only if it holds the lock on that object.


Correct Option: D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) A thread dies as soon as the execution of the start() method ends. This option is incorrect because a thread continues to execute even after the start() method ends. The start() method is responsible for creating a new thread and invoking the run() method of that thread. The thread will continue to execute until the run() method completes or until it is explicitly interrupted or terminated.

Option B) A thread's priority can be specified as an argument to its constructor. This option is correct. When creating a new thread in Java, you can specify its priority using the Thread class constructor. The thread priority range is from 1 to 10, where 1 is the lowest priority and 10 is the highest priority.

Option C) A sleeping thread can be made runnable by invoking notify(). This option is incorrect. The notify() method is used to wake up a thread that is waiting on a specific object's monitor. It does not make a sleeping thread runnable. To make a sleeping thread runnable, you need to use the interrupt() method to interrupt the sleep and resume the execution of the thread.

Option D) A thread can call notify() on an object only if it holds the lock on that object. This option is correct. In Java, a thread can only call the notify() method on an object if it currently holds the lock (synchronized) on that object. This ensures that only the thread that has acquired the lock can notify other threads waiting on the same object's monitor.

The correct answer is: D. This option is correct because a thread can call notify() on an object only if it holds the lock on that object.

  1. Exactly 10 seconds after the start method is called, "Time's Up!" will be printed.

  2. Exactly 10 seconds after "Start!" is printed, "Time's Up!" will be printed.

  3. The delay between "Start!" being printed and "Time's Up!" will be 10 seconds plus or minus one tick of the system clock.

  4. If "Time's Up!" is printed you can be sure that at least 10 seconds have elapsed since "Start!" was printed.


Correct Option: D
  1. By calling notify(5)

  2. By calling notifyAll()

  3. By calling myThread.notify()

  4. By calling notify(myThread)

  5. None of these


Correct Option: D