For each call to the following method, indicate what console output is produced:public void mysteryXY(int x, int y) { if (y == 1) { System.out.print(x); } else { System.out.print(x * y + ", "); mysteryXY(x, y - 1); System.out.print(", " + x * y); }} mysteryXY(4, 1); mysteryXY(4, 2); mysteryXY(8, 2); mysteryXY(4, 3); mysteryXY(3, 4);

Respuesta :

Answer:

1. mysteryXY(4, 1); = 4

2. mysteryXY(4, 2); = 8, ,4

3. mysteryXY(8, 2); = 16, , 8

4. mysteryXY(4, 3); = 12, , 8

5. mysteryXY(3, 4); = 12, , 9

Step-by-step explanation:

public void mysteryXY(int x, int y) {

if (y == 1) {

System.out.print(x);

}

else

{

System.out.print(x * y + ", ");

mysteryXY(x, y - 1);

System.out.print(", " + x * y); }

}

mysteryXY(4, 1); = 4

On line 2, the value of Y is tested;

Y = 1. So the operation on line 3 will be executed.

The values of X will be printed

X= 1

For question 2 through 5, the value of Y is not 1, so it'll skip line and jump to 6.

The statement on line 6 print x * y appended with a comma

On line 7, the values of y is reduced by 1

On line 8, it prints , and the result of x * y.

So, we have

2. mysteryXY(4, 2); = 8, ,4

4 * 2 = 8

Reduce y by 1

Then, 4 * 1 = 4

Output: 8, , 4

Applying the same logic to 3 to 5

3. mysteryXY(8, 2); =

Output: 16, , 8

4. mysteryXY(4, 3); =

Output: 12, , 8

5. mysteryXY(3, 4); =

Output: 12, , 9