Masters programmes in Computer Science and Information Systems Object-Oriented Design and Programming
Sample module entry test — xxth December 2013
This sample paper has more questions than the “real” paper will have.
Allocated time: 90 mins 18:15–19:45
• Candidates should attempt ALL XX questions on the paper.
• You are advised to look through the questions before commencing your answers to assist in planning your strategy.
• Simplicity and clarity of expression in your answers is important.
• Electronic calculators are NOT allowed.
• This is a closed book test.
• You should not use a computer of any type to assist in answering the questions (this includes mobile devices of any type).
Question: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Total
Marks: 2 5 6 6 8 10 4 4 12 8 15 16 16 3 4 6 125
Answer the questions in the spaces provided on the question sheets. If you run out of room for an answer,
continue on the back of the page.
Name:
User Id:
Write a switch statement to print the word even if the int variable number is even, or print the word odd if number is odd.
Question 2 . . . 5 marks For each of the following in an Employee class, state whether it should be static, instance, or local.
(a) An array of employees’ Social Security numbers.
(b) Their job title.
(c) A method to compute bonuses based on job titles.
(d) A variable used to hold the number of hours they have worked this week.
(e) A variable used to compute their additional pay for overtime work.
Question 3 . . . 6 marks
(a) (What is the purpose of the JDK? That is, what does having it allow you to do? 3 marks
(b) What is the purpose of the JRE? That is, what does having it allow you to do? 3 marks
You can compute the average of two numbers by adding them together and dividing by two.
(a) Write a method to compute and return the average of two double values. 2 marks
(b) Should the above method be static? Why or why not? 2 marks
(c) What will happen if you try to call the above method with two int values? 2 marks
Question 5 . . . 8 marks Every Person must have a name. Every Employee (which extends Person) must have an integer employeeID. Write a minimal constructor for Employee utilising best practices for encapsulation.
Two Book objects are “equal” if they have the same ISBN, where isbn is an instance variable of type String. Write a correct and complete equals method for Book objects, and indicate (with a @ annotation) that it overrides the inherited equals method.
Question 7 . . . 4 marks A class has a private instance int[] variable called position. Write a getter method for position.
(a) String s contains only digits. Write a single assignment statement to convert this 2 marks string to an integer and assign it to variable n.
(b) Can you use the access modifiers public and private for variables declared inside 2 marks a method? Why or why not?
Question 9 . . . 12 marks Consider the following code fragment.
1 ArrayList<String> list = new ArrayList<>();
2 list.add("P");
3 list.add("Q");
4 list.add("R");
5 list.set(2,"s");
6 list.add(2,"T");
7 list.add("u");
8 System.out.println(list);
What is printed as a result of executing the code segment?
[An extract from the API for the java.util.ArrayList class is given at the end of the paper.]
List the four access modifiers available in Java and explain what each means for the instance variable or method that it modifies.
What is the output of the following Java program?
1 public class SuperWriter {
2
3 public SuperWriter(int n) {
4 for (int i = 0; i < n; i++)
5 System.out.print("N");
6 }
7
8 public SuperWriter() {
9 System.out.print("W");
10 }
11
12 public static void main(String[] args) {
13 System.out.println("String...");
14 new Writer("ZYU");
15 System.out.println();
16 new SuperWriter(5);
17 System.out.println();
18 }
19
20 }
21
22 class Writer extends SuperWriter {
23 public Writer(int num, boolean val, String s1, String s2) {
24 super(num - 1);
25 System.out.print(val ? s1 : s2);
26 }
27
28 public Writer() {
29 System.out.print("I");
30 }
31
32 public Writer(String name) {
33 System.out.println("A");
34 System.out.println(name);
35 new Writer();
36 new Writer(2, (1 > 3), "BB", "CD");
37 }
38
39 }
Write a recursive method that takes a string and produces the following output (this is for the string "abcde").
abcde abcd abc ab a ab abc abcd abcde
Due to fire restrictions only a certain number of people are allowed to be in a building at the same time. Write a class Bods that will not permit more than N instances of Bods to be constructed. If an attempt is made to construct more than N Bods, then an appropriate error message should be printed and the program exited. You should also include a howManyBods method that returns the number of Bods constructed so far.
Suppose you get a NullPointerException for the following statement:
System.out.println(myFriend.spouse.getBirthday());
What can you say about the probable cause of the error?
Question 15 . . . 4 marks For whom should you write internal (// and /*...*/) comments, and what kind of information should you convey?
Question 16 . . . 6 marks Answer either part (a) or part (b), but NOT both.
(a) Draw the memory diagram for the array that results from the following declaration:
int[ ][ ] foo = new int[ ][ ] { { 3, 1, 4 }, {1, 5, 9 }, { 2, 6 } };
i. System.out.print(1 + 2 + "buckle my shoe" + 3 + 4 + "close the door");
ii. System.out.print("hello".length() == 5 ? "yes" : "no");
iii. System.out.print(new char[] { ’a’, ’b’, ’c’ });
iv. int x; System.out.print(x = 5 + 7 * 2);
v. char[] chs = new char[] { ’a’, ’b’, ’c’ }; System.out.print(chs[1]);
ArrayList() Constructs an empty list with an initial capacity of ten.
boolean add(E elem) Appends the specified element to the
end of this list and returns true.
void add(int index, E element) Inserts the specified element at the position index in this list.
Object set(int index, E element) Replaces the element at position index with the specified element.
E get(int index) Returns the element at the specified
position in this list; throws an exception if the index is out of range.
E remove(int index) Removes the element at the specified
position in this list and returns the element removed.
int size() Returns the number of elements in
this list.