DEV Community

Cover image for Method Overloading Tricky Questions :
Kavitha
Kavitha

Posted on

Method Overloading Tricky Questions :

1.

class Test {
    void show(int a) { }
    void show(long a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)

2.

class Test {
    void show(Integer a) { }
    void show(int a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}

Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)
3.

class Test {
    void show(int a, float b) { }
    void show(float a, int b) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10, 10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: Compile-time Error
4.

class Test {
    void show(int... a) { }
    void show(int a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)

5.

class Test {
    void show(int a) { }
    void show(int... a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show();
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int... a)

6.

class Test {
    void show(double a) { }
    void show(float a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: compile time error
7.

class Test {
    void show(Object o) { }
    void show(String s) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(null);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(String s)
8.

class Test {
    void show(String s) { }
    void show(Integer i) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(null);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(String s)

9.

class Test {
    void show(int a, int b) { }
    void show(int... a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10,20);
    }
}

Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a, int b)

10.

class Test {
    void show(int a) { }
    static void show(int a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: Compile time error

11.

class Test {
    void show(long a) { }
    void show(float a) { }

    public static void main(String[] args) {
        new Test().show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: compile time error
12.

class Test {
    void show(int a) { }
    void show(int a, int... b) { }

    public static void main(String[] args) {
        new Test().show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)
13.

class Test {
    void show(char c) { }
    void show(int i) { }

    public static void main(String[] args) {
        new Test().show('A');
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER:show(char c)

14.

class Test {
    void show(byte b) { }
    void show(short s) { }

    public static void main(String[] args) {
        byte b = 10;
        new Test().show(b);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER:show(byte b)
15.

class Test {
    void show(Object o) { }
    void show(String s) { }
    void show(CharSequence c) { }

    public static void main(String[] args) {
        new Test().show("Java");
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER:show(String s)

Top comments (0)