0
0
Javaprogramming~15 mins

Method declaration in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of a method with parameters
What is the output of the following Java program?
Java
public class Test {
    public static int multiply(int a, int b) {
        return a * b;
    }
    public static void main(String[] args) {
        System.out.println(multiply(3, 4));
    }
}
A7
B12
C34
DError: method multiply not found
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Return type and output
What will be printed when this Java code runs?
Java
public class Demo {
    public static String greet() {
        return "Hello!";
    }
    public static void main(String[] args) {
        System.out.println(greet());
    }
}
Anull
Bgreet
CHello!
DCompilation error
Attempts:
2 left
💻 code output
advanced
2:00remaining
Method overloading output
What is the output of this Java program with overloaded methods?
Java
public class Overload {
    public static int add(int a, int b) {
        return a + b;
    }
    public static double add(double a, double b) {
        return a + b;
    }
    public static void main(String[] args) {
        System.out.println(add(2, 3));
        System.out.println(add(2.0, 3.0));
    }
}
A
5.0
5
B
5
5
CCompilation error due to ambiguous method call
D
5
5.0
Attempts:
2 left
💻 code output
advanced
2:00remaining
Void method and output
What will be printed when this Java program runs?
Java
public class VoidTest {
    public static void printMessage() {
        System.out.println("Hi there!");
    }
    public static void main(String[] args) {
        printMessage();
        System.out.println("Done");
    }
}
A
Hi there!
Done
B
Done
Hi there!
CHi there!Done
DCompilation error: void method used in print statement
Attempts:
2 left
💻 code output
expert
2:00remaining
Method recursion output
What is the output of this Java program using recursion?
Java
public class Recursion {
    public static int factorial(int n) {
        if (n <= 1) return 1;
        else return n * factorial(n - 1);
    }
    public static void main(String[] args) {
        System.out.println(factorial(4));
    }
}
A24
B10
CError: StackOverflowError
D1
Attempts:
2 left