LearnFrenzy
Sign in Take a free test

Methods and Recursion · Find the Error · Question 4

Error: a varargs parameter that is not last Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links