Test: Computer Science

Consider the following code:

public static interface Shape2D {

     public double getArea();

}

 

public static class Ellipse implements Shape2D {

     private double r1,r2;

     public Ellipse(double r1,double r2) {

          this.r1 = r1;

          this.r2 = r2;

     }

     public double getArea() {

          return Math.PI * r1 * r2;

     }

}

public static class Circle extends Ellipse {

     private double radius;

     public Circle(double r) {

          super(r,r);

     }

     public double getArea() {

          return super.getArea();

     }

     public double getCircumference() {

          return Math.PI * 2 * radius;

     }

}

 

public static void main(String[] args) {

     Shape2D[] vals = {new Circle(5),new Ellipse(8,5)};

     for(int i = 0; i < vals.length; i++) {

          System.out.println("Area " + (i+1) + ": " + vals[i].getArea());

     }

}

1.

What is the output for this code?

Area 1: 78.53981633974483

Area 2: 125.66370614359172

Area 1: 78.53981633974483

Area 2: 78.53981633974483

Area 1: 31.41592653589793

Area 2: 201.061929829746752

Area 0: 78.53981633974483

Area 1: 201.061929829746752

The code will not compile.

1/3 questions

0%

Access results and powerful study features!

Take 15 seconds to create an account.
Start now! Create your free account and get access to features like:
  • Full length diagnostic tests
  • Invite your friends
  • Access hundreds of practice tests
  • Monitor your progress over time
  • Manage your tests and results
  • Monitor the progress of your class & students
By clicking Create Account you agree that you are at least 13 years old and you agree to the Varsity Tutors LLC Terms of Use and Privacy Policy.
Learning Tools by Varsity Tutors