Exam: Object Oriented Software
Development // (SWEN20003_2021_SM1)
Started: Jun 15 at 15:01
Quiz Instructions
Instructions to Students
The exam has 32 questions:
10 true/false – 10 marks
20 multiple choice – 20 marks
2 long answer questions – 70 marks
Total marks for the exam is 100 marks which will count
for 50% of the final subject mark.
This is an online, open-resources exam, which means you are allowed to look up
anything including searching the Internet. However, if you refer to material from
other sources, you are not allowed do directly copy anything including from subject
material, and your own notes prepared before the exam – the answers must be your
own.
Academic Integrity Declaration
By commencing and/or submitting this assessment I agree that I have read and
understood the University’s policy on academic integrity.
(https://academicintegrity.unimelb.edu.au/#online-exams)
I also agree that:
1. Unless paragraph 2 applies, the work I submit will be original and solely my own
work (cheating);
2. I will not seek or receive any assistance from any other person (collusion) except
where the work is for a designated collaborative task, in which case the individual
contributions will be indicated; and,
3. I will not use any sources without proper acknowledgment or referencing
(plagiarism).
4. Where the work I submit is a computer program or code, I will ensure that:
https://academicintegrity.unimelb.edu.au/#online-exams
a. any code I have copied is clearly noted by identifying the source of that code at
the start of the program or in a header file or, that comments inline identify the
start and end of the copied code; and
b. any modifications to code sourced from elsewhere will be commented upon to
show the nature of the modification.
1 ptsQuestion 1
True
False
“Double” is a primitive data type.
1 ptsQuestion 2
True
False
ArrayList is a data structure that uses generics.
1 ptsQuestion 3
True
False
Within a class, a static method cannot access non-static attributes.
1 ptsQuestion 4
True
False
The Strategy Pattern uses inheritance as the main technique for
achieving code reuse.
1 ptsQuestion 5
True
False
“private” attributes in a parent class are accessible in child classes.
1 ptsQuestion 6
True
False
Exception handling is an example of event-driven programming.
1 ptsQuestion 7
Lambda expressions can be used to pass a function as a piece of data
to another function.
True
False
1 ptsQuestion 8
True
False
A data type defined by “enum” is a class except that it cannot have
methods inside.
1 ptsQuestion 9
True
False
Data entries added into a HashMap are stored as an ordered
sequence.
1 ptsQuestion 10
True
False
Generics can take multiple type parameters such as classes, interfaces,
or primitive types.
1 ptsQuestion 11
2
3
4
1
5
What is the output of the following program?
1 ptsQuestion 12
HashMap allows to store key-value pairs of non-primitive types.
HashSet allows to store unique values of primitive types such as int or double.
The size of an ArrayList must be defined at the time the ArrayList is created.
ArrayList has a fixed length.
Which of the following statements are correct? You may choose more
than one answer.
1 ptsQuestion 13
[Alis, Brian, Chloe, David]
[Alis, Brian, Chloe]
[Brian, Chloe, Alis, David]
[Anna, Charles, Bob, David]
What is the output of the following program?
1 ptsQuestion 14
Multiple listeners can respond to the same event.
Event-driven programming does not allow multiple events to be handled
simultaneously.
Event driven design usually increases coupling between components.
A listener can handle events only from one component.
Which of the following statements are correct regarding event-driven
programming? You may choose more than one answer.
1 ptsQuestion 15
Exception handling enables providing more detailed information about the
error that happened.
Exception handling allows to chain exceptions, in which the order of the
exceptions in the chain is not important.
Exception handling is usually used for handling syntax errors.
Unchecked exceptions can be detected at compile time.
Compiler can detect checked exceptions.
Which of the following statements about exception handling are
correct? You may select more than one answer.
1 ptsQuestion 16
Which of the following statements are correct? You may select more
than one answer.
Assuming an object named ‘printer’ has a private method named ‘print()’, we
can still call printer.print()
Private methods in the parent class can be accessed by its child classes.
Static methods can use (reference to) any attribute defined in the same class
Static methods can call any public method defined within the same class.
Protected methods in the parent class can be accessed by its child classes.
1 ptsQuestion 17
Polymorphism is one of the core features of the object-oriented paradigm.
Polymorphism enables programmer to write more maintainable code.
Polymorphism is another name for encapsulation.
Inheritance, interfaces, overloading and overriding are some features that
enable polymorphism in Java.
Polymorphism and inheritance are the same concept.
Select statements which are NOT correct about polymorphism. You
may select more than one answer.
1 ptsQuestion 18
An interface can define private methods that child classes will implement.
Interfaces help enable polymorphism in Java.
An interface cannot be used as type parameter for a generic data type such
as ArrayList.
A non-empty method implemented in an interface can be overridden by its
child classes.
Which of the following statements are NOT correct about interfaces?
You may select more than one answer.
1 ptsQuestion 19
A class can have multiple constructors with different constructor names.
The returned value of a class constructor can be of any type such as int, bool,
etc.
A constructor can throw exceptions
A constructor of an interface has the same name as the interface.
A constructor can be set private or protected.
Which of the following statements are correct? You may choose more
than one answer.
1 ptsQuestion 20
Observer Pattern.
Strategy Pattern.
Template Method.
Factory Method.
Singleton Pattern.
A real estate application has several clients who are interested in
buying and selling real estate properties. Clients who want to buy
properties under a particular price can set a price alert to receive
updates about properties that satisfy clients’ criteria. Which one of the
following design patterns is most suited for this problem?
1 ptsQuestion 21
A Class 1 object must be associated with one Class 2 object.
A Class 2 object must be associated with at least one Class 1 object.
A Class 1 object must be associated with no more than one Class 2 object.
A Class 2 object can only be associated with no more than nine objects of
type Class 2.
A Class 2 object can be associated with five objects of type Class 1.
Which of the following statements are correct regarding the following
UML diagram? You may choose more than one answer.
1 ptsQuestion 22
A Java class cannot extend multiple abstract classes.
A Java class that implements multiple interfaces and extends multiple abstract
classes must implement all methods in the parents.
A Java class can extend multiple abstract classes and interfaces.
A child class can override all methods in parent class.
A child class can override the constructor of its parent class.
Which of the following statements are correct? You may choose more
than one answer.
1 ptsQuestion 23
FILL_1: i.hasNext(), FILL_2: result == null || n.getValue() >= result.getValue()
FILL_1: i.next(), FILL_2: result != null || n.getValue() >= result.getValue()
FILL_1: i.next(), FILL_2: n.getValue() >= result.getValue() || result == null
FILL_1: i.hasNext(), FILL_2: result != null || n.getValue() >= result.getValue()
Fill in the missing conditions FILL_1 at line 11 and FILL_2 at line 13 of
the following code so that the code outputs “SWEN20003=500”.
1 ptsQuestion 24
What is the output of the following code?
5
10
8
The program will crash
1 ptsQuestion 25
Template Method
Strategy Pattern
Singleton Pattern
Observer Pattern
Factory Method
A parent class has an algorithm that has multiple small steps that need
to be implemented by child classes. What is the most suitable design
pattern for this problem?
1 ptsQuestion 26
Which one of the following statements is correct?
Composition over inheritance avoids using inheritance to reduce the
unnecessary code complexity.
Composition over inheritance primarily uses inheritance to allow flexible
design.
In event-driven programming, the flow of a program’s execution is controlled
primarily by State.
Graphical User Interface (GUI) applications are often considered as not an
event-driven programming style.
1 ptsQuestion 27
Unit testing allows to prove the presence of a bug in a software program.
JUnit is a programming language that is used to write tests for Java programs.
Unit testing allows to reveal all the bugs that can happen in a software
program.
Unit testing allows to ensure that a software program is free of errors.
JUnit allows to check whether the expected output of a program matches with
the actual output of the program
Which of the following statements are correct? You may choose more
than one answer.
1 ptsQuestion 28
Design should allow minimum coupling and maximum cohesion.
Design should allow maximum coupling and minimum cohesion.
Design should allow classes to be open to modification and closed to
extension.
Design should allow classes to be open to modification via using inheritance.
Which of the following statements are correct? You may choose more
than one correct answer.
1 ptsQuestion 29
Enum class can have methods and attributes.
Enum class cannot have constructors.
Enum class can consist of an infinite number of constants.
Enum class does not have toString() method.
Enum can implement an interface
Which of the following statements are correct about enum in Java? You
may choose more than one answer.
1 ptsQuestion 30
In Java, a functional interface has one method
NullPointerException is a checked exception
In Java, lambda expressions are represented as objects of type known as a
functional interface
FileNotFoundException is a checked exception
Which of the following statements are correct? You may choose more
than one answer.
40 ptsQuestion 31
In this task, you will help Bob, a student in SWEN20003, implement the
following problem specification.
Problem specification. Given an initial set of arrays of length five, in
which each array item is an integer, which is either 0 or 1, the system
tries to modify the arrays so that it finally arrives at the target array of
five integers that are all 0 (zero).
Example. Assuming the initial population of arrays are a1: [1,0,1,0,1],
a2: [0,0,1,1,0], a3: [0,0,0,1,0], the system tries to modify the arrays to
come up with a new population of arrays, and selects arrays that have
“more 0” to repeat the process. For example, the system will select
arrays a2 and a3 (which have three and four zeros respectively) to
change them. The final target that the system needs to find is the array
[0,0,0,0,0] that have all items to be zero.
Code. To help Bob implement the system above, your task is to
complete the code for the classes: FixedGenerationAlgo,
RandomMutationStrategy, EvenSplitCrossOverStrategy,
ValuePropotionalProductionStrategy, Chromosome, and TargetAllZeros,
and answer some questions below. Note that you will have to create a
class by yourself following the UML diagram.
Answer six sub-questions below and copy paste your answer into the
text box of this question.
Q1. Implement the class Chromosome shown below following the
instruction given. (7 marks)
The Chromosome consists of genes, which is an array of five integer
numbers, which are either 0 or 1. Fill in the code for the following
methods in Chromosome.
(a) Implement constructors Chromosome() and Chromosome(genes:
int[]). The constructor Chromosome() initializes the genes with random
values (between 0 and 1). Hint: To generate a random integer number
between 0 and 1, you can use java.util.Random.nextInt(2). The
constructor Chromosome(int[] genes) assign the genes in the
Chromosome class with the given genes in the argument of the
constructor. (1 mark)
(b) Implement the method calculateFitness. This method calculates the
fitness value, which counts how many zeros there are in the genes. For
example, a chromosome with genes being [0,0,1,0,1] has fitness of 3
because the array has three zeros. The lowest fitness a chromosome
can have is 0 and the highest is 5, i.e., [1,1,1,1,1] has fitness of 0
because it has no zero, while [0,0,0,0,0] has fitness of 5 because it has
five zeros. This method should only compute the fitness once and set
the computed value to the (private) fitness variable. This
implementation makes use of the private variables “calculatedFitness”
and “fitness” in the Chromosome class. The Boolean variable
calculatedFitness indicates whether the fitness value of the
Chromosome has been computed or not. (1.5 marks)
(c) Implement the method getFitness. This method returns the
computed fitness. Hint: use method calculateFitness. (1 mark)
(d) Implement method getGenes, which returns the genes of the
Chromosome. (1 mark)
(e) Implement method compareTo. This method compares the fitness
values of two Chromosomes. It returns -1 if the fitness value of the
current object is less that the fitness value of the object o being
compared to. It returns 0 if the fitness values of the objects are equal
and returns 1 otherwise. (1 mark)
(f) Implement method clone() that returns a copy of the current
Chromosome. The attributes in the copy must have the same values as
the value of the current Chromosome. Note that for copying the array
genes, each item in the array must be copied over to a new array. (1.5
marks)
Q2. Implement the class FixedGenerationAlgo and create the abstract
class GAlgo following the UML diagram below. (7 marks)
(a) The variable maxGenerationCount should be initially set to 100 and
variable generationCount should be initially set to 0. (1 mark)
(b) Implement method moreGeneration. This method returns true if
generationCount is less than maxGenerationCount and false otherwise.
(1 mark)
(c) Implement method incrementGeneration. This method increase the
generationCount by one. (1 mark)
(d) Implement method currentGeneration. This method returns the
generationCount. (1 mark)
(e) Implement method initializeGeneration. This method initializes the
(protected) variable chromosomes with an ArrayList of 10 new
Chromosomes. Hint: use new Chromosome() to create a new
Chromosome object. (1 mark)
(f) Assuming the method peseudo code of solve() in GAlgo is:
while(moreGeneration()){
initializeGeneration();
incrementGeneration();
}
What is the design pattern used here? Please briefly justify your
answer. (2 marks)
Q3. Implement the class ValuePropotionalProductionStrategy and
create the interface ProductionStrategy following the UML diagram
below. (7 marks)
(a) Implement method select. This method returns two best
Chromosomes, which have highest fitness values, from the ArrayList
chromosomes. Hint: you can use Collections.sort. (3 marks)
(b) Implement method bestChromosome. This method returns the best
Chromosome that has highest fitness value from the ArrayList c. (2
marks)
(c) Implement method worstChromosome. This method returns the
worst Chromosome that has lowest fitness value from the ArrayList c. (2
marks)
Q4. Implement class RandomMutationStrategy and create the interface
MutationStrategy following the UML below. (7 marks)
(a) Implement method mutate. This method changes/modifies a given
Chromosome and returns the newly created Chromosome using the
following algorithm: (3.5 marks)
Given a Choromsome m, the method randomly picks an item, which is
an interger number, in the array genes of the Chromosome m, and it
flips the picked number in the following way. If the picked number/item
is 0, it replaces the item by the number 1. If the picked number/item is
1, it replaces the item by the number 0. For example, if the genes of the
Chromosome m is [0,1,0,0,1], the method randomly picks the item at
index 3 of the array, which has the value 0. The method then flips this
item to the value 1 and returns a new Chromosome that has the genes
of [0,1,0,1,1].
(b) Implement method mutate. This method uses the private mutate
method above to mutate every Chromosome in a given ArrayList of
Chromorome selectedChromosomes. It returns the list of newly created
Chromosomes. (3.5 marks)
Q5. Implement class EvenSplitCrossOverStrategy and create the
CrossoverStrategy inferface in the below UML. (7 marks)
Implement method crossover using the following algorithm,
Given a list of Chromosomes, namely chromosomes, which has only
two items of type Chromosome, this method tries to create two new
Chromosomes based on the given chromosomes. It does so by
swaping the first two numbers/items of the first Chromosome with the
first two numbers/items of the second Chromosome. For example,
given two Chromosomes that have genes [0, 0, 0, 0, 1] and [1, 0, 1, 1,
1], the method creates two new Chromosomes with genes [1, 0, 0, 0, 1]
and [0, 0, 1, 1, 1]. Here, the first two numbers [0, 0] and [1, 0] of the
arrays are swapped so that to create new Chromosomes.
Q6. Implement class TargetAllZeros and create the TerminationStrategy
in the below UML. (5 marks)
p 0 words
Implement method terminate. This method returns true if the given
ArrayList of Chromosomes c contains a Chromosome that has the
fitness value equals to 5. It returns false otherwise.
>
30 ptsQuestion 32
In this question, you will implement a wallet of digital currencies. A
wallet contains digital currencies and their balances, can perform
operations such as “deposit” and “send” currencies between wallets,
and contains histories of the operations. For example, the following
code creates two wallets w1 and w2, deposits currencies, e.g.,
depositing 100 AVAX and 200 ETH into wallet w1, sends 20 AVAX from
wallet w1 to wallet w2. The code then prints out the balances of w1 and
w2 and the histories of operations performed on w1 and w2.
Wallet w1 = new Wallet();
Wallet w2 = new Wallet();
Edit View Insert Format Tools Table
20px Paragraph
w1.deposit(AVAX.getInstance(), 100.0);
w1.deposit(ETH.getInstance(), 200.0);
w1.send(AVAX.getInstance(), 20.0, w2);
w1.printBalances();
w2.printBalances();
w1.printHistory();
w2.printHistory();
To implement the Wallet, you will write code following the instructions
below:
1. Implement an abstract class named Currency, a currency named
AVAX and a currency named ETH that implement the class Currency.
The class Currency has a private attribute named price of type double
and setter and getter methods for the attribute price. Please design the
AVAX class so that whenever the function AVAX.getInstance() is called,
it returns the same instance of type AVAX. Design the ETH class so that
whenever the function ETH.getInstance() is called, it returns the same
instance of type ETH. (6 marks)
2. Implement an enum named TransferType which holds two types of
transfers namely DEPOSIT, and SEND. (2 marks)
3. Implement a class History that is used to record performed
operations such as DEPOSIT and SEND. This class will keep
information about Currency c, the quantity (of type Double) of the
currency, and the type of the operation (TransferType) in a transaction.
The constructor of History is:
History(Currency c, Double quantity, TransferType type) (3 marks)
4. Implement a class Wallet that holds the following (19 marks):
An attribute named balances that holds the quantity of a currency in
the wallet. It should maintain an association between “Currency” and
“Double” so that it allows to query the quantity of a currency held in
the wallet. (2 marks)
An attribute named histories that stories a series of History. This
attribute records the history of operations performed on the wallet.
Please use appropriate data structure for the attribute. It should allow
the histories to automatically grow in size. (2 marks)
Implement a method named deposit with the signature: public void
deposit(Currency c, Double quantity) that adds a quantity of a
currency into the current wallet. This method also needs to record
the history of the operation performed. Hint: it should add a History of
DEPOSIT type into the histories attribute. (5 marks)
Implement a method named send with the signature: public void
send(Currency c, Double quantity, Wallet targetWallet)that sends a
quantity of the currency c from the current wallet to a target wallet.
Note that this method should decrease and increase the balance of
the currency c in the current wallet and the target wallet respectively.
If the balance of the currency c in the current wallet is not sufficient,
this method should throw a RuntimeException to indicate an
insufficient balance for the send operation and the send operation
should halt without changing the balance of the wallets involved. This
method should also record the history of the operations performed.
Hint: this method should make use of the deposit method to increase
the balance of the target wallet. To handle history, it should add the
history for the type SEND. (6 marks)
Implement a method printBalance() that prints the balance of each
currency in the wallet.(2 marks)
Implement a method printHistory() that prints all the History in the
histories attribute. (2 marks)
Edit View Insert Format Tools Table
20px Paragraph
Saved at 13:01
p 0 words >
Submit Quiz