logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Objects and Collections See What Others Are Saying!
  • Question
  • Which interface provides the capability to store objects using a key-value pair?


  • Options
  • A. Java.util.Map
  • B. Java.util.Set
  • C. Java.util.List
  • D. Java.util.Collection

  • Correct Answer
  • Java.util.Map 

    Explanation
    An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

    More questions

    • 1. What will be the output of the program?
      import java.util.*;
      public class NewTreeSet2 extends NewTreeSet 
      {
          public static void main(String [] args) 
          {
              NewTreeSet2 t = new NewTreeSet2();
              t.count();
          }
      }
      protected class NewTreeSet
      {
          void count() 
          {
              for (int x = 0; x < 7; x++,x++ ) 
              {
                  System.out.print(" " + x);
              }
          }
      }
      

    • Options
    • A. 0 2 4
    • B. 0 2 4 6
    • C. Compilation fails at line 2
    • D. Compilation fails at line 10
    • Discuss
    • 2. What will be the output of the program?
      int x = 3; 
      int y = 1; 
      if (x = y) /* Line 3 */
      {
          System.out.println("x =" + x); 
      }
      

    • Options
    • A. x = 1
    • B. x = 3
    • C. Compilation fails.
    • D. The code runs with no output.
    • Discuss
    • 3. What will be the output of the program?
      public static void main(String[] args) 
      {
          Object obj = new Object() 
          {
              public int hashCode() 
              {
                  return 42;
              }
          }; 
          System.out.println(obj.hashCode()); 
      }
      

    • Options
    • A. 42
    • B. Runtime Exception
    • C. Compile Error at line 2
    • D. Compile Error at line 5
    • Discuss
    • 4. What will be the output of the program?
      public class SyncTest 
      {
          public static void main (String [] args) 
          {
              Thread t = new Thread() 
              {
                  Foo f = new Foo();
                  public void run() 
                  {
                      f.increase(20);
                  }
              };
          t.start();
          }
      }
      class Foo 
      {
          private int data = 23;
          public void increase(int amt) 
          {
              int x = data;
              data = x + amt;
          }
      }
      
      and assuming that data must be protected from corruption, what?if anything?can you add to the preceding code to ensure the integrity of data?

    • Options
    • A. Synchronize the run method.
    • B. Wrap a synchronize(this) around the call to f.increase().
    • C. The existing code will cause a runtime exception.
    • D. Synchronize the increase() method
    • Discuss
    • 5. What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

    • Options
    • A. public
    • B. abstract
    • C. protected
    • D. synchronized
    • E. default access
    • Discuss
    • 6. Which of the following statements is true?

    • Options
    • A. If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.
    • B. As of Java version 1.4, assertion statements are compiled by default.
    • C. With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.
    • D. When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.
    • Discuss
    • 7. What will be the output of the program?
      public class Switch2 
      {
          final static short x = 2;
          public static int y = 0;
          public static void main(String [] args) 
          {
              for (int z=0; z < 3; z++) 
              {
                  switch (z) 
                  {
                      case x: System.out.print("0 ");
                      case x-1: System.out.print("1 ");
                      case x-2: System.out.print("2 ");
                  }
              }
          }
      }
      

    • Options
    • A. 0 1 2
    • B. 0 1 2 1 2 2
    • C. 2 1 0 1 0 0
    • D. 2 1 2 0 1 2
    • Discuss
    • 8. Which is the valid declarations within an interface definition?

    • Options
    • A. public double methoda();
    • B. public final double methoda();
    • C. static void methoda(double d1);
    • D. protected void methoda(double d1);
    • Discuss
    • 9. Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

    • Options
    • A. TreeMap
    • B. HashMap
    • C. LinkedHashMap
    • D. The answer depends on the implementation of the existing instance.
    • Discuss
    • 10. Which is a valid declarations of a String?

    • Options
    • A. String s1 = null;
    • B. String s2 = 'null';
    • C. String s3 = (String) 'abc';
    • D. String s4 = (String) '\ufeed';
    • Discuss


    Comments

    There are no comments.

Enter a new Comment