136k views
1 vote
Given the following methods, write down the printed output of the method calls: (5 points) public static void doSomething(String x) { System.out.println("A"); } public static void doSomething(int x) { System.out.println("B"); } public static void doSomething(double x) { System.out.println("C"); } public static void doSomething(String x, int y) { System.out.println("D"); } public static void doSomething(int x, String y) { System.out.println("E"); } public static void doSomething(double x, int y) { System.out.println("F"); } Method calls

1. doSomething(5);
2. doSomething (5.2, 9);
3. doSomething(3, "Hello");
4. doSomething("Able", 8)

1 Answer

2 votes

Answer:

1. doSomething(5);

B

2. doSomething (5.2, 9);

F

3. doSomething(3, "Hello");

E

4. doSomething("Able", 8)

D

Step-by-step explanation:

The methods presented in the question are overloaded method. Overloaded methods are the feature that is available in some programming languages such as Java where two or more methods can share the same name but with different argument list.

For example,

public static void doSomething(String x)

{

System.out.println("A");

}

public static void doSomething(int x)

{

System.out.println("B");

}

The two overloaded methods above different from one another with the data types (String vs int) in their argument list.

public static void doSomething(String x, int y)

{

System.out.println("D");

}

public static void doSomething(int x, String y)

{

System.out.println("E");

}

Besides, overloaded methods can also possess different sequential order of the arguments. (String , int VS int, String)

A specific version of the overloaded methods will be invoked if the argument list that passed to the method is matched. For example, doSomething(5) will invoke

public static void doSomething(int x)

{

System.out.println("B");

}

whereas doSomething (3, "Hello") will invoke

public static void doSomething(int x, String y)

{

System.out.println("E");

}

User Disperse
by
6.8k points