Question

For each call to the following method indicate what console output is produced public void mysteryXY...

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(5,2)
mysteryXY(10,1)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

First call:

For the call mysteryXY(5,2)

First, mysteryXY is called with x=5 and y=2

As y is not equal to 1 , else part is executed:

First line of else block is:

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

which prints 10 ,

Then second line of else block makes a call mysteryXY(5, 1)

As y=1 in this call, This call goes to if block and prints 5 , and control goes out of the function and back to the original call where x=5 and y=2

Then third line of else block is executed which prints , 10

Output:

Second call:

In the call mysteryXY(10,1) x=10 and y=1 , so it goes to the if block only and simply prints x

So,

10

output:

Hope you like it!

Please provide comments if you have any doubts!

Add a comment
Know the answer?
Add Answer to:
For each call to the following method indicate what console output is produced public void mysteryXY...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT