Core Java Test 4

40 Questions | Total Attempts: 524

SettingsSettingsSettings
Core Java Quizzes & Trivia

COLLECTION UTILITY PACKAGE


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

      If the equals()methood returns true,the hashCode() comparison==might return false.

    • B. 

      If the equals()methood returns false,the hashCode() comparison==might return true.

    • C. 

      If the hashCode() comparison==returns true,the equals() method must return true

    • D. 

      If the hashCode() comparison==returns true,the equals() method might return true

    • E. 

      If the hashCode() comparison!=returns true,the equals() method must return true

  • 2. 
    Which of the following most closely describes a bitset collection?
    • A. 

      A class that contains groups of unique sequences of bits. 

    • B. 

      A method for flipping individual bits in instance of a primitive type.

    • C. 

      An array of boolean primitives that indicate zeros or ones.

    • D. 

      A collection for storing bits as on-off information, like a vector of bits.

  • 3. 
    Which most closely matches a java description of a java map
    • A. 

      A vector of arrays for a 2D geographic representation.

    • B. 

      A class for containing unique array elements.

    • C. 

      A class for containing unique vector elements.

    • D. 

      An interface that ensures that implementing classes cannot contain duplicate keys.

  • 4. 
    Which collection class(es)allows you to grow or shrink its size and provides indexed access to its elements,but whose methods are not synchronised??(choose all that aaply)
    • A. 

      Java.util.HashSet

    • B. 

      Java.util.LinkedHashSet

    • C. 

      Java.util.List

    • D. 

      Java.util.ArrayList

    • E. 

      Java.util.Vector

    • F. 

      Java.util.PriorityQueue

  • 5. 
    The Enumeration interface allows a program to iterate its way through the elements of a container such as a Vector
    • A. 

      True

    • B. 

      False

  • 6. 
    The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set.
    • A. 

      True

    • B. 

      False

  • 7. 
    The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.
    • A. 

      True

    • B. 

      False

  • 8. 
    The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.
    • A. 

      True

    • B. 

      False

  • 9. 
    Public static Collection get()  { Collection sorted = new LinkedList();  sorted.add("B"); sorted.add("C"); sorted.add("A");  return sorted; } public static void main(String[] args)  {  for (Object obj: get())  {  System.out.print(obj + ", ");  } }   What is the result?
    • A. 

      A,B,C

    • B. 

      B,C,A

    • C. 

      Compilation fails

    • D. 

      The code runs with no output

    • E. 

      Exception is thrown at runtime

  • 10. 
    Properties method getProperty locates the value associated with the specified key. If the key is not found in this Properties object,getProperty attempts to locate the key in the default Properties object (if there is one). 
    • A. 

      True

    • B. 

      False

  • 11. 
    Import java.util.*; public class TryMe {     public static void main(String args[]) {         Queue<String> q = new PriorityQueue<String>();         q.add("3");         q.add("1");         q.add("2");         System.out.print(q.poll() + " ");         System.out.print(q.peek() + " ");         System.out.print(q.peek());     } }
    • A. 

      1 2 3

    • B. 

      3 2 1

    • C. 

      1 2 2

    • D. 

      3 1 1

    • E. 

      2 3 3

    • F. 

      1 1 2

  • 12. 
    Import java.util.*; public class TestArrayList { public static void main(String args[]) { List test =new ArrayList(); String s="hi"; testadd(s); testadd(s+s); System.out.println(test.size()); System.out.println(test.contains(42)); System.out.println(test.contains("hihi")); test.remove("hi"); System.out.println(test.size()); } }   What is the output of this program???  
    • A. 

      3 false true 2

    • B. 

      2 false true 3

    • C. 

      3 true false 2

    • D. 

      2 true false 3

  • 13. 
    Import java.util.*; public class WrappedString  {  private String s; public WrappedString(String s)  {  this.s = s;  }  public static void main(String[] args)  {  HashSeths = new HashSet(); WrappedString ws1 = new WrappedString("aardvark");  WrappedString ws2 = new WrappedString("aardvark");  String s1 = new String("aardvark"); String s2 = new String("aardvark");  hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2); System.out.println(hs.size());  }  }   What is the result?  
    • A. 

      0

    • B. 

      1

    • C. 

      2

    • D. 

      3

    • E. 

      4

    • F. 

      Compilation error

    • G. 

      Exception is thrown at runtime

  • 14. 
    Import java.util.*; public class PQ { public static void main(String[] args)  {  PriorityQueue pq = new PriorityQueue(); pq.add("carrot"); pq.add("apple"); pq.add("banana");  System.out.println(pq.poll() + ":" + pq.peek()); }  }   What is the result?
    • A. 

      Apple:apple

    • B. 

      Carrot:apple

    • C. 

      Apple:banana

    • D. 

      Banana:apple

    • E. 

      Carrot:carrot

    • F. 

      Carrot:banana

  • 15. 
    Import java.util.*; public class Example  { public static void main(String[] args) { // insert code here set.add(new Integer(2));  set.add(new Integer(1)); System.out.println(set);  }  }   Which code, inserted at line 4, guarantees that this program will output [1, 2]?
    • A. 

      Set set = new TreeSet();

    • B. 

      Set set = new HashSet();

    • C. 

      Set set = new SortedSet();

    • D. 

      Set set = new LinkedHashSet();

    • E. 

      Set set = new SortedList();

  • 16. 
    If you do not specify a capacity increment, the system will automatically add 2 to the size of the Vector each time additional capacity is needed. 
    • A. 

      True

    • B. 

      False

  • 17. 
    How does the set collection deal with duplicate elements?
    • A. 

      An exception is thrown if you attempt to add an element with a duplicate value.

    • B. 

      The add method returns false if you attempt to add an element with a duplicate value.

    • C. 

      A set may contain elements that return duplicate values from a call to the equals method.

    • D. 

      Duplicate values will cause an error at compile time.

  • 18. 
    Given: TreeSet map=new TreeSet(); map.add("one"); map.add("two"); map.add("three"); map.add("four"); map.add("one"); Iterator it=map.iterator(); while(it.hasNext()) { System.out.println(it.next()+" "); }   what is the result ???
    • A. 

      Compialtion fails

    • B. 

      One two three four

    • C. 

      Four one three two

    • D. 

      One two three four one

    • E. 

      One four three two one

    • F. 

      An exception is thrown at runtime

    • G. 

      The print order is not guaranteed

  • 19. 
    Given: import java.util.* class AlgaeDiesel{ public static void main(String[] args) { String sa={"foo","bar","baz" }; //insert method invocations here } }   what java.util.Arrays and/or java.util.Collections methods could you use  to convert sa to a List and then search the List to find the index  of the element whose valueis "foo"??(choose three methods)
    • A. 

      Sort()

    • B. 

      AsList()

    • C. 

      ToList

    • D. 

      Search()

    • E. 

      SortList()

    • F. 

      Contains()

    • G. 

      BinarySearch()

  • 20. 
    Given:  public static void search(List list)  {  list.clear();    list.add("b");    list.add("a");    list.add("c");    System.out.println(Collections.binarySearch(list, "a"));  }   What is the result of calling search with a valid List implementation?
    • A. 

      0

    • B. 

      1

    • C. 

      2

    • D. 

      A

    • E. 

      B

    • F. 

      C

    • G. 

      Result is undefined

  • 21. 
    Given: ArrayList a = new ArrayList(); containing the values {“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”} Which code will return 2?
    • A. 

      Collections. sort(a, a.reverse()); int result = Collections.binarySearch(a, “6”);

    • B. 

      Comparator c = Collections.reverseOrder(); Collections.sort(a, c); int result = Collections.binarySearch(a, “6”);

    • C. 

      Comparator c = Collections.reverseOrder(a); Collections.sort(a, c); int result = Collections.binarySearch(a, “6”,c);

    • D. 

      Comparator c = new InverseComparator(new Comparator()); Collections.sort(a); int result = Collections.binarySearch(a, “6”,c);

  • 22. 
    Given:  public class Key { private long id1; private long id2;    // class Key methods    }   A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap.Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.)
    • A. 

      Public int hashCode()

    • B. 

      Public boolean equals(Key k)

    • C. 

      Public int compareTo(Object o)

    • D. 

      Public boolean equals(Object o)

    • E. 

      Public boolean compareTo(Key k)

  • 23. 
    Given:  import java.util.*;   public class LetterASort {  public static void main(String[] args)  {  ArrayList strings = new ArrayList();    strings.add("aAaA");   strings.add("AaA");   strings.add("aAa");    strings.add("AAaa");    Collections.sort(strings);    for (String s : strings) { System.out.print(s + " ");  }  }  }   What is the result?
    • A. 

      Compilation fails.

    • B. 

      AAaA aAa AAaa AaA

    • C. 

      AAaa AaA aAa aAaA

    • D. 

      AaA AAaa aAaA aAa

    • E. 

      aAa AaA aAaA AAaa

    • F. 

      An exception is thrown at runtime.

  • 24. 
    Given: import java.util.*; class Flubber { public static void main(String[] args) { List x=new ArrayList(); x.add("x"); x.add("xx"); x.add("Xx"); //insert code here for(String s:x) System.out.println(s); }} the output is: xx Xx  x which code, inserted will produce the preceding output?? (choose al that apply)  
    • A. 

      Collections.sort(x);

    • B. 

      Comparable c=Collections.reverse(); Collections.sort(x,c);

    • C. 

      Collections.sort(x,c);

    • D. 

      Comparable c=Collections.reverseOrder(); Collections.sort(x,c);

    • E. 

      Comparator c=Collections.reverseOrder(); Collections.sort(x,c);

  • 25. 
    Given the code. What is the result? public class TrickyNum<X extends Number> {         private X x;        public TrickyNum(X x) {         this.x = x;     }     private double getDouble() {         return x.doubleValue();     }     public static void main(String args[]) {         TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));         System.out.print(a.getDouble());     } }
    • A. 

      Compilation fails.

    • B. 

      An exception is thrown at runtime.

    • C. 

      "1.0" is printed.

    • D. 

      "1" is printed.

Back to Top Back to top