You are on page 1of 7

GEIT 1412 Computer Science II

Quiz III Review

1) Use the check mark (√) to answer the following questions with either True or
False (all questions are related to the Java programming language):

A method that takes a superclass object as argument can also take an


object of any direct or indirect subclass of it.
√ True
False

It is possible to instantiate objects from abstract classes.


True
√ False

The following is a valid declaration of an abstract method:


public void abstract draw();
True
√ False

Determining the object’s type at execution time is called static binding.


True
√ False
Interfaces may contain concrete methods.
True
√ False

A concrete class implements an interface using the keyword implement.


True
√ False

Interfaces may also contain final and static constants.


√ True
False

Objects of any class that implements an interface can also be thought of as


objects of the interface type.
√ True
False
2) For the following questions, check (√) the correct answer (all questions are related
to the Java programming language):

Classes that can be used to instantiate objects are called


abstract classes
solid classes
concrete classes
none of the above

One of the following is a valid declaration of an abstract method.


public abstract void draw();
public void abstract draw();
abstract public void draw();
none of the above

The UML expresses the relationship between a class and an interface


through a relationship called
inheritance
realization
acquisition
none of the above

Choose the wrong statement.


Interfaces can have any number of methods.
Interfaces may also contain final and static constants.
All methods declared in an interface are implicitly
private abstract methods.
none of the above
3) Trace the following program to determine its output.

public class Q3 {

public static void main(String[] args) {


Staff[] staff = new Staff[4];

Manager m1 = new Manager("Mike Smith","staff-012-90", 30003150, 300);


Manager m2 = new Manager("Allen Roberts","staff-011-88", 50005250, 500);

SalesPersonnel s1 = new SalesPersonnel("Rob Dickson","staff-123-77",


40004120, "Chicago", 200);
SalesPersonnel s2 = new SalesPersonnel("Peter Jordan","staff-444-55",
45004635, "Washington", 200);

staff[0]=m1;
staff[1]=m2;
staff[2]=s1;
staff[3]=s2;

for (Staff st: staff)


{
st.raise();
System.out.println(st);
}
}

abstract class Staff {


private final String name;
private final String ID;
double salary;

public abstract void raise();

public Staff(String name, String ID, double salary) {


this.name=name;
this.ID=ID;
this.salary=salary;
}

public String getName() {return name;}


public String getID() {return ID;}

public double getSalary() {return salary;}


public void setSalary(double salary) {this.salary=salary;}

public String toString() {return String.format("Name=%s, ID=%s, Salary=$


%.2f",name,ID,salary);}

class Manager extends Staff {


double bonus;
final double RAISE_RATE = 0.05;

public Manager(String name, String ID, double salary, double bonus) {


super(name,ID,salary);
this.bonus=bonus;
}
public void setBonus(double bonus) {this.bonus=bonus;}
public double getBonus() {return bonus;}

public String toString() {return String.format("%s, Bonus=%.2f",super.toString(),


bonus);}
@Override
public void raise() {setSalary(getSalary()+getSalary()*RAISE_RATE);}
}

class SalesPersonnel extends Staff {


String salesArea;
double carAllowance;
final double RAISE_RATE = 0.03;

public SalesPersonnel(String name, String ID, double salary, String salesArea, double
carAllowance) {
super(name,ID,salary);
this.salesArea=salesArea;
this.carAllowance=carAllowance;
}
public void setSalesArea(String salesArea) {this.salesArea=salesArea;}
public String getSalesArea() {return salesArea;}

public void setCarAllowance(double carAllowance)


{this.carAllowance=carAllowance;}
public double getCarAllowance() {return carAllowance;}

public String toString() {return String.format("%s, Sales Area=%s, Car Allowance=


%.2f",super.toString(), salesArea, carAllowance );}
@Override
public void raise() {setSalary(getSalary()+getSalary()*RAISE_RATE);}
}

Output:

Name= Mike Smith, ID=staff-012-90, Salary=$3150.00, Bonus=300.00


Name= Allen Roberts, ID=staff-011-88, Salary=$5250.00, Bonus=500.00
Name= Rob Dickson, ID=staff-123-77, Salary=$4120.00, Sales Area=Chicago, Car Allowance=200.00
Name= Peter Jordan, ID=staff-444-55, Salary=$4635.00, Sales Area=Washington, Car Allowance=200.00

You might also like