wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Method Overloading

Total questions: 6

Worksheet time: 3mins

Name
Class
Date
1.

What will be the output of the following program?

class A

{

 

}

 

class B extends A

{

 

}

 

class C extends B

{

 

}

 

public class MainClass

{

    static void overloadedMethod(A a)

    {

        System.out.println("ONE");

    }

 

    static void overloadedMethod(B b)

    {

        System.out.println("TWO");

    }

 

    static void overloadedMethod(Object obj)

    {

        System.out.println("THREE");

    }

 

    public static void main(String[] args)

    {

        C c = new C();

 

        overloadedMethod(c);

    }

}

a)

1

b)

2

c)

3

d)

4

2.

In a class, one method has two overloaded forms. One form is defined as static and another form is defined as non-static. Is that method properly overloaded?

a)

Yes

b)

No

3.

In the below Class X, is ‘method’ properly overloaded?

class X

{

    int method(int i, int d)

    {

        return i+d;

    }

 

    static int method(int i, double d)

    {

        return (int)(i+d);

    }

 

    double method(double i, int d)

    {

        return i+d;

    }

 

    static double method(double i, double d)

    {

        return i+d;

    }

}

a)

Yes

b)

No

4.

What will be the output of the following program?

class X

{

    void method(int a)

    {

        System.out.println("ONE");

    }

 

    void method(double d)

    {

        System.out.println("TWO");

    }

}

 

class Y extends X

{

    @Override

    void method(double d)

    {

        System.out.println("THREE");

    }

}

 

public class MainClass

{

    public static void main(String[] args)

    {

        new Y().method(100);

    }

}

a)

1

b)

2

c)

3

d)

4

5.

Method Overriding shows static polymorphism. True or false?

a)

True

b)

False

6.

In a class, One method has 4 overloaded forms. All have different access modifiers (private, default, protected and public). Is that method properly overloaded?

a)

Yes

b)

No