Java OCA OCP Practice Question 3161

Question

Which statements, when inserted independently at (1), will guarantee that the following program will print [1, 9]?

import java.util.*;
public class Main {

 public static void main(String[] args) {
   // (1) INSERT DECLARATION HERE.
   collection.add(1); collection.add(9); collection.add(1);
   System.out.println(collection);
 }
}

Select the four correct answers.

  • (a) Collection<Integer> collection = new HashSet<Integer>();
  • (b) Set<Integer> collection = new HashSet<Integer>();
  • (c) HashSet<Integer> collection = new LinkedHashSet<Integer>();
  • (d) Set<Integer> collection = new LinkedHashSet<Integer>();
  • (e) Collection<Integer> collection = new TreeSet<Integer>();
  • (f) NavigableSet<Integer> collection = new TreeSet<Integer>();


(b) and (d)

Note

The methods add() and retainAll(), return the value true if the collection was modified during the operation.

The contains() and containsAll() methods return a boolean value, but these membership operations never modify the current collection, and the return value indicates the result of the membership test.

The clear() method does not return a value.




PreviousNext

Related