Rectangle Factory
A Rectangle class provides two constructors, one for a square and one for a general rectangle.
What is printed when the following Java program is compiled and run?
class Rectangle {
private int w, h;
public Rectangle(int s) {
w = s;
h = s;
}
public Rectangle(int w, int h) {
this.w = w;
this.h = h;
}
public int area() {
return w * h;
}
}
public class QuestionTrace {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(5);
Rectangle r2 = new Rectangle(4, 6);
System.out.println(r1.area() + r2.area());
}
}
A
24
B
49
C
50
D
25
Question Leaderboard
Not enough data yet to show leaderboard.
