Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. A bear is an animal and a zoo contains many animals, including bears. Three c

ID: 3718312 • Letter: 1

Question

1. A bear is an animal and a zoo contains many animals, including bears. Three classes Animal, Bear, and Zoo are declared to represent animal, bear and zoo objects. Which of the following is the most appropriate set of declarations?

Question 1 options:

public class Animal extends Bear
{
...
}
public class Zoo
{
   private Animal[] myAnimals;
...
}

public class Animal extends Zoo
{
     private Bear myBear;
     ...
}

public class Bear extends Animal, Zoo
{
     ...
}

public class Bear extends Animal implements Zoo
{
   ...
}

public class Bear extends Animal
{
...
}
public class Zoo
{
   private Animal[] myAnimals;
...
}

2. Consider the following static method.

private static void recur(int n)
{
    if (n != 0)
    {
         recur (n - 2);
         System.out.println (n + " " );
    }
}

What numbers will be printed as a result of the call recur(7)?

Question 2 options:

No numbers will be printed because of infinite recursion

7 5 3 1

Many numbers will be printed because of infinite recursion.

-1 1 3 5 7

1 3 5 7

3. Assume that class Vehicle contains the following method.

public void setPrice(double price)
{   /* implementation not shown */   }

Also assume that class Car extends Vehicle and contains the following method.

public void setPrice(double price)
{ /*implementation not shown */}

Assume Vehicle v is initialized as follows.

Vehicle v = new Car();
v.setPrice(1000.0);

Which of the following is true?

Question 3 options:

The code above will cause a compile-time error because of type mismatch.

The code above will cause a run-time error, because a subclass cannot have a method with teh same name and the same signature as its superclass.

The code above will cause a compile-time error, because a subclass cannot have a method with the same name and the same signature as its superclass.

The code v.setPrice(1000.0); will cause the setPrice method of the Car class to be called.

The code v.setPrice(1000.0); will cause the setPrice method of the Vehicle class to be called.

public class Animal extends Bear
{
...
}
public class Zoo
{
   private Animal[] myAnimals;
...
}

public class Animal extends Zoo
{
     private Bear myBear;
     ...
}

public class Bear extends Animal, Zoo
{
     ...
}

public class Bear extends Animal implements Zoo
{
   ...
}

public class Bear extends Animal
{
...
}
public class Zoo
{
   private Animal[] myAnimals;
...
}

Explanation / Answer

1)

Ans)

public class Bear extends Animal
{
...
}
public class Zoo
{
   private Animal[] myAnimals;
...
}

________________

2)

Ans) No numbers will be printed because of infinite recursion

________________

3)

Ans)

The code v.setPrice(1000.0); will cause the setPrice method of the Car class to be called.

_______Thank You

The code v.setPrice(1000.0); will cause the setPrice method of the Car class to be called.