Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
public class Main {
static double average(int... marks, String student) {
int sum = 0;
for (int m : marks) sum += m;
return (double) sum / marks.length;
}
public static void main(String[] args) {
System.out.println(average(80, 90, "Asha"));
}
}
- A average must return int
- B A varargs parameter must be the last parameter; write average(String student, int... marks) Correct
- C Varargs cannot be int
- D marks.length is invalid
Correct Answer: Option B
Otherwise the compiler could not tell where the variable arguments end.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!