...
Compiler-Design
October 20, 2023
Programming
October 20, 2023
Compiler-Design
October 20, 2023
Programming
October 20, 2023

Programming

Question 284
Consider the following recursive Java function f that takes two long arguments and returns a float value :
public static float f(long m, long n)
{
float result = (float) m / (float) n;
if (m < 0 || n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
Which of the following integers best approximates the value of f(2,3) ?
A
0
B
3
C
1
D
2
Question 284 Explanation: 
public static float f(long m, long n)
{
float result = (float) m / (float) n;
if (m < 0 || n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
function call→ f(2,3)
→ result=2.0/3.0
=0.666
if condition is false, else will be executed result = 0.666+f(4,9)
function call→ f(4,9)
→ result=4.0/9.0
=0.444
result=0.4444+f(8,27)
function call→ f(8,27)
→ result=8.0/27.0
=0.2962
result=0.2962+f(16,54)
function call→ f(16,81)
→ result=16.0/81.0
=0.1975
result=0.1975+f(32,243)
function call→ f(32,243)
→ result= 32.0/243.0
= 0.13168
result=0.13168+f(64,729)
and so on
f(128,2187),f(256,6561),f(512,19683),f(1024,59049),f(2048,177147)
result=0.6666+0.4444+0.2962+0.1975+0.13168+0.08779+0.0390+0.026+0.0173+0.0115
The best approximate value is sum of all the function call result values which is equal 2.
Correct Answer: D
Question 284 Explanation: 
public static float f(long m, long n)
{
float result = (float) m / (float) n;
if (m < 0 || n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
function call→ f(2,3)
→ result=2.0/3.0
=0.666
if condition is false, else will be executed result = 0.666+f(4,9)
function call→ f(4,9)
→ result=4.0/9.0
=0.444
result=0.4444+f(8,27)
function call→ f(8,27)
→ result=8.0/27.0
=0.2962
result=0.2962+f(16,54)
function call→ f(16,81)
→ result=16.0/81.0
=0.1975
result=0.1975+f(32,243)
function call→ f(32,243)
→ result= 32.0/243.0
= 0.13168
result=0.13168+f(64,729)
and so on
f(128,2187),f(256,6561),f(512,19683),f(1024,59049),f(2048,177147)
result=0.6666+0.4444+0.2962+0.1975+0.13168+0.08779+0.0390+0.026+0.0173+0.0115
The best approximate value is sum of all the function call result values which is equal 2.
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
error: Alert: Content selection is disabled!!