logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Objects and Collections See What Others Are Saying!
  • Question
  • Assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true?
    x = 0;
    if (x1.hashCode() != x2.hashCode() )  x = x + 1;
    if (x3.equals(x4) )  x = x + 10;
    if (!x5.equals(x6) ) x = x + 100;
    if (x7.hashCode() == x8.hashCode() )  x = x + 1000;
    System.out.println("x = " + x);
    


  • Options
  • A. x2.equals(x1)
  • B. x3.hashCode() == x4.hashCode()
  • C. x5.hashCode() != x6.hashCode()
  • D. x8.equals(x7)

  • Correct Answer
  • x3.hashCode() == x4.hashCode() 

    Explanation
    By contract, if two objects are equivalent according to the equals() method, then the hashCode() method must evaluate them to be ==.

    Option A is incorrect because if the hashCode() values are not equal, the two objects must not be equal.

    Option C is incorrect because if equals() is not true there is no guarantee of any result from hashCode().

    Option D is incorrect because hashCode() will often return == even if the two objects do not evaluate to equals() being true.


  • More questions

    • 1. What will be the output of the program?
      class SSBool 
      {
          public static void main(String [] args) 
          {
              boolean b1 = true;
              boolean b2 = false;
              boolean b3 = true;
              if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
                  System.out.print("ok ");
              if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
                  System.out.println("dokey");
          }
      }
      

    • Options
    • A. ok
    • B. dokey
    • C. ok dokey
    • D. No output is produced
    • E. Compilation error
    • Discuss
    • 2. Which will legally declare, construct, and initialize an array?

    • Options
    • A. int [] myList = {"1", "2", "3"};
    • B. int [] myList = (5, 8, 2);
    • C. int myList [] [] = {4,9,7,0};
    • D. int myList [] = {4, 3, 7};
    • Discuss
    • 3. What will be the output of the program?
      class BitShift 
      {
          public static void main(String [] args) 
          {
              int x = 0x80000000;
              System.out.print(x + " and  ");
              x = x >>> 31;
              System.out.println(x);
          }
      }
      

    • Options
    • A. -2147483648 and 1
    • B. 0x80000000 and 0x00000001
    • C. -2147483648 and -1
    • D. 1 and -2147483648
    • Discuss
    • 4. Which of the following are Java reserved words?

      1. run
      2. import
      3. default
      4. implement

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 3 and 4
    • D. 2 and 4
    • Discuss
    • 5. What will be the output of the program?
      class Equals 
      {
          public static void main(String [] args) 
          {
              int x = 100;
              double y = 100.1;
              boolean b = (x = y); /* Line 7 */
              System.out.println(b);
          }
      }
      

    • Options
    • A. true
    • B. false
    • C. Compilation fails
    • D. An exception is thrown at runtime
    • Discuss
    • 6. Which interface does java.util.Hashtable implement?

    • Options
    • A. Java.util.Map
    • B. Java.util.List
    • C. Java.util.HashTable
    • D. Java.util.Collection
    • Discuss
    • 7. Which two of the following methods are defined in class Thread?

      1. start()
      2. wait()
      3. notify()
      4. run()
      5. terminate()

    • Options
    • A. 1 and 4
    • B. 2 and 3
    • C. 3 and 4
    • D. 2 and 4
    • Discuss
    • 8. 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..
    • Discuss
    • 9. What will be the output of the program?
      class MyThread extends Thread 
      {
          MyThread() 
          {
              System.out.print(" MyThread");
          }
          public void run() 
          {
              System.out.print(" bar");
          }
          public void run(String s) 
          {
              System.out.println(" baz");
          }
      }
      public class TestThreads 
      {
          public static void main (String [] args) 
          {
              Thread t = new MyThread() 
              {
                  public void run() 
                  {
                      System.out.println(" foo");
                  }
              };
              t.start();
          }
      }
      

    • Options
    • A. foo
    • B. MyThread foo
    • C. MyThread bar
    • D. foo bar
    • Discuss
    • 10. What will be the output of the program?
      String s = "hello"; 
      Object o = s; 
      if( o.equals(s) )
      {
          System.out.println("A"); 
      } 
      else
      {
          System.out.println("B"); 
      } 
      if( s.equals(o) )
      {
          System.out.println("C"); 
      } 
      else
      { 
          System.out.println("D"); 
      }
      
      1. A
      2. B
      3. C
      4. D

    • Options
    • A. 1 and 3
    • B. 2 and 4
    • C. 3 and 4
    • D. 1 and 2
    • Discuss


    Comments

    There are no comments.

Enter a new Comment