aps105f_2001_exam - H i i UNIVERSITY OF TORONTO FACULTY OF...

Info icon This preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
Image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: H i i UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APSIOSF — Computer Fundamentals Final Examination — December, 2001 Examine-m: 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 field 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. "III-mun 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 first 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 doc-f (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 definition 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 defined 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 definition 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 definition of the method butterflySearch to search for a specific 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 first compare the first 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] , aha-1]. an] . aha-2] . a[2] . aim-3]. Elia], aim-4} . a[4] , aEn-S], ... 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 definition of class Circle. Within this class. define an inner class, Point. that contains the two-dimensional coordinates of a point. (x ,y), which are declared as instance fields 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 field of type double. Both fields are private instance fields. Complete the definition 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 defined 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 fields are shown below. class List { private Node head; class Node 1' int info; Node link; } } Suppose further that all linked lists are maintained in non-deceasing 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 Pflueue 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. Specifically, 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 hiarkfl 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 defined 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 defined 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 quastionfi) you are answering on this page. ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern