In Java Language: 1. Write a recursive method to print every other element in an
ID: 3698093 • Letter: I
Question
In Java Language:
1.
Write a recursive method to print every other element in an array, starting with the element in position 0.
The method header is: public void printEveryOther(int[] nums)
2.
What condition defines the base case for this method?
Select one:
a.(x <= 1)
b.(x = = 0)
c.(x > 1)
d.(x <= 0)
e.(x = = 1)
public int factorial(int x) {
if (x > 1)
return x * factorial (x – 1);
else
return 1;
}
3.
The following method should return true if the int parameter is even and either positive or 0, and false otherwise.
Which set of code should you use to replace … so that the method works appropriately?
public boolean evenPosZero(int x) {
if(x<0) return false;
… // ??? what goes here
}
a.else if (x = = 0) return false;
else return evenPosZero(x – 1);
b.else if (x = = 0) return false;
else return evenPosZero(x – 2);
c.else if (x = = 0) return true;
else return evenPosZero(x – 2);
d.else return(x = = 0);
e.else if (x = = 0) return true;
else return evenPosZero(x – 1);
4.
The current method executing is always the method whose activation record is ________.
Select one:
a.at the top of the stack.
b.never placed on the stack.
c.second from the top of the stack, just below the previous method call.
d.at the bottom of the stack.
Thank you for helping!
Explanation / Answer
1.
public static void printEveryOther(int[] nums, int i)
{
System.out.print("[" + nums[i] + "]");
if (i == nums.length)
{
++i;
printEveryOther(nums, i);
}
}
2.
c.(x > 1)
3.
e.else if (x = = 0) return true;
else return evenPosZero(x – 2);
4.
a.at the top of the stack.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.