LearnFrenzy
Sign in Take a free test

Lambda Expressions · Predict the Output · Question 20

var and explicit types in lambda parameters Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        BiFunction<String, Integer, String> a = (s, n) -> s.repeat(n);
        BiFunction<String, Integer, String> b = (var s, var n) -> s.substring(0, n);
        BiFunction<String, Integer, String> c = (String s, Integer n) -> s.charAt(n) + "";
        System.out.println(a.apply("ab", 2) + " " + b.apply("lambda", 3) + " " + c.apply("java", 1));
    }
}
  • A abab lam a Correct
  • B ab2 lam a
  • C abab lamb j
  • D Compilation error
Correct Answer: Option A

Implicit, var and explicit parameter types are all allowed – as long as one lambda does not mix them.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links