🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Composition vs Inheritance: all 10 questions

Quiz: Composition vs Inheritance · Question 8 of 10

What does this print? (code)

What does this print?

import java.util.*;

public class Main {
    interface Skill { int points(); }

    public static void main(String[] args) {
        List<Skill> skills = new ArrayList<>();
        skills.add(() -> 10);
        skills.add(() -> 5);
        int total = 0;
        for (Skill s : skills) total += s.points();
        skills.add(() -> 20);
        System.out.println(total + " " + skills.size());
    }
}
  1. A.35 3
  2. B.15 2
  3. C.15 3
  4. D.35 2
Answer & explanation

Correct answer: 15 3

The total is computed before the third skill is added, but the list then holds 3 skills.

Can you get the rest right without peeking?

Take the timed Quiz: Composition vs Inheritance quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →