logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Threads See What Others Are Saying!
  • Question
  • What will be the output of the program?
    class MyThread extends Thread 
    {
        public static void main(String [] args) 
        {
            MyThread t = new MyThread(); /* Line 5 */
            t.run();  /* Line 6 */
        }
    
        public void run() 
        {
            for(int i=1; i < 3; ++i) 
            {
                System.out.print(i + "..");
            }
        }
    }
    


  • Options
  • A. This code will not compile due to line 5.
  • B. This code will not compile due to line 6.
  • C. 1..2..
  • D. 1..2..3..

  • Correct Answer
  • 1..2.. 

    Explanation
    Line 6 calls the run() method, so the run() method executes as a normal method should and it prints "1..2.."

    A is incorrect because line 5 is the proper way to create an object.

    B is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete.

    D is incorrect because the for loop only does two iterations.


    More questions

    • 1. Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?

      1. If the equals() method returns true, the hashCode() comparison == must return true.
      2. If the equals() method returns false, the hashCode() comparison != must return true.
      3. If the hashCode() comparison == returns true, the equals() method must return true.
      4. If the hashCode() comparison == returns true, the equals() method might return true.

    • Options
    • A. 1 and 4
    • B. 2 and 3
    • C. 3 and 4
    • D. 1 and 3
    • Discuss
    • 2. Which three are methods of the Object class?

      1. notify();
      2. notifyAll();
      3. isInterrupted();
      4. synchronized();
      5. interrupt();
      6. wait(long msecs);
      7. sleep(long msecs);
      8. yield();

    • Options
    • A. 1, 2, 4
    • B. 2, 4, 5
    • C. 1, 2, 6
    • D. 2, 3, 4
    • Discuss
    • 3. Which two can be used to create a new Thread?

      1. Extend java.lang.Thread and override the run() method.
      2. Extend java.lang.Runnable and override the start() method.
      3. Implement java.lang.Thread and implement the run() method.
      4. Implement java.lang.Runnable and implement the run() method.
      5. Implement java.lang.Thread and implement the start() method.

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 1 and 4
    • D. 3 and 4
    • Discuss
    • 4. What is the numerical range of a char?

    • Options
    • A. -128 to 127
    • B. -(215) to (215) - 1
    • C. 0 to 32767
    • D. 0 to 65535
    • Discuss
    • 5. Which four can be thrown using the throw statement?

      1. Error
      2. Event
      3. Object
      4. Throwable
      5. Exception
      6. RuntimeException

    • Options
    • A. 1, 2, 3 and 4
    • B. 2, 3, 4 and 5
    • C. 1, 4, 5 and 6
    • D. 2, 4, 5 and 6
    • Discuss
    • 6. Which three are valid method signatures in an interface?

      1. private int getArea();
      2. public float getVol(float x);
      3. public void main(String [] args);
      4. public static void main(String [] args);
      5. boolean setFlag(Boolean [] test);

    • Options
    • A. 1 and 2
    • B. 2, 3 and 5
    • C. 3, 4, and 5
    • D. 2 and 4
    • Discuss
    • 7. What will be the output of the program?
      class Bitwise 
      {
          public static void main(String [] args) 
          {
              int x = 11 & 9;
              int y = x ^ 3;
              System.out.println( y | 12 );
          }
      }
      

    • Options
    • A. 0
    • B. 7
    • C. 8
    • D. 14
    • Discuss
    • 8. Which two statements are true?

      1. Deadlock will not occur if wait()/notify() is used
      2. A thread will resume execution as soon as its sleep duration expires.
      3. Synchronization can prevent two objects from being accessed by the same thread.
      4. The wait() method is overloaded to accept a duration.
      5. The notify() method is overloaded to accept a duration.
      6. Both wait() and notify() must be called from a synchronized context.

    • Options
    • A. 1 and 2
    • B. 3 and 5
    • C. 4 and 6
    • D. 1 and 3
    • Discuss
    • 9. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

    • Options
    • A. java.util.HashSet
    • B. java.util.LinkedHashSet
    • C. java.util.List
    • D. java.util.ArrayList
    • Discuss
    • 10. Which of the following statements is true?

    • Options
    • A. In an assert statement, the expression after the colon ( : ) can be any Java expression.
    • B. If a switch block has no default, adding an assert default is considered appropriate.
    • C. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
    • D. It is appropriate to handle assertion failures using a catch clause.
    • Discuss


    Comments

    There are no comments.

Enter a new Comment