Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
hard Solved by 1 students

Recursion and Thread Scheduling

Mark
< Prev
Next >

Consider the following OutOfOrderRecursion class. Which statement best describes the possible output when the main method is executed?

public class OutOfOrderRecursion {
    public static void recursivePrint(int n) {
        if(n <= 0){
            System.out.println("Base case reached");
            return;
        }
        new Thread(() -> recursivePrint(n - 1)).start();
        System.out.println("Printing " + n);
    }
    
    public static void main(String[] args) throws InterruptedException {
        recursivePrint(3);
        Thread.sleep(1000);
    }
}
A

The program will always result in a deadlock, so no output is produced.

B

The output could be non-deterministic due to thread scheduling, with ‘Printing’ statements interleaved with the ‘Base case reached’ message.

C

The output will always display numbers in descending order followed by the base case message.

D

The base case message will always be printed first, followed by numbers in ascending order.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|