0
0
Javaprogramming~15 mins

Method calling in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Master of Method Calling
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of method call with recursion
What is the output of this Java program when the printNumbers method is called with argument 3?
Java
public class Test {
    public static void printNumbers(int n) {
        if (n == 0) return;
        System.out.print(n + " ");
        printNumbers(n - 1);
    }

    public static void main(String[] args) {
        printNumbers(3);
    }
}
A3 2 1
B1 2 3
C3 2 1 0
D0 1 2 3
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Output of method call with overloaded methods
What is the output of this Java program?
Java
public class OverloadTest {
    public static void display(int a) {
        System.out.println("int: " + a);
    }

    public static void display(String a) {
        System.out.println("String: " + a);
    }

    public static void main(String[] args) {
        display(5);
        display("hello");
    }
}
A
int: 5
String: hello
B
String: 5
int: hello
C
int: 5
int: hello
D
String: 5
String: hello
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Identify the error in method call
What error does this Java code produce when compiled?
Java
public class ErrorTest {
    public static void greet() {
        System.out.println("Hello");
    }

    public static void main(String[] args) {
        greet("World");
    }
}
ARuntime error: NullPointerException
BCompilation error: method greet() in class ErrorTest cannot be applied to given types
CCompilation error: missing return statement
DNo error, prints "Hello World"
Attempts:
2 left
💻 code output
advanced
2:00remaining
Output of method call with static and instance methods
What is the output of this Java program?
Java
public class StaticInstance {
    public static void staticMethod() {
        System.out.println("Static method called");
    }

    public void instanceMethod() {
        System.out.println("Instance method called");
    }

    public static void main(String[] args) {
        StaticInstance.staticMethod();
        StaticInstance obj = new StaticInstance();
        obj.instanceMethod();
    }
}
A
Static method called
Static method called
B
Instance method called
Static method called
C
Static method called
Instance method called
D
Instance method called
Instance method called
Attempts:
2 left
💻 code output
expert
3:00remaining
Output of method call with method overriding and polymorphism
What is the output of this Java program?
Java
class Parent {
    void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child show");
    }
}

public class TestPolymorphism {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}
ARuntime error: ClassCastException
BParent show
CCompilation error: cannot override show()
DChild show
Attempts:
2 left