Sam Gold
Posts: 6
Nickname: g0dfather
Registered: Dec, 2006
|
|
Re: December 14 JAVA... bijhan here
|
Posted: Dec 14, 2006 1:19 PM
|
|
Part A: Key Concepts [10 marks]
1. Explain why one should be cautious in using recursion and iteration within each other? [2 marks]
2. Why is it important that a tree be complete for an arrayList implementation? [2 marks]
3. Explain how protected visibility can be equivalent to public visibility. [2 marks]
4. ____, which means many forms or shapes, enables the JVM to determine which method to invoke at run time? [1 mark]
0 Polymorphism.
5. Which of the following statements is correct? [1 mark]
0 A precondition is an assertion about the state of the program before a method is executed. 0 The catch clause need not follow a try block. 0 When an exception is thrown, the flow of execution is suspended and continues at the appropriate try clause. 0 You cannot use a throw statement in a lower-level method to indicate that an error condition has been detected.
6. Analyze the following pseudo code. [1 mark]
1. class Test 2. function main() 3. inner = new Inner() 4. print(inner.k)
5. class Inner 6. protected int k;
0 The program has a syntax error because the Inner class does not have a constructor and you cannot create an object from it. 0 The program has a syntax error because the Inner class is private and it cannot be accessed in the main method. 0 The program has a syntax error because k is protected in the Inner class and it cannot be accessed in the main method. 0 The program has a syntax error because the Inner class is not static and it cannot be used to create an object in the main method.
7. Implement the method hasNext() for the class Iter. [2 marks]
8. Which of the following is the inorder traversal of a binary tree? [1 mark]
0 Traverse TL, traverse TR, visit root node. 0 Visit root node, traverse TL, traverse TR. 0 Traverse TL, visit root node, traverse TR. 0 Visit root node, traverse TR, traverse TL. 9. Draw the tree that would be formed by inserting the words from the following sentence into a binary search tree “java is much like drinking coffee” [2 marks]
10. Generic methods, like generic classes, have parameters. [1 mark]
0 True. 0 False. Part B: Exercises [25 marks]
1. Write a recursive method for the SingleLinkedList class shown below that has the following signature int countItem(<E> item). This method returns the number of times that item is located in the SingleLinkedList. You can assume that the items support an equals method. Note: The method int countItem(<E> item) is a wrapper class to the actual recursive method. [10 marks]
public class SingleLinkedList<E> { private Node<E> head; private int size;
public SingleLinkedList() { head = null; }
private class Node<E> { private E value; private Node next;
private Node(E obj) { value = obj; next = null; } private Node(E obj, Node next) { value = obj; this.next = next; } } }
2. Complete the code below using an iterator to achieve the following output … (0.0,0.0), (0.0,1.0), . . . , (0.0,4.0), . . . , (4.0,4.0). [5 marks]
List doubles = new ArrayList<Double>(); for (int c=0; c<5; c++) doubles.add(new Double(c));
// Include code to print output by using an iterator
3. Create the UML diagram for the following code [10 marks]
public class Customer { ArrayList accounts = new ArrayList();
public Customer() { Account defaultAccount = new SettlementAccount (); accounts.add(defaultAccount); } }
public abstract class Account { public abstract void deposit(float amount); public abstract void withdraw(float amount); }
public class SettlementAccount extends Account { private float balance = 0; private float limit = 0; private float debt = 0; float availableFunds() { return (balance + limit - debt); } public void deposit(float amount) { balance = balance + amount; } public void withdraw(float amount) { if (amount > this.availableFunds()) { throw new InsufficientFundsException(); } balance = balance - amount; } }
4. For the pseudo code below, what is the output? Make certain to use the line numbers in the pseudo code as reference to any traces. [5 marks]
1. main throws Exception 2. print ("m: -1-") 3. A (3) 4. print ("m: -2-") 5. 6. function A (integer i) throws Exception 7. if (i <= 0) 8. throw Exception ("0 or negative value") 9. else 10. print ("a(",i,"): -1-") 11. A (i-1) 12. print ("a(",i,"): -2-");
5. Show for the steps of sorting the array below using the Heap sort algorithm. For each step list the number of comparisons and exchanges. [7 marks]
6. Show an adjacency list and adjacency matrix representation of a graph for the map shown below from the block defined by Spadina, Grange, Grange Park, and Stephanie. Comment on which representation has better storage efficiency for this example. [8 marks]
Use the following abbreviations for the streets. Do not consider any streets not listed below.
Sp – Spadina, Hu – Huron, GrPl – Grange Pl, Be – Beverly, McC – McCaul, GrA – Grange Ave, Su – Sullivan, St – Stephanie, Jo – John St.
Street intersections can be represented by combining the street abbreviations with a dash. For example the intersection of Spadina and Grange Ave. is Sp-GrA. Consider street intersections as nodes. Use the following index mapping to create your graph.
1. Sp-GrA 2. Sp-Su 3. Hu-GrA 4. Hu-Su 5. GrPl-GrA 6. GrPl-Su 7. Su-Be 8. Be-St 9. St-Jo 10. GrA-Be
One way streets are represented by the blue arrows. The transition in the one way direction for Grange Ave. and Sullivan occurs at Huron St. The transition in the one way direction for Huron St. occurs at Grange Ave.
|
|