Lambdas created inside a loop
Predict the outputMediumJava only
import java.util.*;
import java.util.function.*;
public class Main {
public static void main(String[] args) {
List<Supplier<Integer>> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int j = i;
list.add(() -> j * 10);
}
for (Supplier<Integer> s : list) {
System.out.print(s.get() + " ");
}
System.out.println();
}
}
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.