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);
return result;
}Which of the following integers best approximates the value of f(2,3)?
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: C. 2
Explanation
f(2, 3) = 2/3 + 4/9 + 8/27 + … = (2/3)/(1 − 2/3) = 2. Recursion stops when m or n overflows to a negative value, by which point the remaining terms are negligible.
More Programming Languages and Computer Graphics questions
- What does the following Java function perform? (Assume int occupies four bytes of storage) public static int f( int a) { // Pre-conditions…
- Which of the following HTML5 codes will affect the horizontal as well as vertical alignment of the table content?
- Consider the C/C++ function f() given below: void f(char w[]) { int x = strlen(w); // length of the string char c; for (int i = 0; i < x; i…
- The definitions in an XML document are said to be __________ when the tagging system and definitions in the DTD are all in compliance.
- Consider the JavaScript code: var y = "12"; function f() { var y = "6"; alert(this.y); function g() { alert(y); } g(); } f(); If M is the n…
- What is the output of the following Java program? class simple { public static void main(String[] args) { simple obj = new simple(); obj.st…