What is the output from the following program segment? class A{ public void chee
ID: 3853403 • Letter: W
Question
What is the output from the following program segment?
class A{
public void cheers()
{
System.out.println(cheer(4));
}
String cheer(int n)
{
if (n == 1)
return "Hurray!!";
else if( n == 2)
return "Hip Yes " + cheer(n-1);
else
return "Hip " + cheer(n-1);
}
Select one:
a. Hip Hip Hip Hip Yes Hurray!!
b. Hurray!! Hip Yes Hip Hip Hip
c. Hurray!! Hip Hip Hip Hip Yes
d. Hip Hip Hip Yes Hip Hurray!!
e. Hurray!! Hip Yes Hip Hip Yes
Explanation / Answer
Answer: All are wrnong answers. Output should be "Hip Hip Hip Yes Hurray!!"
Because we are passing 4 value to cheer() method.
When n = 4, "Hip" will add to the return string.
When n = 3, "Hip Hip" will add to the return string.
When n = 2, "Hip Hip Hip Yes" will add to the return string.
When n = 1, "Hip Hip Hip Yes Hurray!!" will add to the return string.
Finally string "Hip Hip Hip Yes Hurray!" will return to the function called cheers() method and display the string.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.