← 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());
}
}
- A.35 3
- B.15 2
- C.15 3
- 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 →