Question

Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case...

Considering the following code segment, answer the multiple choice questions.

Which line(s) indicate the base case of the recursion?

Which line(s) of code make recursion move toward the base case?


  1. public String foo(String input) {

  2. String str = input.toLowerCase();

  3. return bar(str);

  4. }

  5. private String bar(String s) {

  6. if (s.length() <= 1)   {return "";}

  7. else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));}

  8. else if (s.length() % 2 == 1) {return "*" + bar(s+"n");}

  9. else {return "";}

  10. }

Homework Answers

Answer #1

There are two lines of code that indicates the base case and they are:

  1. if (s.length() <= 1)   {return "";}
  2. else {return "";}

Both of these lines does not make any further recursive calls and thus causing the recursive procedure to arrive at a base case and then stop. A base case is a case in recursion where the problem can be solved without any further recursion.

There are two lines of code that causes the recursion to move to the base case and they are:

  1. else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));}

  2. else if (s.length() % 2 == 1) {return "*" + bar(s+"n");}

These lines of code makes further recursive calls and causes the recursion to move towards the base case. The subsequently reduces the size of the string on each recursive call and then causes the recursion to approach the base case.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} } Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Read the case and answer the following Multiple choice questions. There are 5 questions total, where...
Read the case and answer the following Multiple choice questions. There are 5 questions total, where some of them might have more than one correct answers. You can choose more than one options where you think is suitable for the above question. PERFORMANCE MANAGEMENT Project Manager Oliver Caine skimmed his notes as he waited for Ben Robins to come to the meeting room. He hoped Ben would arrive soon, as he wanted to get the con-versation finished quickly. Ben walked...
Multiple Choice Questions \Indicate the best answer in the space provided 1. The difference between regulations...
Multiple Choice Questions \Indicate the best answer in the space provided 1. The difference between regulations and revenue rulings is that _____ a. Revenue rulings are not limited to a given set of facts and regulations are limited b. Revenue rulings are the direct law-making powers of Congress and regulations are not c. Revenue rulings require approval by the Secretary of the Treasury; regulations do not d. Revenue rulings do not have the authority of regulations; regulations are a direct...
Outline and answer all discussion questions following case description in details. (Do not attempt to solve...
Outline and answer all discussion questions following case description in details. (Do not attempt to solve if you can not fulfill all the requirements!!!!) THE ENERGY BAR INDUSTRY In 1986, PowerBar, a firm in Berkeley, California, single-handedly created the energy bar category. Positioned as an athletic energy food, it was distributed at bike shops and events that usually involved running or biking. The target segment was the athlete who needed an efficient, effective energy source. Six years later, seeking to...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT