Java OCA OCP Practice Question 1637

Question

Which options can fill in the blanks to print Cleaned 2 items?

import java.util.*;

class Logger<T ___ Collection> {
   T item;//  w  w  w  .  j  a va  2  s .  com
   public void clean(T items) {
      System.out.println("Cleaned " + items.size() + " items");
   }
}

public class Main {
   public static void main(String[] args) {
      Logger<List> wash = new
      wash.clean(Arrays.asList("A", "B"));   }
}
  • A. extends, Logger<ArrayList>();
  • B. extends, Logger<List>();
  • C. super, Logger<ArrayList>();
  • D. super, Logger<List>();


B.

Note

Options A and C are incorrect because a generic type cannot be assigned to another direct type unless you are using upper or lower bounds in that statement.

Now, we just have to decide whether a lower or upper bound is correct for the T formal type parameter in Logger.

The clue is that the method calls size().

This method is available on Collection and all classes that extend/implement it.

Therefore, Option B is correct.




PreviousNext

Related