
Gr H.
asked 01/20/22what is the answer to the question in the description
Given the following class:
import java.util.ArrayList;
public class RectangleTester
{
public static void main(String[ ] args)
{
ArrayList< Rectangle > shapes = new ArrayList< Rectangle >();
shapes.add(new Rectangle(1, 1));
shapes.add(new Rectangle(2, 2));
shapes.add(new Rectangle(3, 3));
shapes.add(new Rectangle(4, 4));
Rectangle dataRecord;
for(int index = 0; index < shapes.size(); index++)
{
dataRecord = shapes.get(index);
dataRecord.calcRectArea();
dataRecord.calcRectPerimeter();
System.out.println("Area = " + dataRecord.getArea());
System.out.println("Perimeter = " + dataRecord.getPerimeter());
}
}
}
Assume that getArea() returns the area of a rectangle and getPerimeter() returns the perimeter of a rectangle. What will print when index = 3?
a | Area = 9 | Perimeter = 12 |
b | Area = 4 | Perimeter = 8 |
c | Area = 16 | Perimeter = 16 |
d | Area = 25 | Perimeter = 20 |
e | Area = 1 | Perimeter = 4 |
1 Expert Answer

Luke L. answered 04/27/22
Senior FAANG Software Engineer Specializing in Java
Dan B. is correct. When `index` is 3, `shapes.get(3)` is called. Since lists in java have zero-indexed (meaning they start at index 0 instead of index 1), `shapes.get(3)` is actually getting the fourth item, which is `Rectangle(4, 4))`. The perimeter of a rectangle with all sides length 4 is 16, and the area of a rectangle with all sides length 4 is also 16. Therefore, C is the correct answer.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Dan B.
c. As index 3 refers to the 4th element, it will be referencing the Rectangle(4,4) so the area will be 4x4 and the perimeter will be 4+4+4+4 (which is the same thing01/20/22