147k views
2 votes
Consider the mutually recursive methods below. Select the method call that could be used to generate the output sequence: A5 B4 A3 B2 A1 public static void methodA(int value) { if (value > 0) { System.out.print(" A" value); methodB(value - 1); } } public static void methodB(int value) { if (value > 0) { System.out.print(" B" value); methodA(value - 1); } }

User ALFmachine
by
5.8k points

1 Answer

7 votes

Answer:

methodA(5);

Step-by-step explanation:

When you make the call method(5); here is what happens:

methodA is called with value = 5, prints A5, and calls the methodB(4)

methodB is called with value = 4, prints B4, and calls the methodA(3)

methodA is called with value = 3, prints A3, and calls the methodB(2)

methodB is called with value = 2, prints B2, and calls the methodA(1)

methodA is called with value = 1, prints A1, and stops because the value is not greater than 1 anymore.

User Valeh Mikayilzadeh
by
5.7k points