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...
Determine how all of your classes are related, and create a complete UML class diagram representing...
Determine how all of your classes are related, and create a complete UML class diagram representing your class structure. Don't forget to include the appropriate relationships between the classes. GameDriver import java.util.Scanner; public class GameDriver { Scanner in = new Scanner(System.in); public static void main(String[] args) { Move move1 = new Move("Vine Whip", "Grass", 45, 1.0f); Move move2 = new Move("Tackle", "Normal", 50, 1.0f); Move move3 = new Move("Take Down", "Normal", 90, 0.85f); Move move4 = new Move("Razor Leaf", "Grass",...
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 -...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
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...
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...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT