Instructions For the following questions, determine the value returned as a resu
ID: 3664268 • Letter: I
Question
Instructions
For the following questions, determine the value returned as a result of the given method call.
public static int quest (int x, int y)
(x < y)
x;
y + quest (x – 1, y + 1);
quest(4, 3);
Answer:
public static in quest(int a)
(a <= 1)
1;
(a % 2 == 0){
a – quest (a – 1);
a + quest (a – 1);
Answer:
public static int quest (int x, int y)
rem = x % y;
(rem == 0)
y;
x = y;
y = rem;
return quest(x, y);
quest(330, 210);
Answer:
Explanation / Answer
1)
public static int quest (int x, int y)
(x < y)
x;
y + quest (x – 1, y + 1);
quest(4, 3);
Answer: 6
Explaination:
In 1st pass: x=4 and y=3 , thus, x>y hence, else part will be executed.
=y + quest (x – 1, y + 1)
=3+quest(3,4)
After this recursive call, In 2nd pass: x=3, y=4 thus x<y hence if will be executed.
=3+quest(3,4)
=3+3=6
2)
public static in quest(int a)
(a <= 1)
1;
(a % 2 == 0){
a – quest (a – 1);
a + quest (a – 1);
Answer: let value of a=5
In first pass: we go through the first two conditions which are evaluated to be false as neither a=9<1 nor 9%2=0.
Thus, third part gets evaluated
=a + quest (a – 1)
=5+ quest (4)
Here function is recursively called. now a=4 thus second condition will be true.
=5+(4 – quest (4– 1))
=5+(4-quest(3) )
Again the function is recursively called. here a=3, third case is true.
=5+(4-(3+quest(2 )))
Again the function is recursively called. here a=2, seond case is true.
=5+(4-(3+(2-quest(1 ))))
now a=1 thus returns 1
final result =5+(4-(3+(2-1)))=5
3)
public static int quest (int x, int y)
rem = x % y;
(rem == 0)
y;
x = y;
y = rem;
return quest(x, y);
quest(330, 210);
Answer: 30
Explaination:
In 1st pass: x=330, y=210, then x % y is not equal to 0 thus, else part will be executed.
in else part we are assigning the value of y to x and value of remainder to y and calling the function recursively till the time when remainderf becomes =0.
At this time, we are returning the value of y which comes out to be 30 in above case.