← Quiz: Creational Patterns in Java: all 10 questions
Quiz: Creational Patterns in Java · Question 7 of 10
What does this print? (code)
What does this print?
interface Shape { String name(); }
class Circle implements Shape { public String name() { return "circle"; } }
class Square implements Shape { public String name() { return "square"; } }
public class Main {
static Shape create(int sides) {
return sides == 0 ? new Circle() : new Square();
}
public static void main(String[] args) {
System.out.println(create(4).name() + "-" + create(0).name());
}
}
- A.circle-square
- B.square-circle
- C.square-square
- D.Shape-Shape
Answer & explanation
Correct answer: square-circle
create(4) returns a Square and create(0) returns a Circle.
Can you get the rest right without peeking?
Take the timed Quiz: Creational Patterns in Java quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →