Java OCA OCP Practice Question 789

Question

Which of the following use generics and compile without warnings? (Choose two.)

  • A. List<String> a = new ArrayList();
  • B. List<> b = new ArrayList();
  • C. List<String> c = new ArrayList<>();
  • D. List<> d = new ArrayList<>();
  • E. List<String> e = new ArrayList<String>();
  • F. List<> f = new ArrayList<String>();


C, E.

Note

The diamond operator is only allowed to be used when instantiating rather than declaring.

It can't go on the left side of the equal sign.

Options B, D, and F are incorrect.

Option A produces a warning because generics are not used on the right side of the assignment operator.

Options C and E are correct.

Option C is better than Option E since it uses the diamond operator rather than specifying a redundant type.




PreviousNext

Related