← Quiz: SOLID in Java: all 10 questions
Quiz: SOLID in Java · Question 3 of 10
What does this print? (code)
What does this print?
interface Shape { double area(); }
class Rect implements Shape {
int w, h;
Rect(int w, int h) { this.w = w; this.h = h; }
public double area() { return w * h; }
}
class Circle implements Shape {
int r;
Circle(int r) { this.r = r; }
public double area() { return 3 * r * r; }
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Rect(2, 5), new Circle(2) };
double total = 0;
for (Shape s : shapes) total += s.area();
System.out.println(total);
}
}
- A.22
- B.22.0
- C.10.012.0
- D.Compilation error
Answer & explanation
Correct answer: 22.0
10.0 + 12.0 = 22.0; the total is a double, so Java prints 22.0. The loop works for any Shape — open/closed in action.
Can you get the rest right without peeking?
Take the timed Quiz: SOLID in Java quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →