This preview shows pages 1–12. 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 APSlOS —— Computer Fundamentals
Final Examination — April, 2002 Examiner: John Carter Duration: 2.5 h Exam Type: A
This is a “closed book” examination; no aids are allowed. Calculator Type: 4
N0 calculators are allowed. All questions are to be answered on the examination paper. If the space provided for a question is
insufﬁcient, extra pages are provided at the end of the examination If you use these pages, please
indicate clearly what you have done. The examination has 12 pages. The marks allocated to the questions, out of a total of 140, are shown in the question headings. You must use the Java programming language to answer programming questions. You may use any
of the methods from the Math, String, and Stdin clas'Ses. Name Student Number __—_____ eef login __—____ 1. [20 Marks] Each part of this question is worth two marks. (23.) Given that x is of type double and n is of type int, write an expression that would assign to n
the value of x, rounded to the nearest integer. (b) Write a statement that. will assign to the int variable guess a random value in the range
1 S guess 3 10‘ (c) How many asterisks will be printed by the following fragment?
for (int i 3; i > —5; i);
System.out.println( ’ **’); (d) What is the difference between a class and an object? (6) Trace a binary search as it Seeks the value 60 in the array called list shown below. To shew
your trace, print the value of the array element examined at each stage of the search. 9""‘_10.11 12 13 14
55 61 66 73 75 84 (f) Suppose that an arm}r initially contains the values {5, 4, 1, 6, 2}. If the array is to be sorted
into ascending order using selection sort: Show the contents of the array as it would appear after
each of the ﬁrst two passes of the sort. (g) What is the difference between a ﬁeld that is declared to be public and a ﬁeld that is declared
to be private? (h) What is a stack? (i) Evaluate the postﬁx expressiOn: 2 5 x 8 4 + 3 +  (j) Convert. the expression (6 + 4) + (8  2 x 3) to preﬁx form. !\D [12 Marks] Ackermann’s functzon: a(m,n) is deﬁned for nonnegative integers m and n as follows: 7‘; + 1 if m = 0
a(m,n)= ohm1,1) ifm>0ancln =0
a(m — r, a(m,n — 1)) otherwise 1. Evaluate each expression. You must Show your work for full credit. (a) a“): 0) (b) 641,0) (c) a(1,1} (d) a(2,1) 2. Complete the deﬁnitiorr of the method whose header is show below so that it returns the value
of Ackermann‘s function. Assume that the values of In and n are. valid. public static int a (int m, int n) 3. [8 Marks] Suppose that we want a class Dog in which each dog has a name, a breed, and an age (in years). The
ﬁelds of the class are private String name;
private String breed;
private int. age; (a) Write an appropriate constructor with three parameters. (b) Write an equals method for the Dog class. Two Dog objects should be considered equal if the
breeds are the same and the ages differ by one year or less. (c) Write a toString method for the class. As an example, for an object representing a three year
old Samoyed named “Toby”, the method should return "Toby  Breed: Samoyed, Age: 3" 01 4. ['20 Marks]
Complete the deﬁnition of the method printStars whose heading is Sl'lOWIl below. The method should
print a pattern of asterisks and blanks that is height lines high and width spaces wide. The pattern
should be similar to that on the flag of the United States. For example, the call printStarsCS ,9) ;
Shoqu print a pattern like the following: *i kill *
*ikit it
* #120: it it
**t t ****t Notice that the ﬁrst character on the ﬁrst line should be an asterisk not a blank. If either height or
width are less than one, the method should print nothing. public static void printStars (int height, int width)
{ [’20 Marks] Complete the deﬁnitiorl of the method compress so that it returns a modified versiorn of its String
parameter, s. The modiﬁed string should contain the ﬁrst occurrence of each character in s but all
repeated occurrences should be removed. For example, if s = "Here is the sample string"= then
the method should return the string “Her isthampltng” public static String compress (String s) { [20 Marks] Lacsap’s Triangle is a triangular array of numbers in which each row starts and ends with the row
number and each interior value is the sum of the two values on either side of it in the preceding row
The following diagram shows the ﬁrst ﬁve rows of Lacsap‘s Triangle. 1
2 2
.3 4 3
4 T 7 4
5 11 14 11 5 Write a method that has a single int parameter, n. The method should print the values in the ﬁrst
n r0ws of Lacsap’s Triangle. If n < 0, the method should print nothing. For simplicity, do not try to
print the triangle in the symmetrical form shown here. [‘20 Marks] The nodes of a linked list contain points in the Cartesian plane, with each node in the list having the
following ﬁelds: class Node {
char label; // a single letter such as ’A’, ’8’, etc.
int x; // x—coordinate of point
int y; // ycoordinate of point
Node link;
} The Node class is an inner class of the class List that has a single ﬁeld called head, a reference to the ﬁrst node in a list.
You are to complete the deﬁnition of the method shortestSegment so that it returns the string containing the endpoints of the shortest line segment that can be formed using> the points in a list.
For example, if the points with labels ’8’ and ’E' are closest, the method should return either "BE"
or "EB". You may assume that the distances between all pairs of points are different, Your method
should not create any new data structure. public String shortestSegment () [20 Marks] Complete the deﬁnitions of the methods print whose headers are shown below. The methods should
print the contents of a binary tree in parenthesized form as follows: (<Ieft SubtrEE>) <item> (<right Subtree>)
Here, <eft Subtree> is the contents of the left subtree printed in parenthesized form, <itern> is the
info ﬁeld of the root, and <right subtree) IS the contents of the right subtree printed in parenthesized
form. An empty tree (or Subtree) should produce no output. As an example, the tree would be printed as: ((4)2)1((5)3(6)) class Tree { private Node root; public void print ()
{ } class Node { int info;
Node 1Child;
Node rChild; void print 0
{ 10 Extra Space Please spemfy which question you are answering on this page. 1] Extra Space Please specify winch questzon you are answeran on this page. 12 ...
View Full
Document
 Fall '08
 Maclean

Click to edit the document details