Read the code below. What do you think will happen here?
import java.util.function.Supplier;
import java.util.stream.Stream;
public class Question2 {
public static void main(String[] args) {
Supplier<Integer> randomSupplier = () -> (int) (Math.random() * 3999) + 1;
Stream.generate(randomSupplier).map(Question2::convertToRoman).forEach(System.out::println);
}
public static String convertToRoman(int number) {
String[] thousands = { "", "M", "MM", "MMM" };
String[] hundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
String[] tens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
String[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
return thousands[number / 1000] +
hundreds[(number % 1000) / 100] +
tens[(number % 100) / 10] +
units[number % 10];
}
}
- A) Java will complain that it’s an infinite stream and will not run
- B) It’ll run perfectly normally in an infinite loop
- C) The stream will be closed after the first run, and an IllegalAccessException will be raised
- D) Nothing will be printed
- E) After 42 minutes, it will stop running