logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Threads See What Others Are Saying!
  • Question
  • Which of the following will not directly cause a thread to stop?


  • Options
  • A. notify()
  • B. wait()
  • C. InputStream access
  • D. sleep()

  • Correct Answer
  • notify() 

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

    Option B is wrong. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

    Option C is wrong. Methods of the InputStream class block until input data is available, the end of the stream is detected, or an exception is thrown. Blocking means that a thread may stop until certain conditions are met.

    Option D is wrong. sleep() - Causes the currently executing thread to sleep (temporarily cease execution) for a specified number of milliseconds. The thread does not lose ownership of any monitors.


    More questions

    • 1. You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

    • Options
    • A. public
    • B. private
    • C. protected
    • D. default access
    • Discuss
    • 2. What will be the output of the program?
      class Super
      { 
          public int i = 0; 
      
          public Super(String text) /* Line 4 */
          {
              i = 1; 
          } 
      } 
      
      class Sub extends Super
      {
          public Sub(String text)
          {
              i = 2; 
          } 
      
          public static void main(String args[])
          {
              Sub sub = new Sub("Hello"); 
              System.out.println(sub.i); 
          } 
      }
      

    • Options
    • A. 0
    • B. 1
    • C. 2
    • D. Compilation fails.
    • Discuss
    • 3. What will be the output of the program (in jdk1.6 or above)?
      public class BoolTest 
      {
          public static void main(String [] args) 
          {
              Boolean b1 = new Boolean("false");
              boolean b2;
              b2 = b1.booleanValue();
              if (!b2) 
              {
                  b2 = true;
                  System.out.print("x ");
              }
              if (b1 & b2) /* Line 13 */
              {
                  System.out.print("y ");
              }
              System.out.println("z");
          }
      }
      

    • Options
    • A. z
    • B. x z