LearnFrenzy
Sign in Take a free test

Methods and Recursion · Exercise 3 of 4

Methods and Recursion Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program contains a method mistake that javac rejects. Choose the option that names the real problem.

Question 1 Easy

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

Main.java
public class Main {
    static String grade(int marks) {
        if (marks >= 60) return "pass";
        else if (marks >= 0) return "fail";
    }

    public static void main(String[] args) {
        System.out.println(grade(70));
    }
}
A grade must be void
B When marks is negative no return is reached; every path of a non-void method must return a value (add a final return or throw)
C else if is not allowed
D marks must be final
Question page & discussion
Question 2 Easy

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

Main.java
public class Main {
    int square(int x) { return x * x; }

    public static void main(String[] args) {
        System.out.println(square(5));
    }
}
A square must return long
B square is an instance method and main is static, so there is no object to call it on; make square static or call new Main().square(5)
C println cannot call methods
D 5 must be a variable
Question page & discussion
Question 3 Easy

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

Main.java
public class Main {
    static int parse(String s) { return Integer.parseInt(s); }
    static double parse(String s) { return Double.parseDouble(s); }

    public static void main(String[] args) {
        System.out.println(parse("42"));
    }
}
A parse is a reserved name
B Both methods have the same signature parse(String); a different return type alone does not make a valid overload
C Strings cannot be parsed
D main must be first
Question page & discussion
Question 4 Easy

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)
C Varargs cannot be int
D marks.length is invalid
Question page & discussion
Question 5 Easy

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

Main.java
public class Main {
    static void total(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        total(2, 3);
    }
}
A a + b overflows
B A void method cannot return a value; declare the return type int
C return is not allowed in static methods
D total needs three parameters
Question page & discussion
Question 6 Hard

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

Main.java
public class Main {
    static void log(String message) { System.out.println("String: " + message); }
    static void log(Integer code) { System.out.println("Integer: " + code); }

    public static void main(String[] args) {
        log(null);
    }
}
A log cannot be overloaded
B null fits both String and Integer and neither is more specific, so the call is ambiguous; cast it, e.g. log((String) null)
C null cannot be passed to methods
D log must return a value
Question page & discussion
Question 7 Easy

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

Main.java
public class Main {
    static double bmi(double kg, double metres) { return kg / (metres * metres); }

    public static void main(String[] args) {
        System.out.println(bmi(70));
    }
}
A 70 must be 70.0
B bmi needs two arguments; Java has no default parameter values – pass both, or add an overload bmi(double kg)
C bmi must be public
D Math is missing
Question page & discussion
Question 8 Easy

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

Main.java
public class Main {
    static int average(int a, int b) {
        return (a + b) / 2.0;
    }

    public static void main(String[] args) {
        System.out.println(average(3, 4));
    }
}
A 2.0 is not a number
B (a + b) / 2.0 is a double, which cannot be returned as int without a cast; return double or cast
C average must be void
D a and b must be long
Question page & discussion
Question 9 Easy

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

Main.java
public class Main {
    static int discount(int price) {
        return price / 10;
        System.out.println("discount computed");
    }

    public static void main(String[] args) {
        System.out.println(discount(500));
    }
}
A price must be double
B The println comes after return in the same block, so it is unreachable – javac rejects it
C Methods cannot print
D return needs parentheses
Question page & discussion
Question 10 Medium

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

Main.java
public class Main {
    static int scale(int value) {
        int value = 10;
        return value * 2;
    }

    public static void main(String[] args) {
        System.out.println(scale(3));
    }
}
A scale must be void
B value is already defined as a parameter; a local variable cannot redeclare it in the same scope
C 10 is too small
D return must use a new variable
Question page & discussion
Up next · Exercise 4Methods and Recursion Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links