You are on page 1of 14

Programming 2 ALC Sample Exam Questions

Q1
(a) With reference to the model class diagram in Appendix A and the partial
class definitions in Appendix B, write down and complete the missing
numbered phrases explaining the purpose of each. Note some phrases are
used more than once in the code. [14]

(b) With reference to the partial class definitions in Appendix B and your answer
to (a), explain each of the components of the method definitions. [6]

(c) With reference to the model class diagram in Appendix C explain the
different types of methods required giving an example of each type using a
method signature. [16]

(d) Describe the purpose of each type of modifier:

(i) public;
(ii) private;
(iii) static;
(iv) final; [4]

Q2
(a) With reference to the model class diagram in Appendix A and the text file
sample in Appendix D explain the operation of the load() method in
Appendix E used to populate a Repository object from the sample text file. [10]

(b) With reference to your answer to (a) explain the changes required to load
from an object file and any required changes to model class definitions. [5]

(c) Explain issues concerning the store() method detailed in Appendix F with
reference to the model class diagram in Appendix A and partial class
definitions in Appendix B. [5]
(d) With reference to the model class diagram in Appendix C and the partial
method definition in Appendix G, complete and write down the missing
numbered phrases explaining the purpose of each. [14]

(e) With reference to the model class diagram in Appendix C and the partial
method definition in Appendix G, produce a sample text file format to persist
Artist & Painting objects. [6]

Q3
(a) Explain the purpose, benefits and organisation of the Java Collections
Framework. [12]
(b) With reference to the class diagram in Appendix A and partial class
definitions in Appendix B, explain any changes to the classes to implement
the following functionality using the list variable defined below.

List<Attendee> attendeeList;

(i) a report showing all attendee details in order of id;

(ii) a report showing all attendee details in order of name. [8]

(c) The Java Collections Framework defines Interfaces, Implementations and


Algorithms: explain the purpose of each of the interface types below
detailing implementations and algorithm examples for each.

(i) List;
(ii) Set. [8]

(d) With reference to the class diagram in Appendix C, explain any changes to
the class to implement the following functionality using the list variable
defined below.

List<Artist> artists;
(i) a report showing all artist details in order of artist name;
(ii) a report showing all artist details in order of number of
paintings. [8]

(e) With reference to your answer to (d), explain any changes required if the
variable type is changed to:

Set<Artist> artists; [2]

(f) Explain the differences and use of an Iterator and a ListIterator. [2]

Q4
(a) With reference to the class diagram in Appendix H, explain the role of each
class. [10]

(b) With reference to the class diagram in Appendix H, for each statement
below explain why you think the statement is TRUE or FALSE.

(i) the abstract class can be instantiated;

(ii) the interface can be instantiated;

(iii) the repository class needs to implement all the methods


specified in the interface. [6]

(c) Explain the following concepts:


(i) coupling;

(ii) cohestion. [8]


(d) With reference to the class diagrams in Appendix A and partial class
definitions in Appendix B, explain how each of the following processes are
supported in NetBeans.

(i) version control;

(ii) documentation;

(iii) refactoring;

(iv) unit testing. [12]

(e) With reference to the interface definition in Appendix I, outline the


requirements of a class that implements the interface. [4]

(f) Identify FOUR standard functional interfaces available in Java describing a


way in which they could be used. [8]

(g) Using an application scenario of your choice identify ways in which you
might use the NetBeans support below:

(a) version control;

(b) documentation;

(c) refactoring;

(d) unit testing. [20]


Appendix A
A conference keeps track of registered attendees; some attendees volunteer for particular
roles.

Appendix B
Attendee
public class Attendee {
---1--- ---2--- int id;
---1--- String name;
---3--- int lastIdAllocated = 0;

public Attendee() {
this.id = ++lastIdAllocated;
this.name = "Unknown";
}

public Attendee(String name) {


this.id = ++lastIdAllocated;
this.name = name;
}

public int getId() {


return this.id;
}

public String getName() {


return this.name;
}

public void setName(String name) {


this.name = name;
}

---4---
public String toString() {
return "Id: " + Integer.toString(this.id) +
" Name: " + this.name;
}
}
Volunteer
public class Volunteer ---5--- Attendee {
private String role;

public Volunteer() {
---6---;
this.role = "TBA";
}

public Volunteer(String name, String role) {


---7---;
this.role = role;
}

public String getRole() {


return this.role;
}

public void setRole(String role) {


this.role = role;
}

---4---
public String toString() {
return super.toString() + " Role: " + this.role;
}
}

Appendix C

Appendix D

1,"Leo Messi"
2,"Andres Iniesta","Greeter"
3,"Sergi Busquets"
4,"Carles Puyol","TBA"
Appendix E

public Repository load(String filename) {


Repository repository = new Repository();
try (BufferedReader br =
new BufferedReader(new FileReader(filename))) {
String[] temp;
String line = br.readLine();
while(line!=null){
temp=line.split(Character.toString(DELIMITER));
int id = Integer.parseInt(temp[0]);
String name = stripQuotes(temp[1]);
if (temp.length > 2) {
String role = stripQuotes(temp[2]);
Volunteer volunteer = new Volunteer(id, name,
role);
repository.add(volunteer);
}
else {
Attendee attendee = new Attendee(id, name);
repository.add(attendee);
}
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName())
.log(Level.SEVERE, null, ex);
}
return repository;
}

