For each call to the following method, indicate what console output is produced:public void mysteryXY(int x, int y) { if (y == 1) { System.o

Question

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);

in progress 0
Evelyn 1 month 2021-10-21T18:03:16+00:00 1 Answer 0 views 0

Answers ( )

    0
    2021-10-21T18:04:42+00:00

    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

Leave an answer

45:7+7-4:2-5:5*4+35:2 =? ( )