LearnFrenzy
Sign in Take a free test

Lambda Expressions · Exercise 2 of 4

Lambda Expressions Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 1 Easy

What is the output of the following Java program?

Main.java
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> doubleIt = x -> x * 2;
        Function<Integer, String> stars = n -> "*".repeat(n);
        Function<String, Integer> len = String::length;
        System.out.println(doubleIt.apply(21) + " " + stars.apply(3) + " " + len.apply(stars.apply(doubleIt.apply(2))));
    }
}
A 42 *** 4
B 42 *** 2
C 21 *** 4
D 42 3 4
Question page & discussion
Question 2 Medium

What is the output of the following Java program?

Main.java
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> plus3 = x -> x + 3;
        Function<Integer, Integer> times2 = x -> x * 2;
        System.out.println(plus3.andThen(times2).apply(5) + " " + plus3.compose(times2).apply(5) + " " + times2.andThen(times2).andThen(plus3).apply(1));
    }
}
A 16 13 7
B 13 16 7
C 16 16 7
D 13 13 5
Question page & discussion
Question 3 Medium

What is the output of the following Java program?

Main.java
import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        Predicate<Integer> even = n -> n % 2 == 0;
        Predicate<Integer> big = n -> n > 50;
        Predicate<Integer> p = even.and(big).or(n -> n == 7);
        StringBuilder sb = new StringBuilder();
        for (int n : new int[]{4, 7, 60, 71, 100}) sb.append(p.test(n) ? "Y" : "n");
        System.out.println(sb + " " + even.negate().test(9) + " " + big.and(even.negate()).test(71));
    }
}
A nYYnY true true
B nYYYY true true
C YYYnY true false
D nYYnY false true
Question page & discussion
Question 4 Medium

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class Main {
    public static void main(String[] args) {
        List<Supplier<String>> tasks = new ArrayList<>();
        for (String name : List.of("build", "test", "deploy")) {
            tasks.add(() -> name.toUpperCase() + "(" + name.length() + ")");
        }
        StringBuilder sb = new StringBuilder();
        for (Supplier<String> t : tasks) sb.append(t.get()).append(' ');
        System.out.println(sb.toString().trim());
    }
}
A BUILD(5) TEST(4) DEPLOY(6)
B DEPLOY(6) DEPLOY(6) DEPLOY(6)
C BUILD(5) BUILD(5) BUILD(5)
D Compilation error
Question page & discussion
Question 5 Medium

What is the output of the following Java program?

Main.java
import java.util.List;

public class Main {
    private int total = 0;
    private static int calls = 0;

    void sum(List<Integer> nums) {
        nums.forEach(n -> { total += n; calls++; });
    }

    public static void main(String[] args) {
        Main a = new Main(), b = new Main();
        a.sum(List.of(1, 2, 3));
        b.sum(List.of(10));
        a.sum(List.of(4));
        System.out.println(a.total + " " + b.total + " " + calls);
    }
}
A 10 10 5
B 6 10 4
C 10 10 4
D Compilation error
Question page & discussion
Question 6 Medium

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        String site = "LearnFrenzy";
        Predicate<String> inSite = site::contains;                    // bound to 'site'
        BiFunction<String, String, Boolean> contains = String::contains;   // first arg is the receiver
        Function<String, Integer> idx = site::indexOf;
        System.out.println(inSite.test("Fren") + " " + contains.apply("abc", "bc") + " " + contains.apply("bc", "abc") + " " + idx.apply("n"));
    }
}
A true true false 4
B true true true 4
C true false false 5
D false true false 4
Question page & discussion
Question 7 Medium

What is the output of the following Java program?

Main.java
import java.util.function.Supplier;

public class Main {
    static String expensive(String tag) { System.out.print("[computing " + tag + "] "); return tag.toUpperCase(); }

    static String orDefault(String value, Supplier<String> fallback) { return value != null ? value : fallback.get(); }

    public static void main(String[] args) {
        String a = orDefault("given", () -> expensive("a"));
        String b = orDefault(null, () -> expensive("b"));
        System.out.println(a + " " + b);
    }
}
A [computing b] given B
B [computing a] [computing b] given B
C given B
D [computing a] given B
Question page & discussion
Question 8 Medium

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>(List.of("pear", "Fig", "apple", "kiwi", "Date"));
        words.sort((a, b) -> a.compareToIgnoreCase(b));
        String first = String.join(",", words);
        words.sort(Comparator.comparing(String::length).thenComparing(Comparator.reverseOrder()));
        System.out.println(first + " | " + String.join(",", words));
    }
}
A apple,Date,Fig,kiwi,pear | Fig,pear,kiwi,Date,apple
B Date,Fig,apple,kiwi,pear | Fig,Date,kiwi,pear,apple
C apple,Date,Fig,kiwi,pear | Fig,Date,kiwi,pear,apple
D apple,Date,Fig,kiwi,pear | apple,Date,kiwi,pear,Fig
Question page & discussion
Question 9 Easy

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(5, 12, 8, 3, 20, 7));
        nums.removeIf(n -> n % 2 != 0);
        nums.replaceAll(n -> n / 4);
        int[] sum = {0};
        nums.forEach(n -> sum[0] += n);
        System.out.println(nums + " " + sum[0]);
    }
}
A [3, 2, 5] 10
B [12, 8, 20] 40
C [1, 3, 2, 0, 5, 1] 12
D [3, 2, 5] 0
Question page & discussion
Question 10 Hard

What is the output of the following Java program?

Main.java
import java.util.function.Supplier;

public class Main {
    private final String id = "outer";

    Supplier<String> lambda() { return () -> this.id; }

    Supplier<String> anonymous() {
        return new Supplier<String>() {
            private final String id = "inner";
            @Override public String get() { return this.id + "/" + Main.this.id; }
        };
    }

    public static void main(String[] args) {
        Main m = new Main();
        System.out.println(m.lambda().get() + " " + m.anonymous().get());
    }
}
A outer inner/outer
B outer outer/outer
C inner inner/outer
D Compilation error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links