CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

Quiz 4: Questions

Q4.1.

[6 pts] Given the two classes below, what will main display?

public class Worker {
  private String job;

  public Worker(String s) {
    job = s;
  }

  public void setJob(String s) {
    job = s;
  }

  public String getJob() {
    return job;
  }
}
public class Main {
  public static void main(String[] args) {
    Stack<Worker> s = new Stack<Worker>();
    Worker gwb = new Worker("president");
    Worker bho = new Worker("senator");
    Worker you = new Worker("student");
    s.push(gwb);
    s.push(bho);
    s.push(you);
    gwb.setJob("none");
    you = bho;
    you.setJob("pres");
    bho = gwb;
    System.out.println(s.pop().getJob());
    System.out.println(s.pop().getJob());
    System.out.println(s.pop().getJob());
  }
}
Q4.2.

[8 pts] Suppose we remove from the heap whose tree diagram is below. What will its tree diagram then be?

Q4.3.

[8 pts] Suppose we add 3 into the heap represented by the array below. What will the array then be?

Q4.4.

[8 pts] Suppose that we have a Manager class that implements the ActionListener interface. The class has a no-parameter constructor. For each of the following fragments, indicate whether it would compile (at least one would), and for those that would not, indicate what the compiler would complain about.

a.
void reg(JButton b, Manager m) {
	b.addActionListener(m);
}

void run() {
	JButton b = new JButton("OK");
	reg(b, new Manager());
}
    b.
void reg(JButton b, ActionListener m) {
	b.addActionListener(m);
}

void run() {
	JButton b = new JButton("OK");
	reg(b, new Manager());
}
c.
void reg(JButton b, Manager m) {
	b.addActionListener(m);
}

void run() {
	JButton b = new JButton("OK");
	reg(b, new ActionListener());
}
    d.
void reg(JButton b, ActionListener m) {
	b.addActionListener(m);
}

void run() {
	JButton b = new JButton("OK");
	reg(b, new ActionListener());
}

Quiz 4: Solutions

Q4.1.
student
pres
none
Q4.2.
Q4.3.
Q4.4.

a. This is OK.

b. This is OK.

c. You cannot create an instance of an interface such as ActionListener. Also, reg expects a Manager but in run's invocation of reg, ActionListener does not conform to that type.

d. You cannot create an instance of an interface such as ActionListener.