← Quiz: UML Class Diagrams: all 10 questions
Quiz: UML Class Diagrams · Question 9 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
static class Order {
private final List<String> items = new ArrayList<>(); // composition: 1..* items
Order(String first) { items.add(first); }
void add(String item) { items.add(item); }
int count() { return items.size(); }
}
public static void main(String[] args) {
Order o = new Order("Pen");
o.add("Notebook");
System.out.println(o.count());
}
}
- A.1
- B.2
- C.0
- D.Compilation error
Answer & explanation
Correct answer: 2
The constructor guarantees at least one item (1..*), and one more is added.
Can you get the rest right without peeking?
Take the timed Quiz: UML Class Diagrams quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →