The knowledge sharing zone - Guruzon.com

OCJP Collections Questions

This page contains the lot of Java Collections questions and answers for OCJP exam preparation. All these are object questions or multiple choice questions with program code snippet.
reviews

OCJP Collections Questions and Answers : 1

Question : Given :

34. HashMap props = new HashMap();
35. props.put("key45", "some value");
36. props.put("key12", "some other value");
37. props.put("key39", "yet another value");
38. Set s = props.keySet();
39. // insert code here

What, inserted at line 39, will sort the keys in the props HashMap?

A.  Arrays.sort(s);
B.  s = new TreeSet(s);
C.  Collections.sort(s);
D.  s = new SortedSet(s);

Answer : B


SCJP Collections Questions and Answers : 2

Question : Given :

23. Object [] myObjects = {
24.   new Integer(12),
25.   new String("foo"),
26.   new Integer(5),
27.   new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0; i<myObjects.length; i++) {
31.   System.out.print(myObjects[i].toString());
32.   System.out.print(" ");
33. }

What is the result?

A.   Compilation fails due to an error in line 23.
B.   Compilation fails due to an error in line 29.
C.   A ClassCastException occurs in line 29.
D.   A ClassCastException occurs in line 31.
E.   The value of all four objects prints in natural order.

Answer : C


OCJP Collections Questions and Answers : 3

Question : Given :

1. import java.util.*;
2. public class Old {
3.  public static Object get0(List list) {
4.   return list.get(0);
5.  }
6. }

 Which three will compile successfully? (Choose three.)

A.  Object o = Old.get0(new LinkedList());
B.  Object o = Old.get0(new LinkedList<?>());
C.  String s = Old.get0(new LinkedList<String>());
D.  Object o = Old.get0(new LinkedList<Object>());
E.  String s = (String)Old.get0(new LinkedList<String>());

Answer : A, D, E


SCJP Collections Questions and Answers : 4

Question : Given :

1. import java.util.*;
2. public class Example {
3.  public static void main(String[] args) {
4.   // insert code here
5.   set.add(new Integer(2));
6.   set.add(new Integer(1));
7.   System.out.println(set);
8.  }
9. }

 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.   List set = new SortedList();
E.   Set set = new LinkedHashSet();

Answer : A


OCJP Collections Questions and Answers : 5

Question : Given :

11. public static Collection get() {
12.  Collection sorted = new LinkedList();
13.  sorted.add("B"); sorted.add("C"); sorted.add("A");
14.  return sorted;
15. }
16. public static void main(String[] args) {
17.  for (Object obj: get()) {
18.   System.out.print(obj + ", ");
19.  }
20. }

 What is the result?

A.   A, B, C,
B.   B, C, A,
C.   Compilation fails.
D.   The code runs with no output.
E.   An exception is thrown at runtime.

Answer : B


SCJP Collections Questions and Answers : 6

Question : Given :

11. public void genNumbers() {
12.  ArrayList numbers = new ArrayList();
13.  for (int i=0; i<10; i++) {
14.   int value = i * ((int) Math.random());
15.   Integer intObj = new Integer(value);
16.   numbers.add(intObj);
17.  }
18.  System.out.println(numbers);
19. }

 Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for
garbage collection?

A.  Line 16
B.  Line 17
C.  Line 18
D.  Line 19
E.  The object is NOT a candidate for garbage collection.

Answer : D


OCJP Collections Questions and Answers : 7

Question : Given :

11. public static Iterator reverse(List list) {
12.  Collections.reverse(list);
13.  return list.iterator();
14. }
15. public static void main(String[] args) {
16.  List list = new ArrayList();
17.  list.add("1"); list.add("2"); list.add("3");
18.  for (Object obj: reverse(list))
19.   System.out.print(obj + ", ");
20. }

 What is the result?

A.   3, 2, 1,
B.   1, 2, 3,
C.   Compilation fails.
D.   The code runs with no output.
E.   An exception is thrown at runtime.

Answer : C


SCJP Collections Questions and Answers : 8

Question : Given :

1. import java.util.*;
2. public class PQ {
3.  public static void main(String[] args) {
4.   PriorityQueue<String> pq = new PriorityQueue<String>();
5.   pq.add("carrot");
6.   pq.add("apple");
7.   pq.add("banana");
8.   System.out.println(pq.poll() + ":" + pq.peek());
9.   }
10. }

 What is the result?

A.   apple:apple
B.   carrot:apple
C.   apple:banana
D.   banana:apple
E.   carrot:carrot
F.   carrot:banana

Answer : C


SCJP Collections Questions and Answers : 10

Question : Given :


1. import java.util.*;
2. public class WrappedString {
3.  private String s;
4.  public WrappedString(String s) { this.s = s; }
5.  public static void main(String[] args) {
6.   HashSet<Object> hs = new HashSet<Object>();
7.   WrappedString ws1 = new WrappedString("aardvark");
8.   WrappedString ws2 = new WrappedString("aardvark");
9.   String s1 = new String("aardvark");
10.  String s2 = new String("aardvark");
11.   hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12.   System.out.println(hs.size()); } }

 What is the result?

A.   0
B.   1
C.   2
D.   3
E.   4
F.   Compilation fails.
G.   An exception is thrown at runtime.

Answer : D


OCJP Collections Questions and Answers : 11

Question : Given :

11. public class Key {
12.  private long id1;
13.  private long id2;
14.
15.  // class Key methods
16. }

 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)

Answer : A, D


SCJP Collections Questions and Answers : 12

Question : Given :

11. public static int sum(List list) {
12.  int sum = 0;
13.  for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14.   int i = ((Integer)iter.next()).intValue();
15.   sum += i;
16.  }
17.  return sum;
18. }

 Which three changes must be made to the method sum to use generics? (Choose three.)

