Answer to Question #60063 in Java | JSP | JSF for MB Knudsen

Question #60063
Which of the following methods will compile without error?

public int add(List<? extends Number> list) {
int sum = 0;
for (Integer i : list) sum += i;
return sum;
}

public int add(List<? super Integer> list) {
int sum = 0;
for (Integer i : list) sum += i;
return sum;
}

public int add(List<? extends Integer> list) {
int sum = 0;
for (Integer i : list) sum += i;
return sum;
}

public int add(List<?> list) {
int sum = 0;
for (Integer i : list) sum += i;
return sum;
}
1
Expert's answer
2016-05-24T11:50:03-0400
public int add(List<? extends Integer> list) {
int sum = 0;
for (Integer i : list) sum += i;
return sum;
}
Because only in this method is a valid class cast

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS