This preview shows pages 1–13. Sign up to view the full content.
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 Document
Unformatted text preview: UNIVERSITY OF TORONTO
FACULTY OF APPLIED SCIENCE AND ENGINEERING APSlOSF — Computer Fundamentals
Final Examination — December, 2002 Examiners: John Carter, Baochun Li, James IVIacLean Duration: 2.5 h 0 Examination Type A: This is a “closed hook" examination: no aids are permitted.
0 Calculator Type =1: No electronic or mechanical computing devices are permitted.
0 Write your answers in the spaces provided. Please answer using a pen. 0 If more space is required, blank pages are provided at the back of the examination.
0 Rough work. if necessary. can be done on the backs of the pages, a This examination has 13 pages. 0 You must use the Java programming language to answer programming questions.
0 You may use an},r methods from the Math and Strmg classes. 9 You ma}r assume that the following methods of the class In are available:
getlnt, getLong, getFloat. getDouble. getString, and getChar. L0101 L0102 1.0103 L0104 Circle your lecture section: or or _ or
MacLean Carter L: Carter Name Student, Number .____ ECF Login ._ IVI ARKS Question ‘
Value ‘ 1 . l20 Marks} Each part of this question is worth two marks. Answers should be brief: point form is permitted. (a) Given that x is of type doubler write a statement that will round it to one decimal place. (b) Simplify the expression 1(x != y M x != 2) as much as possible. (c) Rewrite the following fragment. using a for statement. 1 — 10,
do
{ i—, System.out.println(i*i); }
while (i > 0); d) Suppose that an array that initially contains the values {5. 4, 7, 2 , 3} is to be sorted into ascending
Order usmg a selection sort Show the contents of the array as it would appear after each of the ﬁrst
two lieLsses of the selection sort. (0) Shown belmv is a Java method for adding iiititriees. Give a bigOh estinmto for the task of executing
this method, assuming the matrices are square \"llll 71 rows and 7? columns. public static doublel] sum (double U a. double{] b) {
double c = new doubleEa.length][alO].length];
for (int i = O; i < a.longth; 1++)
for (int j = 0; j \ aEG].1ength; J++)
Cli][j] = a[i][33 + bli][J];
return c;
} (f) Suppose that a method that is known to have time complexity 0(122) requires '20 time units 1.0 process
a list of size 500. Estimate the Lime that would be required by the method to process a similar list of
size 2000. (g) What is a stack? (h) The diagram shows a binary tree. State the order in which the nodes of the tree would be visited by
a postorder traversal. /’\.
5k
/ \ (i) The diagram bel0w Shows a binary search tree containing the elements ‘1. T, and 9. Draw diagrams to
Show all other possible binary search trees containing these values. 7 / \
4 9 (j) Find the base 10 representation of 21339;? (a: l‘J £10 Marks}
Suppose, that we want to create a class Temp‘w‘orker in which each object has the following ﬁelds? private String name;
private boolean experienced;
private int wpm; The ﬁeld experienced is true if and only if the worker is experienced with computers while the ﬁeld uprn is
the worker’s typing speed in words per minute, {a} Write an appropriate constructor with three parameters. (b) Write an equals method for the class. Two TempWOrlrer objects should be considered to be equal if
(1) they are both experienced with computers or {23) neither one is experienced with computers and
their typing speeds differ by less than ﬁve words per minute. (c) Write a toString method for the claws. As an example, {Or a worker named George who has experience
with computers and types at a rate of 55 wpm, the method should return:
“George  experienced (yes)  55 upm"
1f the worker is not experienced with computers the word no should appear inside the parentheses. 3. {15 hiarke
Consider the following method: public static int mystery (mt [1 list, mt start, int fmish)
{
int result;
System.out.println{”Range; ” + start * ” “ + finish);
if (start == flnish)
result = llst[start];
else {
int middle = (start + flnishl/Q;
result = Math.max(mystery(list,start,middle),mystery(llst,middle+1.finlshll; } System.out.print1n("Result: " + result);
return result; } What would be printed by execution of the following statements? intU a = {5,7,9,2};
Systom.out.println(mystery(a,0,3)); \'_,'l I! I  [5 Marks} Assume that we have an array, a, whose elements are of type. intr Complete the deﬁnition of the method
duplicateSearch to search for a speciﬁc integer key in the array. Rather than simply searching for the
location of the key, we wish to know if there exist duplicates of the key in the array. If exactly two copies of
the. same integer key exist, the method returns true; otherWise it returns false. For example. if we. perform duplicateSearch for the key 5 in an array {5 , 4 , 3 , 1 , 5, 1, 1}. the method returns true. If we perform dupllcateSearch for the key 4‘ 6 or 1 with the some army, the method returns
false, public static boolean duplicateSearchunt [J a. int key) Cl [10 Marks} Using recursiori. complete the deﬁnition of the quickSort method, sorting an array of integers (type int)
in increasing order using quicksort. When choosing the pate: in each pass, randomly choose an item among
items to be processed in that pass. In your solution: you may assume the existence of a method partition
with header
public static int partition (int list, int low, int high) The partition method rearranges the elements of the subarray of list from index 10'. 1.0 index high so
that the eleu'ient, I, that. was at. listﬂou] is new in a posiLiOH so that all elemean preceding it are less
than or equal to a: and all elements following are greater than :23; The value returned by partition is the
inch of I after the partition has been performed. public static void quickSortUnt list) l [15 Marks] Suppose that linked lists are represented in the usual we}: seen in class with List. objects Containing a ﬁeld
private Node head; and llode objects containing ﬁelds:
int 1nfo; Node link;
Assuming that. lists are maintained so that their int ﬁelds are in increasing order with no repetitions. complete the deﬁnition of the method intersectSum so that it returns the sum of the info ﬁelds: of the
elements that appear in both the implicit. List object and the 1151. referred to by other, AS an example: if
the implicit List object. Contains 3 I1 6 7 and the list referred to by other mntaim 1 3 6 8, then the method
Should return the value 9 (because the lists contain the common elements 3 and 6). public int intersectSum (Llst other) “ml [15 Marks] Two strings are said to he anagrams of one another if thoy contain the same characters in a (possibly)
different order. For example. "the morse code“ is an anagram of "here come dots", and "elvis" is an
anagram of "lives". Complete the deﬁnition of the method isAnagram that returns true if and only if its
two String parameters are anagrams of one another. public static boolean isAnagram (Stung 51, String $2) £15 Marks] Sometimes it is necessary to perform antlmuetic on integers larger than thosa that can be represented by Java‘s long data type. One way to do this for positive integch is to store them in arrays; with one digit stored in each element {with the most signiﬁcant digit Stored in the. ﬁrst, array olerncnt and the least signiﬁcant digit stored in the last array element). For example, the number 139 could be stored in the array 1 by writing
int [1 x = {1,5,2}: Complete the deﬁnition of a method named addBig that takes two int arrays as parameters and “adds”
them, returning an army that. contains the sum of the numbers stored in the input arrays. Note: Your solution may return an army with a single loading ‘0‘. You may assume that. the elements of the
input arrays are all single digits and that the input arrays have no leading zeros public statlc int addBig(int :2, 1nt n) 10 '93 [15 Marks] Assuming that. binary trees are implemented as thta}r were in ciass. curnplem thc deﬁnition of the method
countVals for the Tree class that returns the number of occurrences of key in a binary tree that stores
integers in each node. If your ansu'm requires more than one method, indicate clearly the class to which
each method belongs; public int. count‘Jalshnt key) 11 10. {15 h‘iarks] \‘l’e can (:all a. binary tree “perfectly balanced" if. for every noclo in the tree, the node's left and right subtree
have the same height” \Vrite a method that. returns true if a binary tree is perfectly balanced. and false
otherwise. In your solution. you should not Worry about the deﬁnition of 1110 height of a subtree. Instead,
yOu should simply assume the existence of a method height with header Static int height (Node :1)
that returns the height of a subtree with :00! no If your answer requires more than one method. indicate clearly the class to which each method belongs. Extra space Please indwatc clearly which questwnh] you arc answering on this page. 13 ...
View
Full
Document
This note was uploaded on 10/16/2010 for the course ECE APS105 taught by Professor Maclean during the Fall '08 term at University of Toronto Toronto.
 Fall '08
 Maclean

Click to edit the document details