A.  remove line 14
B.  replace line 14 with "int i = iter.next();"
C.  replace line 13 with "for (int i : intList) {"
D.  replace line 13 with "for (Iterator iter : intList) {"
E.  replace the method declaration with "sum(List<int> intList)"
F.  replace the method declaration with "sum(List<Integer> intList)"

Answer : A, C, F


OCJP Collections Questions and Answers : 13

Question : Given :

1. import java.util.*;
2.
3. public class LetterASort{
4.  public static void main(String[] args) {
5.   ArrayList<String> strings = new ArrayList<String>();
6.   strings.add("aAaA");
7.   strings.add("AaA");
8.   strings.add("aAa");
9.   strings.add("AAaa");
10.   Collections.sort(strings);
11.   for (String s : strings) { System.out.print(s + " "); }
12.  }
13. }

 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.

Answer : C


SCJP Collections Questions and Answers : 14

Question : Given :

1. public class Score implements Comparable<Score> {
2.  private int wins, losses;
3.  public Score(int w, int l) { wins = w; losses = l; }
4.  public int getWins() { return wins; }
5.  public int getLosses() { return losses; }
6.  public String toString() {
7.   return "<" + wins + "," + losses + ">";
8.  }
9.  // insert code here
10.}

 Which method will complete this class?

A.  public int compareTo(Object o){/*more code here*/}
B.  public int compareTo(Score other){/*more code here*/}
C.  public int compare(Score s1,Score s2){/*more code here*/}
D.  public int compare(Object o1,Object o2){/*more code here*/}

Answer : B


OCJP Collections Questions and Answers : 15

Question : Given :

13. public static void search(List<String> list) {
14.   list.clear();
15.   list.add("b");
16.   list.add("a");
17.   list.add("c");
18.   System.out.println(Collections.binarySearch(list, "a"));
19. }

 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.   The result is undefined.

Answer : G


SCJP Collections Questions and Answers : 16

Question : Given :

11. // insert code here
12.   private N min, max;
13.   public N getMin() { return min; }
14.   public N getMax() { return max; }
15.   public void add(N added) {
16.     if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added;
18.     if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
20.   }
21. }

Which two, inserted at line 11, will allow the code to compile? (Choose two.)

A.   public class MinMax<?> {
B.   public class MinMax<? extends Number> {
C.   public class MinMax<N extends Object> {
D.   public class MinMax<N extends Number> {
E.   public class MinMax<? extends Object> {
F.   public class MinMax<N extends Integer> {

Answer : D, F


OCJP Collections Questions and Answers : 17

Question : Given :

10. interface A { void x(); }
11. class B implements A { public void x() {} public void y() {} }
12. class C extends B { public void x() {} }

And:

20. java.util.List<A> list = new java.util.ArrayList<A>();
21. list.add(new B());
22. list.add(new C());
23. for (A a : list) {
24.   a.x();
25.   a.y();
26. }

 What is the result?

A.   The code runs with no output.
B.   An exception is thrown at runtime.
C.   Compilation fails because of an error in line 20.
D.   Compilation fails because of an error in line 21.
E.   Compilation fails because of an error in line 23.
F.   Compilation fails because of an error in line 25.

Answer : F


SCJP Collections Questions and Answers : 18

Question : Given :

11. public void genNumbers() {
12.  ArrayList numbers = new ArrayList();
13.  for (int i=0; i<10; i++) {
14.   int value = i * ((int) Math.random());
15.   Integer intObj = new Integer(value);
16.   numbers.add(intObj);
17.  }
18.  System.out.println(numbers);
19. }

 Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for
garbage collection?

A.  Line 16
B.  Line 17
C.  Line 18
D.  Line 19
E.  The object is NOT a candidate for garbage collection.

Answer : D


OCJP Collections Questions and Answers : 19

Question : Given :

11. public static void append(List list) { list.add("0042"); }
12. public static void main(String[] args) {
13.  List<Integer> intList = new ArrayList<Integer>();
14.  append(intList);
15.  System.out.println(intList.get(0));
16. }

 What is the result?

A.  42
B.  0042
C.  An exception is thrown at runtime.
D.  Compilation fails because of an error in line 13.
E.  Compilation fails because of an error in line 14.

Answer : B


SCJP Collections Questions and Answers : 20

Question : Given :

11. public class Person {
12.  private name;
13.  public Person(String name) {
14.   this.name = name;
15.  }
16.  public int hashCode() {
17.   return 420;
18.  }
19. }

Which statement is true?

A.   The time to find the value from HashMap with a Person key depends on the size of the map.
B.   Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C.   Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate.
D.   The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

Answer : A


OCJP Collections Questions and Answers : 21

Question : Given :

enum Example { ONE, TWO, THREE }

Which statement is true?

A.   The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.
B.   The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is guaranteed to be less than one.
C.   The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a java.util.EnumMap.
D.   The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable.

Answer : A


SCJP Collections Questions and Answers : 22

Question : Given :

A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add(0, object), but does NOT need to support quick random access. What supports these requirements?

A.  java.util.Queue
B.  java.util.ArrayList
C.  java.util.LinearList
D.  java.util.LinkedList

Answer : D


OCJP Collections Questions and Answers : 23

Question : Given :

int[] myArray = new int[] {1, 2, 3, 4, 5};

What allows you to create a list from this array?

A.  List myList = myArray.asList();
B.  List myList = Arrays.asList(myArray);
C.  List myList = new ArrayList(myArray);
D.  List myList = Collections.fromArray(myArray);

Answer : B


 

 

You would also like to read:

OCJP Core Java Questions
OCJP Java JDBC Questions
OCJP Java IO Questions

Comments :

Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow