Medium Multiple Choice QuestionsΒΆ

These problems are similar to those you will see on the AP CS A exam.

10-13-1: Given the following class declarations, what is the output from Student s1 = new GradStudent(); followed by s1.getInfo();?

public class Student {
   public String getFood() {
      return "Pizza";
   }
   public String getInfo()  {
      return this.getFood();
   }
}

public class GradStudent extends Student {
   public String getFood() {
      return "Taco";
   }
}






10-13-2: Given the following class declarations, and EnhancedItem enItemObj = new EnhancedItem(); in a client class, which of the following statements would compile?

public class Item
{
   private int x;

   public void setX(int theX)
   {
      x = theX;
   }
   // ... other methods not shown
}

public class EnhancedItem extends Item
{
   private int y;

   public void setY(int theY)
   {
      y = theY;
   }

  // ... other methods not shown
}

I. enItemObj.y = 32;
II. enItemObj.setY(32);
III. enItemObj.setX(52);






10-13-3: Given the following class declarations and initializations in a client program, which of the following is a correct call to method1?

public class Test1
{
   public void method1(Test2 v1, Test3 v2)
   {
      // rest of method not shown
   }
}

public class Test2 extends Test1
{
}

public class Test3 extends Test2
{
}

The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3();






10-13-4: If you have a parent class Animal that has a method speak() which returns: Awk. Cat has a speak method that returns: Meow. Bird does not have a speak method. Dog has a speak method that returns: Woof. Pig does not have a speak method. Cow has a speak method that returns: Moo. What is the output from looping through the array a created below and asking each element to speak()?

Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }






10-13-5: Given the following class declarations and code, what is the result when the code is run?

public class Car
{
   private int fuel;

   public Car() { fuel = 0; }
   public Car(int g) { fuel = g; }

   public void addFuel() { fuel++; }
   public void display() { System.out.print(fuel + " "); }
}

public class RaceCar extends Car
{
   public RaceCar(int g) { super(2*g); }
}

What is the result when the following code is compiled and run?

Car car = new Car(5);
Car fastCar = new RaceCar(5);
car.display()
car.addFuel();
car.display();
fastCar.display();
fastCar.addFuel();
fastCar.display();






10-13-6: Given the following class declarations and code, what is the result when the code is run?

public class Book
{
   public String getISBN()
   {
      // implementation not shown
   }

   // constructors, fields, and other methods not shown
}

public class Dictionary extends Book
{
   public String getDefinition(String word)
   {
      // implementation not shown
   }

   // constructors, fields, and methods not shown
}

Assume that the following declaration appears in a client class.

Book b = new Dictionary();

Which of the following statements would compile without error?
I.  b.getISBN();
II. b.getDefinition("wonderful");
III. ((Dictionary) b).getDefinition("wonderful");