Question
Can someone help with these multiple choice questions? Thank you! 9. A palindrome is a word or phrase that reads the same forward or backward.
Can someone help with these multiple choice questions? Thank you!
9. A palindrome is a word or phrase that reads the same forward or backward. What is the function of the palindrome method?
public boolean palindrome(String string)
{
return isPal(string, 0, string.length()-1);
}
private boolean isPal(String string, int left, int right)
{
if (left >= right)
return true;
else
if (string.charAt(left) == string.charAt(right))
return isPal(string, left + 1, right - 1);
else
return false;
}
A) return the palindrome to the calling method
B) provide the string, along with its first and last indexes to the recursive isPal method
C) send the recursive isPal method its terminating condition
D) recursively call itself
10. Consider the recursive square method. It takes a non-negative int parameter. Then it recursively adds the number n to itself n times to produce the square of n. Select the correct code for the square helper method.
public int square(int n)
{
____________________ ;
}
public int square(int c, int n)
{
if (c == 1)
return n;
else
return n + square(c-1, n);
}
A) return square(n,n)
B) return square(n)
C) return square(n-1, n)
D) return square(n, n-1)
11. Why does the best recursive method usually run slightly slower than its iterative counterpart ?
A) testing the terminating condition takes longer
B) each recursion method call takes processor time
C) multiple recursive cases must be considered
D) checking multiple terminating conditions take more processor time
12. Consider the fib method from the book. Computing the 7th fibonacci number, fib(7), recursively computes fib(6), fib(5), and fib(4) ___ times respectively.
public static long fib(int n)
{
if (n <= 2) return 1;
else return fib(n 1) + fib(n 2);
}
A) 1,2,3
B) 6,5,4
C) 4,5,6
D) 3,2,1
13. A terminating condition for a recursive method that finds the middle character of a String with any number of characters could be due to which of the following condition(s):
I the length of the String is 1
II first and last String characters match
III the String is not empty
A) I
B) II
C) I, II and III
D) I and III
14. Which statements are true?
I Recursion is faster then iteration
II Recursion is often easier to understand than iteration
III Recursive design has an economy of thought
A) I
B) I and II
C) II and III
D) I and III
15. Consider the method powerOfTwo. How many recursive calls are made from the original call of powerOfTwo(63) (not including the original call)?
public boolean powerOfTwo(int n)
{
if (n == 1) // line #1
return true;
else
if (n % 2 == 1) // line #2
return false;
else
return powerOfTwo(n/2); // line #3
}
A) 6
B) 4
C) 1
D) 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started