DEGREES of MSc Information Technology, MSc in Software Development and MSc IT Cyber Security
Advanced Programming (IT)
Monday 29 April 2019
1. (a) All Java objects have a method called hashCode that allows them to be hashed.
Briefly describe what hashing is and how it is used in a data structure such as a HashSet. [4 marks]
(b) Explain why the length of the String would likely be a poor hash function for hashing Strings containing words in the English language. [2 marks]
(c) Within the context of hashing, what is a collision? [2 marks]
(d) Consider the following class:
public class Meal {
private final String mainCourse;
private final String dessert;
public Meal(String mainCourse, String dessert) {
this.mainCourse = mainCourse;
this.dessert = dessert
}
public String getMain() {
return mainCourse;
}
public boolean equals(Object o) {
String therMain = ((Meal)o).getMain();
if(mainCourse.equals(otherMain)) {
return true;
}else {
return false;
}
}
public String toString() {
return mainCourse + " and " + dessert;
}
}
With reference to objects in general, explain why String.equals() is used in the equals method as opposed to ==. [4 marks]
(e) A programmer decides to override hashCode in this class, using the method
below. Explain why this hashCode necessitates a change elsewhere in the class and rewrite the relevant part of the original.
public int hashCode() {
return mainCourse.hashCode() + dessert.hashCode();
} [5 marks]
(f) Consider the following code that makes use of this object:
String mainDish = "Fish";
String dessert = "Tiramisu";
Meal m = new Meal(mainDish,dessert);
mainDish = " and chips";
System.out.println(m);
Explain, with reference to the particular properties of Strings, why the output is "Fish and Tiramisu" and not "Fish and chips and Tiramisu" despite the object being referred to by the mainDish reference having changed. [3 marks]
2. (a) Briefly explain the purpose of the decorator design pattern. [3 marks]
(b) What is the advantage of a decorator over sub-classing? [2 marks]
(c) Consider the following two classes:
public abstract class AbstractOffice {
public void openDoor() {
System.out.println("The door has been opened");
}
}
public class ConcreteOffice extends AbstractOffice {
private String address;
public ConcreteOffice(String address) {
this.address = address;
}
}
You are tasked with using the decorator pattern to add a burglar alarm to an office. Firstly, create an abstract decorator class for AbstractOffice. [5 marks]
(d) Now create a concrete burglar alarm decorator. Think about any new methods and attributes that you need. [5 marks]
(e) Write a main method that creates an office decorated with your decorator. [3 marks]
(f) State another design pattern that can be used to add functionality to classes without inheritance or large-scale modifications. [2 marks]
3. (a) The wait and notify methods provided with all Java Objects allow Threads to be placed in a waiting state until notified by another Thread. With reference to the Thread states runnable, blocked, waiting, describe the process by which a Thread acquires an object’s monitor (via e.g. entering a synchronized block), waits and is awaken via notification by another Thread. [5 marks]
(b) The following three classes define a system including an Object (SendReceive)
that permits the Sender object to send messages (in the form of a String) to the Receiver object. The Sender should be able to send a message only when there isn’t one waiting to be received. If there is one waiting, it should wait until it has gone. The receiver should wait until a message appears to be received. The SendReceive object is missing the implementation of the send and receive methods.
public class Sender extends Thread {
private SendReceive sr;
public Sender(SendReceive sr) {
this.sr = sr;
}
public void run() {
String[] messages = {"Hi","How’re you?","Bye!"};
for(String message: messages) {
sr.send(message);
}
}
public static void main(String[] args) {
SendReceive sr = new SendReceive();
new Sender(sr).start();
new Receiver(sr).start();
}
}
public class Receiver extends Thread {
private SendReceive sr;
public Receiver(SendReceive sr) {
this.sr = sr;
}
public void run() {
while(true) {
System.out.println(sr.receive());
}
}
}
public class SendReceive {
/*
hasMessage should be set to true
when there is a message
waiting to be received,
false otherwise
*/
private boolean hasMessage = false;
private String message;
public void send(String message) {
// YOUR CODE HERE
}
public String receive() {
// YOUR CODE HERE
}
}
Using wait and notify, write code for the send and receive methods. You may change the method decorators if you wish. [10 marks]
(c) Using an example, describe what is meant by the term Race condition. For your example, describe how it could be avoided. [5 marks]
4. Please write the answers to the following 10 multiple-choice questions clearly in your answer booklet. There is only one correct answer in each case. Each correct answer is worth 2 marks. Incorrect answers will result in a penalty of two thirds ofa mark to discourage guessing.
(a) Which of the following statements about inheritance is true:
A. A class can extend many classes and implement many interfaces.
B. Private attributes are directly accessible by subclasses.
C. A final class cannot be subclasses.
D. A class can implement only one interface. [2 marks]
(b) Which of the following statements is false:
A. An object referred to by a final reference cannot be modified.
B. A final primitive cannot be modified.
C. A final method cannot be overridden.
D. A final object reference cannot be reassigned to a different object. [2 marks]
(c) Which of the following correctly describes the output of this code snippet:
public static void main(String[] args) {
Double a = 6.5;
Double b = a;
b *= 2;
System.out.println("a: " + a + ", b: " + b);
}
A. a: 13.0, b: 13.0
B. a: 13.0, b: 6.5
C. a: 6.5, b: 6.5
D. a: 6.5, b: 13.0 [2 marks]
(d) Which of the following correctly describes the output of this snippet:
public class ExamQ {
public static int aValue = 0;
public ExamQ() {
aValue++;
}
public static void main(String[] args) {
ExamQ a = new ExamQ();
ExamQ b = new ExamQ();
System.out.println(""+ a.aValue + " " + b.aValue);
}
}
A. 2 2
B. 0 0
C. 1 2
D. 1 1 [2 marks]
(e) Which of the following statements is false:
A. The unknown order of operation of Threads makes it hard to predict when deadlocks might occur.
B. A deadlock describes a situation when two Threads are waiting for one another.
C. Deadlocks can always be avoided through the use of synchronized blocks.
D. Conditions can be used to remove deadlocks. [2 marks]
(f) An object is garbage collected in Java when:
A. The method in which it was created ends.
B. When main has finished.
C. When there exists no references to it.
D. When it is unreachable from main. [2 marks]
(g) Which of the following statements about synchronized and locks is true:
A. Synchronized cannot span multiple methods whereas locks can be locked in one method and unlocked in another.
B. Locks are always preferable to synchronized blocks.
C. Synchronized can only be used to synchronize the class it is being used within.
D. Synchronized blocks are powerful because they allow us to use conditions. [2 marks]
(h) Which of the following is false:
A. Thread.join() is a blocking method.
B. InterruptedException must be caught when using blocking methods.
C. Thread.sleep() cannot be used in main.
D. Calling interrupt() on a Thread that is sleeping causes an Exception. [2 marks]
(i) Consider an Abstract class A that is subclassed by B. Which of the following is true:
A. It is not possible to subclass an abstract class.
B. B b = new B(); A a = b; is permissible
C. A a = new B(); B b = a; is permissible
D. It is essential to use Abstract classes in Java programming. [2 marks]
(j) Using the classes A and B from the previous question. A has a single method getName(). B overrides the method and also introduces a method getAge(). Which of the following is false:
A. B b = new B(); b.getAge(); will compile and run fine.
B. A a = new B(); a.getName(); will use the version of the getName() method defined in class A.
C. A a = new A(); a.getAge(); will fail to compile.
D. A[] a = new A[2]; a[0] = new A(); a[1] = new B(); will compile and run fine. [2 marks]