Ask Question
8 June, 12:25

Consider the recursive method whose definition appear below. Why? public static String mysteryString (String s) { if (s. length () = =1) return s; else return s. substring (s. length () - 1) + mysteryString (s. substring (0, s. length () - 1)); } What is the result of the following call?

System. out. println (mysteryString ("computer"));

+5
Answers (1)
  1. 8 June, 14:52
    0
    retupmoc

    Explanation:

    1.) Anwser will be retupmoc

    because

    public static String mysteryString (String s) {

    if (s. length () = = 1) {

    return s;

    }

    else{

    return s. substring (s. length () - 1) + mysteryString (s. substring (0, s. length () - 1));

    }

    }

    In this program input is "computer". So the function mysteryString (String s) it does

    return s. substring (s. length () - 1) + mysteryString (s. substring (0, s. length () - 1));

    so when it enters the first time? s. substring (s. length () - 1) and it will be give you 'r' then it calls the function recursively by reducing the string length by one. So next time it calls the mysteryString function with string "compute" and next time it calls return s. substring (s. length () - 1) ? + mysteryString (s. substring (0, s. length-1)) so this time it gives "e" and calls the function again recursively. It keeps on doing till it matched the base case.

    so it returns "retupmoc".
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the recursive method whose definition appear below. Why? public static String mysteryString (String s) { if (s. length () = =1) ...” 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