← 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?
- A.Yes, because Integer extends Number
- B.No — List<Integer> is not a subtype of List<Number>
- C.Yes, but it throws at runtime
- 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 →