0
0
Javaprogramming~15 mins

Call stack behavior in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Call Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
What is the output of this recursive call stack?

Consider the following Java code that uses recursion. What will be printed when countdown(3) is called?

Java
public class Test {
    public static void countdown(int n) {
        if (n == 0) {
            System.out.println("Done");
            return;
        }
        System.out.println(n);
        countdown(n - 1);
    }

    public static void main(String[] args) {
        countdown(3);
    }
}
A
Done
1
2
3
B
3
2
1
Done
C
3
2
1
DDone
Attempts:
2 left
💻 code output
intermediate
2:00remaining
What is the value of variable after nested method calls?

Given the following Java code, what is the value of result after main finishes?

Java
public class Test {
    public static int addOne(int x) {
        return x + 1;
    }

    public static int doubleIt(int x) {
        return x * 2;
    }

    public static void main(String[] args) {
        int result = doubleIt(addOne(3));
        System.out.println(result);
    }
}
A8
B7
C6
D5
Attempts:
2 left
💻 code output
advanced
2:30remaining
What is printed by this mutual recursion example?

Examine the following Java code with two mutually recursive methods. What is the output when foo(2) is called?

Java
public class Test {
    public static void foo(int n) {
        System.out.println("foo " + n);
        if (n == 0) {
            System.out.println("foo done");
            return;
        }
        bar(n - 1);
    }

    public static void bar(int n) {
        if (n == 0) {
            System.out.println("bar done");
            return;
        }
        System.out.println("bar " + n);
        foo(n / 2);
    }

    public static void main(String[] args) {
        foo(2);
    }
}
A
foo 2
bar 1
foo 0
bar done
foo done
B
foo 2
bar 1
foo 1
bar 0
bar done
foo done
C
foo 2
bar 1
foo 0
foo done
D
foo 2
bar 1
foo 1
bar 0
foo done
bar done
Attempts:
2 left
🔧 debug
advanced
1:30remaining
What error does this recursive method cause?

Consider this Java method that calls itself without a base case. What error will occur when infiniteRecursion() is called?

Java
public class Test {
    public static void infiniteRecursion() {
        infiniteRecursion();
    }

    public static void main(String[] args) {
        infiniteRecursion();
    }
}
ANo error, program runs forever
BNullPointerException
CIllegalArgumentException
DStackOverflowError
Attempts:
2 left
🧠 conceptual
expert
2:00remaining
How many stack frames are created during this recursive factorial call?

Given the following factorial method, how many stack frames are created when factorial(5) is called?

Java
public class Test {
    public static int factorial(int n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        int result = factorial(5);
        System.out.println(result);
    }
}
A6
B5
C4
D1
Attempts:
2 left