Ask Question
30 May, 19:34

Consider the following definition of a recursive method. public static int mystery (int[] list, int first, int last) { if (first = = last) return list[first]; else return list[first] + mystery (list, first + 1, last); } Given the declaration int[] alpha = {1, 4, 5, 8, 9}; What is the output of the following statement? System. out. println (mystery (alpha, 0, 4)); a. 1 b. 18 c. 27 d. 32

+1
Answers (1)
  1. 30 May, 23:30
    0
    c. 27

    Explanation:

    In recursion method, a method calls itself until a condition becomes true and returned back. In mystery (), at first time, first is 0 and last is 4. Condition is checked and 0!=4 so else part is executed in which again mystery is called with first as 1 and last 4. This will go again and again until first=4. When first=4 it is also equal to last so if part is executed and alpha[4] is returned which is 9, now it comes alpha[3] + 9 which is 8+9 = 17. It is returning values to its previous calls that is why it will reduce to alpha[0]+alpha[1]+alpha[2]+17 which is nothing but sum of all elements of a alpha Then, 1+4+5+8+9=27.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the following definition of a recursive method. public static int mystery (int[] list, int first, int last) { if (first = = last) ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers