Skip to content

Latest commit

 

History

History
47 lines (40 loc) · 1.03 KB

quiz.md

File metadata and controls

47 lines (40 loc) · 1.03 KB

CSE 143

Quiz

Week 5

  1. Recursive Tracing

    public static String mystery (String s, char c) {
    	if (s.length() == 0) {
    		return s;		
    	} else if (s.charAt(s.length() - 1) == c) {
    		return c + mystery(s.substring(0, s.length() - 1), c);
    		
    	} else {
    		int len =  s.length() - 1;
    		return mystery(s.substring(0, len), c) + s.charAt(len);
    	}
    }
    Call Output
    mystery("sce", 'c');
    mystery("static", 't');
    mystery("banana", 'a');
    mystery("java", 'j');
    mystery("ALL", 'L');
  2. Write what the method returns, given the specified inputs. If the method will enter an infinite recursion, say "infinite recursion".

    public static String mystery(int x, int y) {
        if(x == y)
            return "!";
        if(x == y + 1)
            return "**";
        return mystery(x + 1, y) + mystery(x + 2, y);
    }
    Call Output
    mystery(6,5);
    mystery(-5,-4)
    mystery(3,1)
    mystery(4,6)
    mystery(3,6)