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: OODP entry test Page 1 of 12 c Birkbeck 2013
Question 1...................................................................... 2 marks 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 OODP entry test Page 2 of 12 c Birkbeck 2013
Question 4...................................................................... 6 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. OODP entry test Page 3 of 12 c Birkbeck 2013
Question 6..................................................................... 10 marks 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. OODP entry test Page 4 of 12 c Birkbeck 2013
Question 8...................................................................... 4 marks (a) String s contains only digits. Write a single assignment statement to convert this string to an integer and assign it to variable n. 2 marks (b) Can you use the access modifiers public and private for variables declared inside a method? Why or why not? 2 marks 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.] OODP entry test Page 5 of 12 c Birkbeck 2013
Question 10..................................................................... 8 marks List the four access modifiers available in Java and explain what each means for the instance variable or method that it modifies. OODP entry test Page 6 of 12 c Birkbeck 2013
Question 11.................................................................... 15 marks 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 } 40 OODP entry test Page 7 of 12 c Birkbeck 2013
Question 12.................................................................... 16 marks 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 OODP entry test Page 8 of 12 c Birkbeck 2013
Question 13.................................................................... 16 marks 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. OODP entry test Page 9 of 12 c Birkbeck 2013
Question 14..................................................................... 3 marks 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 } }; OODP entry test Page 10 of 12 c Birkbeck 2013
(b) What is printed by each of the following? 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]); OODP entry test Page 11 of 12 c Birkbeck 2013
Extract from the API for java.util.arraylist<e> ArrayList() boolean add(e elem) void add(int index, E element) Object set(int index, E element) E get(int index) E remove(int index) int size() Constructs an empty list with an initial capacity of ten. Appends the specified element to the end of this list and returns true. Inserts the specified element at the position index in this list. Replaces the element at position index with the specified element. Returns the element at the specified position in this list; throws an exception if the index is out of range. Removes the element at the specified position in this list and returns the element removed. Returns the number of elements in this list. OODP entry test Page 12 of 12 c Birkbeck 2013