47.8k views
5 votes
Which of the following answer choices best describes the algorithmic purpose of method num?

/** Precondition: i==0; */

public static int num(int[] ray, int i)
{
if( i >= ray.length ){
return 0;
}
if( ray[i] % 2 != 0 ){
return 1 + num( ray, i+1 );
}
else{
return 0 + num( ray, i+1 );
}
}

a. The method is counting the number of numbers in ray.
b. The method is counting the number of even numbers in ray.
c. The method is counting the number of occurrences of x in ray.
d. The method is counting the number of odd numbers in ray.
e. The method is summing all of the numbers in ray.

User John Lyon
by
5.1k points

1 Answer

3 votes

Answer:

d.

Step-by-step explanation:

It is a recursive function that returns the number of odd numbers in ray starting at index i and above.

The depth of the recursion is equal to the length of ray.

User Sebastian Zaha
by
4.7k points