🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
    }
}
  1. A.circle-square
  2. B.square-circle
  3. C.square-square
  4. 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 →