Question

24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) ( int result 0: if (a <b) ( else if (a b) else ( return result: result mystery2 (a) mystery2 (a)i result - mystery2 (b) result-ab; public static int mystery2 (int x) f int countx for (int i 0; іск; i++) count +1: return counti What are the values stored in the variable result after the following method calls? a) int result mysteryl(4,1): b) int result mysteryl (2,2) c) int result = mystery! (4,6); 16
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER : I have run your code nd output is given below.

HEre in the when we are passing 4,1 in mystery1() then it is going in the else if condition because a>b in mystery2() it is passing 1 and in the mystery2() count is first initialized with 1 and it is incremented also 1 and returning count so output it is giving as 2.

1 public class ArrDemo { 2e public static int mystery1 (int a,int b) int result if(a<b) { -0; 4 result-mystery2(a) mystery2(a) result - mystery2(b); result-ab; else if(a>b) \ 6 7 8 9 10 else return result; 12 13 14e 15 16 public static int mystery2 (int x) int count =x; for(int i-0;i<x;i++) count+-1; return count; 18 19 20 public static void main(String[] args) 21 int result-mystery1 (4,1); System.out.println(result: +result); 23 24 25 Console 3 sterminateds ArrDemo [Java Application] CAProgram FilesJava jre1.8.0144 binjavaw.exe (07-Dec-2018, 9: esult:2

Here we Are passing the 2,2 in mystery1() so in this function our condition did not match so it will go in to the else part and result would store 2*2=4 and gives the output.

Here we are passing the 4,6 in mystery1() so our first condition becomes true and goes in to the mystery2() method and in this method we are passing the 4 and count will have 4 initially and added in the for loop as 4+4 means it will return 8 for first mystry2() method and second time also it will return 8 so result will be 8+8= 16

Add a comment
Know the answer?
Add Answer to:
24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) (...
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
Active Questions
ADVERTISEMENT