Recursion+Practice

Recursion Practice Problems

public void mystery (int x) { if (x <= 0) return; else { System.out.print( x + " "); mystery( x - 2); } }

1) What does mystery(4) print?

public void mystery (int x) { if (x <= 0) return; else { mystery (x - 2); System.out.print(x + " ");} }

2) What does mystery(4) print?

public int mystery (int x) { if (x <= 0) return 1; else return x * mystery(x- 1); }

3) What does mystery(4) return?

public int mystery (int x) { if (x == 0) return 0; else return ((x % 10) + mystery(x/10)); }

4) What does mystery(3543) return?

public int mystery(int x) { if (x == 1) return 2; else return 2 * mystery(x - 1); }

5) What does mystery(6) return?

public int mystery(int x) { if (x == 0) return 0; else return x + mystery( x/2) + mystery ( x/4); }

6) What does mystery(10) return?

7) Which question(s) has a potential endless loop error?

ANSWERS