In java language Question 9 The following method should return true if the int p
ID: 3697767 • Letter: I
Question
In java language
Question 9
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
}
Select one:
a. else if (x = = 0) return false;
else return evenPosZero(x – 1);
b. else if (x = = 0) return true;
else return evenPosZero(x – 2);
c. else if (x = = 0) return true;
else return evenPosZero(x – 1);
d. else return(x = = 0);
e. else if (x = = 0) return false;
else return evenPosZero(x – 2);
Question 10
What does the following recursive method determine if initially invoked with j=0?
public boolean whoKnows(int[ ]a, int[ ] b, int j) {
if (j = = a.length)
return false;
else if (j = = b.length)
return true;
else return
whoKnows(a, b, j+1);
}
Select one:
a. Returns true if the size of a is greater than the size of b, false otherwise
b. Returns the length of array a + length of array b
c. Returns true if a and b have no elements
d. Returns true if a and b are equal in size, false otherwise
e.Returns true if the size of b is greater than the size of a, false otherwise
Question 11
Why will the following method have infinite recursion?
public int infiniteRecursion(int n) {
if (n > 0)
return infiniteRecursion( n ) + 1;
else
return 0;
}
Select one:
a. Because there is no base case
b. Because the base case will never be true
c. Because the recursive call does not move the parameter closer to the base case
d. None of the above, there is no infinite recursion in this method
Question 12
What does the following method compute? Assume the method is called initially with i = 0
public int mysteryMethod(String a, char b, int i) {
if (i = = a.length( ))
return 0;
else if (b = = a.charAt(i))
return mysteryMethod(a, b, i+1) + 1;
else
return mysteryMethod(a, b, i+1);
}
Select one:
a, Returns 1 if char b appears in String a at least once, and 0 otherwise
b. The number of times char b appears in String a
c. The char which appears at location i in String a
d. The length of String a concatenated with char b
e. The length of String a
Question 13
The following method lacks a base case.
public int noBaseCase(int x) {
if (x > 0)
return noBaseCase(x – 1) + 1;
else
return noBaseCase(x – 2) + 2;
}
Select one:
a. True
b. False
Question 14
The following two methods will both compute the same thing when invoked with the same value of x. That is, method1(x) = = method2(x).
public int method1(int x) {
if (x > 0)
return method1(x – 1) + 1;
else
return 0;
}
public int method2(int x) {
if (x > 0)
return 1 + method2(x – 1);
else
return 0;
}
Select one:
a. True
b. False
Explanation / Answer
9.b
11.a
13.b
14.b
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.