The following questions are similar to what you would have seen on the AP CSA exam regarding inheritance. Please answer each to the best of your ability.
Click the Start button when you are ready to begin the exam, but only then as you can only take the exam once. Click on the Next button to go to the next question. Click on the Previous button to go to the previous question. Use the number buttons to jump to a particular question. Click the Pause button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the Finish button after you have answered all the questions. The number correct, number wrong, and number skipped will be displayed.
Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane. Which of these constructors would be legal for the NamedPoint class?
public class Point
{
private int myX; // coordinates
private int myY;
public Point( )
{
myX = 0;
myY = 0;
}
public Point(int a, int b)
{
myX = a;
myY = b;
}
// ... other methods not shown
}
// The following incomplete class declaration is intended to extend the
// above class so that two-dimensional points can be named.
public class NamedPoint extends Point
{
private String myName;
// constructors go here
// ... other methods not shown
}
Proposed Constructors:
I. public NamedPoint()
{
myName = "";
}
II. public NamedPoint(int d1, int d2, String name)
{
myX = d1;
myY = d2;
myName = name;
}
III. public NamedPoint(int d1, int d2, String name)
{
super(d1, d2);
myName = name;
}
I and III
NamedPoint will inherit from Point all fields but the fields are private and they can not be directly accessed in NamedPoint. You can use super as the first line in a constructor to initialize inherited fields. You can also set your own fields in a constructor. If you don’t use super as the first line in a constructor one will be put there by the compiler that will call the parent’s no argument constructor.
I only
I is okay but III is also okay.
II only
II is invalid. Children inherit all of the fields from a parent but do not have direct access to private fields. You can use super in a constructor to initialize inherited fields by calling the parent’s constructor with the same parameter list.
III only
I is also okay
I and II
I is okay but II is invalid. Children inherit all of the fields from a parent but do not have direct access to private fields. You can use super in a constructor to initialize inherited fields by calling the parent’s constructor with the same parameter list.
public class ContactInfo
{
private String name;
private String phoneNumber;
public ContactInfo(String theName, String thePhoneNumber)
{
this.name = theName;
this.phoneNumber = thePhoneNumber;
}
public String getName() { return name; }
public String getPhoneNumber() { return phoneNumber; }
}
public class ExtendedContactInfo extends ContactInfo
{
private String nickname;
public ExtendedContactInfo (String theNickname,
String theName,
String thePhoneNumber)
{
// missing code
}
}
I.
super(theNickname, theName, thePhoneNumber);
II.
this.name = theName;
this.phoneNumber = thePhoneNumber;
this.nickname = theNickname;
III.
this.nickname = theNickname;
this.name = theName;
this.phoneNumber = thePhoneNumber;
IV.
this.nickname = theNickname;
this.name = theName;
this.phoneNumber = thePhoneNumber;
V.
super(theName,thePhoneNumber);
this.nickname = theNickname;
I
There is no parent constructor which takes all three of these parameters.
II
You can not access private inherited fields directly. You can either use public method to get and set their values or invoke the parent’s constructor using super(paramList) as the first line of code in a constructor.
III
You can not access private inherited fields directly. You can either use public method to get and set their values or invoke the parent’s constructor using super(paramList) as the first line of code in a constructor.
IV
You can not access private inherited fields directly. You can either use public method to get and set their values or invoke the parent’s constructor using super(paramList) as the first line of code in a constructor.
V
To initialize inherited private fields you can use the parent’s constructor. Use super followed by the parameter list as the first line of code in the constructor.
I. Methods from a superclass can be used in a subclass without rewriting
or copying code.
II. Objects from subclasses can be passed as arguments to a method designed
for the superclass
III. Objects from subclasses can be stored in the same array
IV. All of the above
V. None of the above
IV
All of these are valid reasons to use an inheritance heirarchy.
V
In fact, all of the reasons listed are valid. Subclasses can reuse methods written for superclasses without code replication, subclasses can be stored in the same array, and passed as arguments to methods meant for the superclass. All of which make writing code more streamlined.
I and II
III is also valid. In some cases you might want to store subclasses together in a single array, and inheritance allows for this.
I and III
II is also valid. In some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass objects of the subclasses to the same method instead of writing individual methods for each subclass.
I only
II and III are also valid, in some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass all the subclasses to the same method instead of writing individual methods for each subclass and you might want to store subclasses together in a single array, and inheritance allows for this.
I. Methods from a superclass can be used in a subclass without rewriting or copying code.
II. An object from a subclass can be passed as an argument to a method that takes an object of the superclass
III. Objects from subclasses can be stored in the same array
IV. All of the above
V. None of the above
IV
All of these are valid reasons to use an inheritance hierarchy.
V
In fact, all of the reasons listed are valid. Subclasses can reuse methods written for superclasses without code replication, subclasses can be stored in the same array, and passed as arguments to methods meant for the superclass. All of which make writing code more streamlined.
I and II
III is also valid. In some cases you might want to store subclasses together in a single array, and inheritance allows for this.
I and III
II is also valid. In some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass objects of the subclasses to the same method instead of writing individual methods for each subclass.
I only
II and III are also valid, in some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass all the subclasses to the same method instead of writing individual methods for each subclass and you might want to store subclasses together in a single array, and inheritance allows for this.
public class Dog
{
private String name;
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
}
public class DogOwner
{
private String name;
private Dog[] dogs;
}
Interface
An interface is a special kind of abstract class. It isn’t a type of relationship between classes.
Polymorphism
Polymorphism is using the run-time type of the object to determine which method to run. It isn’t a type of relationship between classes.
Inheritance (is-a)
Inheritance is when one class (the child class) extends the other (the parent class). Do you see the keyword extends here?
Association (has-a)
Association is when one class keeps track of one or more objects of the other class. In this case a DogOwner object has an array of dog objects.
Overloading
Overloading is when a class has two methods with the same name but the parameter lists are different. It is not a type of relationship between classes.
Class C extends class B, which extends class A. Also, all of the three classes implement a public method test(). How can a method in an object of class C invoke the test() method defined in class A (without creating a new instance of class A)?
This would run the test method in class C since the object was created by the C class. When a method is called the runtime system will start looking for the method in the class that created the object.
super.super.test();
You can’t use super.super. This would cause a compile-time error.
super.test();
This would run the test method in class B since super is used to run a method in your parent class and B is the parent of C.
this.test();
This would run the test method in class C.
There is no way to call a method in a grandparent class from a grandchild class
You can use super to force the runtime to run a method in a parent class, but there is no way to force a call to a method in a grandparent (parent of your parent) class.
public class Vehicle
{
public void test(Car x, SportsCar y) {}
}
public class Car extends Vehicle {}
public class SportsCar extends Car {}
public class VehicleTest
{
public static void main(String[] args)
{
Vehicle v = new Vehicle();
Car c = new Car();
SportsCar sporty = new SportsCar();
}
}
v.test(sporty,v);
This would be true if the test method took a SportsCar object and a Vehicle object.
sporty.test(c,c);
This would be true if the test method took two Car objects or a Car and a Vehicle object.
v.test(sporty,c);
This would be true if the test method took a SportsCar object and a Car object.
sporty.test(sporty,v);
This would be true if the test method took a SportsCar object and a Vehicle object.
c.test(sporty,sporty);
The test method takes a Car object and a SportsCar object. Only this answer correctly passes a SportsCar object as the second parameter. You can use a SportsCar object as a Car object since it is a subclass of Car. The test method can be called on any child of Vehicle.
public class Parent
{
public void m1()
{
System.out.print("pm1");
m2();
}
public void m2()
{
System.out.print("pm2");
}
}
public class Child extends Parent
{
public void m1()
{
super.m1();
System.out.print("cm1");
}
public void m2()
{
super.m2();
System.out.print("cm2");
}
}
pm1pm2cm2cm1
When p.m1() is run it will execute the m1 method in Child since p is an object of that class. The first line calls super.m1() which will execute the m1 method in Parent. That method will print "pm1" and then call m2(). The m2 method in child will execute since p is a Child object. The first line in that method calls super.m2() which will execute the m2 method in Parent. This will print "pm2". Then the parent m2 method will return, so execution will continue in the m2 method of Child and it will print "cm2". Then the child m2 method will return which will continue execution in the m1 method of Child which will print "cm1".
pm1pm2
This would be true if p was an object of the Parent class, but it is an object of the Child class and the runtime will start execution of a method in the Child class method if it has it.
pm1pm2cm1cm2
Remember that each method call is added to the call stack and after the method returns execution continues with the next statement after the method call.
pm1cm1
This would be true if the m1 method in Parent didn’t call m2().
pm1
This would be true if the m1 method in Parent didn’t call m2() and the p was actually an object of the Parent class.
public class Animal
{
// constructors not shown
public void eat()
{ // code not shown
}
}
public class Bear extends Animal
{
// constructors not shown
public void growl()
{ // code not shown
}
}
Assume that the following declaration is in a different class.
Animal b = new Bear();
Which of the following will compile without error?
I. b.eat();
II. b.growl;
III. ((Bear) b).growl();
I only
I does work, but so does another one.
II only
The compiler will look for the method based on the declared type. The declared type for b is Animal and Animal doesn’t have a growl method.
III only
III does work, but so does another one.
I and III only
I works since the declared type is Animal and Animal has an eat method. III works because the cast tells the compiler to treat b is a Bear and Bear has a growl method.
I, II, and III
Does Animal have a growl method? Remember that the compiler checks for the method using the declared type.
A classroom is a room and a building has many rooms. If the three classes Room, Classroom, and Building create objects that have the same relationship which of the following is the most appropriate set of declarations?
public class Room extends Classroom implements Building { … }
Is a Classroom a type of Building? Don’t use extends unless an object of the child class can be substituted for a object of the parent class.
public class Classroom extends Room { … } public class Building { private Room[] rooms; …. }
If a classroom is a room, then Classroom should extend Room (inherit from it). If a Building has rooms it should have a field that holds them. Since a Building can have more than one Room we can use an array to hold the rooms.
public class Room extends Building { private Classroom room; …. }
Is a Room a type of Building? Don’t use extends unless the child is the same type of thing as the parent.
public class Classroom extends Building, Room { … }
You can’t extend two classes in Java so this can’t be right.
public class Room extends Classroom, Building { … }
You can’t extend two classes in Java so this can’t be right.
public class C1
{
private int num;
private String name;
public C1(int theNum)
{
num = theNum;
}
public C1(String theName)
{
name = theName;
}
// other methods not shown
}
public class C2 extends C1
{
// methods not shown
}
Possible constructors
I. public C2 () { }
II. public C2 (int quan) {super (quan); }
III. public C2 (String label) { super(label); }
All three are valid
If there is not a call to super as the first line in a child class constructor then super() is automatically added. However, this will cause a problem if the parent class does not have a no argument constructor.
II only
While II is valid so is another choice.
III only
While III is valid so is another choice.
II and III
Since C1 has constructors that take just an int and just a String both of these are valid.
None are valid
C2 constructors can call C1 constructors using the super keyword. In fact this call is automatically added to C2 constructors as the first line in any C2 constructor if it isn’t there.
Given the following array declaration and the fact that Animal is the parent class for Bird, Dog, Pig, Cat, and Cow, what is output from looping through this array of animals and asking each object to speak()?
Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }
Animal that has a method speak() which returns "Awk".
Bird doesn’t have a speak method
Dog has a speak method that returns "Woof"
Pig doesn’t have a speak method
Cow has a speak method that returns "Moo"
Cat has a speak method that returns "Meow"
Awk Awk Awk Awk Awk
This would be true if none of the children classes overrode the speak method, but many do.
This won’t compile
It is always okay to substitute a child object for a parent object.
Meow Moo Woof Oink Tweet
This would be true if Pig had a speak method that returned "Oink" and Bird had a speak method that returned "Tweet", but they do not. The inherited speak method will be called in Animal.
Meow Moo Woof Oink Awk
This would be true if Pig had a speak method that returned "Oink", but it does not.
Meow Moo Woof Awk Awk
Both Pig and Bird do not have a speak method so the one in Animal will be used.