Instructions For the following questions, determine the value returned as a resu
ID: 3679926 • Letter: I
Question
Instructions
For the following questions, determine the value returned as a result of the given method call.
1-public static int quest (int x, int y)
if (x < y)
return x;
else
return y + quest (x – 1, y + 1);
guest(4, 3);
2-public static in quest(int a)
if (a <= 1)
return 1;
if (a % 2 == 0){
return a – quest (a – 1);
else
return a + quest (a – 1);
}
guest(7);
Answer:
3- public static int quest (int x, int y)
int rem = x % y;
if (rem == 0)
return y;
else
{
x = y;
y = rem;
return quest(x, y);
quest(330, 210);
Explanation / Answer
1.
quest (int x, int y)
it will take two values for x and y
and it will check
the condition (x < y)
if value of x is greater than x then it will return x
or else
it will retrun y+(x-1,y+1)
for ex quest(4,3)
if(4<3)
it will go to else
it reurns 3+(3,4)
and this 3,4 will go to method again and it checks
if(3<4) yes
so it returns 3 so
the final
quest(4,3) returns =3+(3,4)
=3+(3) =6
ans=6
2.
public static in quest(int a)
if (a <= 1)
return 1;
if (a % 2 == 0){
return a – quest (a – 1);
else
return a + quest (a – 1);
}
quest(7) is the value so it will replace 7 in a
if(7<=1)
false
if(7%2==0)]
false
else return 7+quest(6)
7+quest(6)
quest(6)
if(6%2==0)
return 6-quest(5)
final = 7+6-quest(5)
quest(5)
return 5+quest(4)
final = 7+6-(5+quest(4))
= 13-5-quest(4)
= 8-quest(4)
= 8-(return 4 - quest(3))
= 8-4+quest(3)
= 4+(return 3 + quest(2))
= 4+3+quest(2)
= 7+(return 2 + quest(1))
= 7+2 + quest(1)
= 9+(return 1)(as if (1<=1))
= 9+1 =10
so final answer is 10
3.
public static int quest (int x, int y)
int rem = x % y;
if (rem == 0)
return y;
else
{
x = y;
y = rem;
return quest(x, y);
}
quest(330,210)
final = quest(330,210)
= rem=120
= x=210 and y=120
= quest(210,120)
= rem=90
= x=120 and y=90
= quest(120,90)
= rem=30
= x=90 and y=30
= quest(90,30)
= rem=0
= retuyn 30
so answer is 30;