logo Practice-It logo

mystery10

Language/Type: Java recursion recursive tracing
Author: Stuart Reges (on 2021/04/02)

Consider the following method:

        public static void mystery10(List data) {
            if (data.isEmpty()) {
                data.add(0);
            } else {
                int n = data.remove(0);  // returns value removed
                mystery10(data);
                if (n < 0) {
                    data.add(-n);
                } else if (n > 0) {
                    data.add(n);
                }
            }
        }

Assume that a variable called list of type List has been declared and that we make the call mystery10(list). For each initial value of list in the table below, indicate what would be stored in the list after the call.

[1, 2, 3]
[-1, -2, -3]
[5, 0, 0, 4, -3, -2, 7, 0]
[2, 4, -6, -8, 0, 10, 0, -12]
[0, 3, -2, 4, 0, 15]

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.