Appendix F

private void store(String fileName) {


char DELIMITER=",";
try (ObjectWriter output = new ObjectWriter()) {
for (Attendee attendee:attendeeList) {
output.print(attendee.toString(DELIMITER));
}
output.close();
} catch (FileFoundException ex) {
Logger.getLogger(
DAOTextImpl.class.getName()).log(Level.SEVERE,
null, ex);
}
}
Appendix G

public Repository load(String filename) {


Repository repository = new Repository();
try (BufferedReader br =
new BufferedReader(new ---1---(filename))) {
String[] temp;
String line = br.readLine();
while(line!=null){
temp=line.split(Character.toString(DELIMITER));
String artistName = stripQuotes(temp[0]);
int noPaintings = ---2---(temp[1]);
---3---<Painting> paintingsList = new ---3---<>();
Painting newPainting;
for (int i=0; i<noPaintings; i++) {
line = br.readLine();
String title = stripQuotes(line);
newPainting = new Painting(title);
paintingsList.add(newPainting);
}
Artist artist = ---4---
---5---
---6---
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName())
.log(Level.SEVERE, null, ex);
}
---7---
}
Appendix H

Appendix I

public interface DAOInterface {

public Repository load(String filename);

public void store(String filename, Repository repository);


}
Marking Scheme
1
(a)
1. protected
2. final
3. static
4. override
5. extends
6. super()
7. super(name);

1 mark for identification; 1 mark for purpose

(b)
Answer should identify and explain the purpose of the components of a method definition
with reference to the code in Appendix B
Access modifier, method name, arguments, method body, void, return
- 1 mark per point

(c)
Answer should identify typical methods including constructors, getters, setters, toString,
equals, hashCode, collection methods, comparison methods
- 2 marks per method type identified with justification

(d)
1 mark per modifier
2
(a)
Filename supllied through parameter
Repository object created
Stream opened
Repetitively read lines until no more
Split line into temp String array using delimiter
Access first array location to retrieve id value and convert to Integer
Access second array location to retrieve name stripping off quotes
If more than 2 values in the temp array then
Access next location for role
Create Volunteer object and add to repository
Else
Create Attendee object and add to repository
EndIf
Close stream
Return Repository

IOException handling code

Mark flexibly – 1 mark per valid point


(b)
Model classes need to implement Serializable
Change stream to ObjectInputStream
Simply need to readObject and cast as Repository
Handle ClassNotFoundException
Remove unneeded code

1 mark per valid point


(c)
Method should be public
Missing model argument
An Object store implementation so no need for delimiter which uses " instead of '
writeObject method used to write object to file so no need for for loop
Update exception handler

1 mark each
(d)
1 – FileReader
2 – Integer.parseInt
3 – ArrayList or LinkedList
4 – new Artist(artistName, paintingsList)
5 – repository.add(artist)
6 - Line = br.readLine()
7 – return repository

2 marks each including explanation


(e)
"El Greco",2
"View Of Toledo"
"Portrait Of A Gentleman"
"Charles Burchfield",1
"999 (Circus Poster)"
1 mark off for each mistake
3
(a)
Answer should explain the purpose and organization (Interfaces, Implementations,
Algorithms) and identify benefits:
 Reduces programming effort
 Increases program speed and quality
 Allows interoperability among unrelated APIs
 Reduces effort to learn and to use new APIs
 Reduces effort to design new APIs
 Facilitates software reuse

Mark flexibly - 1 mark per valid point


(b)
Implementation of hashCode() and equals() methods to allow comparison
Implement Comparable interface and compareTo() method
Create Comparator with compare() method

Answer should explain the points above with reference to the reports requested
1 mark per valid point
(c)
1 mark each for type explanation
2 marks each for implementations e.g. ArrayList, HashSet etc.
1 mark each for algorithm examples
(d)
Implementation of hashCode() and equals() methods to allow comparison
Implement Comparable interface and compareTo() method
Create Comparator with compare() method

Answer should explain the points above with reference to the reports requested
1 mark per valid point
(e)
No essential difference to comparison implementation, however collection implementation
should be a TreeSet
- 1 mark per valid point
(f)
Explanation of difference, additional methods etc.
- 1 mark per valid point

4
(a)
Classes:
App, Controller, InputHelper, Repository & Interface, DAO Implementations & Interface,
Model Classes
The answer should explain the purpose of each type of class and interface describing their
role in the app's implementation
1 mark per valid point
(b)
(i) False (ii) False (iii) True
2 marks each including explanation
(c)
Answer should explain coupling and cohesion
1 mark per valid point – mark flexibly
(d)
3 marks per topic – mark flexibly
(e)
Class implements interface providing concrete methods for the load and store methods with
same method signatures. Additional methods can be added where necessary.
1 mark per valid point
(f)
Predicate, Supplier, Consumer, Function
2 mark per interface with explanation and use
(g)
Answer should reference chosen scenario
5 marks per topic – mark flexibly

You might also like