Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote