COMP200 - Object Oriented Programming: Test One
COMP200 - Object Oriented Programming: Test One
COMP200 - Object Oriented Programming: Test One
Test One
1. Study the following classes and answer the questions that follow: MyPoint represents a single point in 2-
dimensional space and My3DPoint, a subclass of MyPoint, that represents a point in 3-dimensional space.
Both classes are in the same package.
package questionOne;
int x = 0;
int y = 0;
package questionOne;
int z = 0;
1
COMP200 Test One 2018
1.1.) Will the class My3DPoint compile without error? Explain clearly why it will or will not compile. (2)
Solution: Call to implicit constructor super() will fail as there is no no-arg constructor in the
super-class.
It is not sufficient to say that there is no constructor - because the compiler inserts a no-arg default
constructor. Also it is not sufficient to say that there is no call to super - because the compiler also
inserts a call to super(). So the reason why this does not compile is that there is not no-argument
constructor in the super class, so the call to super() fails.
1.2.) For each of the following constructors, state whether the constructor can be added to the class My3DPoint
without causing a compile error. Give a reason if it will result in a compile error. (10)
Solution: Call to superclass constructor will fail as there is no no-arg constrcutor in the super
class.
Solution: No error.
This is fine, even though the constructor does not initialize the field z. It is not required that
constructors initialize any or all of the fields. It is a good idea to do so, but it is not a compile
error.
Solution: No error.
Solution: Error. Call to super constructor must be first statement in the constructor.
v. public My3DPoint() {
this.z = 0;
}
Solution: Error, Call to superclass constructor will fail. In this case, the compiler inserts a
call to the no-arg super constructor. This fails since there is no no-arg constructor in the super
class.
COMP200 2 2018
COMP200 Test One 2018
1.3.) Assume that a correct constructor has been added to the class My3DPoint and the classes compile without
error.
State whether each of the following methods can be added to the class My3DPoint without resulting in a
compile error. Give a reason if it will result in a compile error. If it compiles without errors then state
whether the method is an override or an overload. (8)
Solution: Cannot be added. This is an override (we are not changing the argument list–so
cannot be an overload.) and overrides are not allowed to change the return type.
Solution: Error. Not override because we cannot make visibility more restrictive. Not over-
load - arguments are the same.
COMP200 3 2018
COMP200 Test One 2018
2. Study the classes below and answer the questions that follow. Ques. 2 Total: 15
public class Vehicle {
private int numberOfWheels;
2.1.) Write a subclass of MotorisedVehicle called Car that additionally has a field doors that stores the num-
ber of doors the car has. (5)
Solution:
public class Car extends MotorisedVehicle {
COMP200 4 2018
COMP200 Test One 2018
2.2.) For each of the following state whether the statement/s may be added to the indicated classes without
causing compilation problems. If it will result in compilation problems then explain why.
i. The statement ArrayList<Vehicle> v = new ArrayList<Vehicle>(); may be added to the main
method without causing compilation problems.
Solution: No problems
Solution: Compile error. Method not defined in type Vehicle. Note that the compiler checks
if the variable v is used properly. Since v is of type Vehicle, the compiler checks if the method
call is correct.
Solution: Error. Type incompatibility. Cannot store super class reference in sub class type.
Solution: No error.
v. The statements:
Object o = new MotorisedVehicle(4,2000);
System.out.println(o.getNumberOfWheels());
(10)
COMP200 5 2018