This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: H i i UNIVERSITY OF TORONTO
FACULTY OF APPLIED SCIENCE AND ENGINEERING APSIOSF — Computer Fundamentals
Final Examination — December, 2001 Examinem: John Carter. Baochun Li, James Madam Duration: 2.5 h 0 Examination Type A; This is a “closed book” examination; no aids are permitted.
 Calculator Type 4: No electronic or mechanical computing devices are permitted.
0 Write your answers in the spaces provided. You must. answer using a pen. 0 If more space is required, blank pages are provided at the back of the examination.
0 Rough work, if neoasan', can be done on the backs of the pages. I This examination has 15 pages. a You must use the Java programming language to answer programming questions. 0 You may use any methods from the Math and String classes. 0 You may assume that the following Stdin methods are available:
getIntO, getFloatC), getDoubleO, getStringC), and getCharO. LOIOI L0102 L0103 L0104
Circle your lecture section: Carter or Li or MacLean or Carter
W RF MTR
Name
Student Number __.—___—___ BCF Login H 1. [20 Marks]
Each part of this question is worth two marks. Answcrs should be brief; point form is permitted. (a) Write the simplest possible expression that is equivalent to the expression
!(x <= y a: 3; <= 2) (b) What does it mean to declare a ﬁeld as being private? (c) What two properties are common to a recursive description of any process? (d) Trace a binary search as it seeks the value 70 in the array called list shown below. To show your
trace. print the value of the angry element examined at each stage of the search. "IIImun 1m . 39 4 45 53 58 51 62 6 72 77 84 86 95
"um“II (9) Suppose that an array initially contains the values {8, 4, 2, 7, 3}. If the array is to be sorted into
ascending order using an insertion sort, show the contents of the array as it would appear after each
of the ﬁrst two passes of the insertion sort. U (f) Use big oh notation to wn'te expressions for the time complexity of selection sort: binary search (3) Find the base 10 representation of IAEIS. (h) Write 58 as a. base 3 numeral. (i) The diagram shows a binary tree. In what order would the nodes be visited by a preorder traversal? \o I docf (j) Draw the binary search tree that. would be produced by inserting the values Jerry, Elaine, George
Cosmo. and Newman (in the given order) into an initially empty tree. (Assume that. the tree is to be
maintained so ihal its entries are in dictionary order.) 1 l H II 2. [15 Marks] Complete the deﬁnition of the method maxCount whose header is shown below. The method should return
the number of local marima in the array list. A local maximum is deﬁned to be either a single value or a
sequence of equal values greater than both the preceding and following values. For example, the sequence
3,7,5,9,9,9.8,2.4 has two local maxima: 7 and 9. public static int maJtCount (int list) H '  H t D [15 Marks]
Complete the deﬁnition of the method delete so that it returns a. string in which any occurrences of any
characters in the string pattern that appear in the string source have been deleted. For example, if the
string 3 bad the value "Toronto", then, after the call a = deleteCa."to");
a would have the value "'l'rn". public static String delete (String source, String pattern) m d I l I l H [15 Marks} Assume that we have an array, a. whose elements are of type int. Using mansion, complete the deﬁnition of
the method butterflySearch to search for a speciﬁc integer key in the array. If the key exists, the method
returns true; otherwise it returns false. The strategy of searching for the key is as follows: we ﬁrst compare the ﬁrst integer with the key, then
compare the last integer, then the second, then the second last, and so on. We progress in this order until
the key is found or all integers have been examined. In other words. for a list with 11 items, we use the
foliowing search sequence: aEO] , aha1]. an] . aha2] . a[2] . aim3]. Elia], aim4} . a[4] , aEnS], ... Hint: You may need to write a supplementary method for this purpose. Marks will not be given if you do
not use recursion. public static boolean butterflySearch (int [1 a, int key) II I l H l l H (J! [15 Marks] Write the complete deﬁnition of class Circle. Within this class. deﬁne an inner class, Point. that contains
the twodimensional coordinates of a point. (x ,y), which are declared as instance ﬁelds of type double. In
the Circle class, the centre of the circle is an object. of the class Point, and the radius of the circle is
represented as a ﬁeld of type double. Both ﬁelds are private instance ﬁelds. Complete the deﬁnition of the
inner class Point and the class Circle with the following methods. Give your answer on both this and the
next page. Write a constructor of the class Point, taking two double parameters to initialize the coordinates of
the point. Write a method distance of the class Point, taking an object of class Point as a parameter. This
method should return a double value that shores the distance between the two points. Write a constructor of the class Circle, taking three double parameters to initialize a circle object.
Write a double valued instance method area, that returns the area of its implicit Circle object. Write a class method selectSmaller that can be called by a statement such as
c3 = Circle.select3mallar(c1, c2); where :1, c2 and c3 are objects of type Circle. The method should make :3 represent the smaller of
the circles represented by c1 and :2 (or c1 if c1 and c2 are the same size). Write a boolean valued instance method islnside that may be called by a statement such as
boolean contained = c1.isInside(c2); The method should return true if :1 is entirely inside c2 and return :talae otherwise. You may take
advantage of the distance method that is already deﬁned in the class Point. *1 This page should be used for your answer to Question 5. H ll 6. [1.5 Marks} Suppose that linked lists are maintained using the class List and the inner class Node whose ﬁelds are shown
below. class List { private Node head; class Node
1'
int info;
Node link;
}
} Suppose further that all linked lists are maintained in nondeceasing order. Write an instance method
simplify that deletes any duplicate items in a list. If, for mample, before calling Bimpl ify, a list contains
13 15 15 17 17 17 19 22 25 25 28
than, after simplify has been called, the list should contain
13 15 17 19 22 25 28 1 I ll [15 Marks] A priority queue is a queue in which each elemait has a priority. Two rules govern the removal of elements
from a. priority queue: o elements having the same priority are removed from the queue in the order in which they were added
to the queue and o the removed element will always have the highest priority of all the elements waiting in the queue. Write a class Pﬂueue that implements a priority queue where each element stores a string and has a priority
value in the range of 0 through 4 (inclusive), with 4 being the highest priority. Speciﬁcally, you must write
methods to enqueue an element, dequeue an element, and check if the queue is empty. You may also require
a constructor. You may make use of the class Queue, which has the following ADT, in your solution: claae Queue {
public void enqueue (String 3);
public String dequeua C); public boolean isEmpty O;
l 10 it H 8. I15 hiarkﬂ A parse tree is a binary tree used to store an arithmetic expression to be evaluated. For example, represents the expression 3 t (4 + 5) + 42/2 — 3. Write a method named evaluateO in the class parseTree
to evaluate the expression stored in a parse tree ((19. 45 for the parse tree shown above) assuming: 1. each leaf node contains a String representing an integer (you can convert the string to an actual
integer using int Integer.pa:selnt(8tring 3)). and 2. each internal node contains one of "7+”. "", "*” or "/” and intemal nodes always have two children,
and 3; you may declare additional methods if needed, and 4. the class Parse'I‘ree is deﬁned as class ParaeTrea { class Node {
String data ;
Node left ;
Node right ; } private Node root ; } You may use this space. and the top of the next page for your answer to this question. If your solution
contains more than one method, indicate clearly the class to which cad] method belongs. 11 H 9. This space can be used for your answer to Question 8. [5 Marks] Shown below is a tree where the nodes have not. yet, been labelled. A post order traversal of this tree gives
the output 1 2 3 4 5 6 7 8 9. Label the tree nodw with the appropriate numbers. I l U 10. [10 Marks] A ”Primary Tree is a tree in which each node can have 0, 1‘ 2 or 3 children. Complete the method
pr intPostOrder below so that. it prints 3 postorder traversal of the elements stored in a Lrinary tree deﬁned
by class Trinary. If your solution contains more than one method, indicate clearly the class to which each
method belongs. class Trinary
{
class Node
{
int data ;
Node left ;
Node middle :
Node right
} private Node root : publ i c void pr intPostOrdar () l3 Extra space Please indicate clearly which question“) you am amwenng on this page. 14 H 1! Extra space Please indicate clearly which quastionﬁ) you are answering on this page. ...
View
Full Document
 Fall '08
 Maclean

Click to edit the document details