🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Generics: all 10 questions

Quiz: Java Generics · Question 3 of 10

Does List<Number nums = new ArrayList<Integer(); compile?

Does List<Number> nums = new ArrayList<Integer>(); compile?

  1. A.Yes, because Integer extends Number
  2. B.No — List<Integer> is not a subtype of List<Number>
  3. C.Yes, but it throws at runtime
  4. D.Only with the diamond operator
Answer & explanation

Correct answer: No — List<Integer> is not a subtype of List<Number>

If it were allowed you could add a Double into a list of Integers. Use List<? extends Number> to accept it.

Can you get the rest right without peeking?

Take the timed Quiz: Java Generics quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →