Considering the following code segment, answer the multiple choice questions.
Which line(s) indicate the base case of the recursion?
|
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 "";}
}
There are two lines of code that indicates the base case and they are:
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:
else if (s.length() % 2 == 0) {return "*" + bar(s.substring(2, s.length()));}
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.
Get Answers For Free
Most questions answered within 1 hours.