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 APS105 —— Computer Fundamentals
Final Examination — April, 2005 Examiner: John Carter Duration: 2.5 h Exam Type: A
This is a4 “closed book” examination; no aids are allowed. Calculator Type: 4
No calculators are allowed. All questions are to be answered on the examination paper. If the space provided for a question is
insufﬁcient, extra space is provided at the end of the examination. If you use this extra space, please
indicate clearly which question(s) you have answered there. The examination has 12 pages, including this one.
The marks allocated to the questions, out of a total of 130, are shown in the question headings. You must use the Java programming language to answer programming questions. Unless speciﬁcally
directed otherwise, you may use any of the methods from the Math, String, and In classes. Name Student Number _—_____.___________._ ecf login MARKS nnn— To—al iIIIIIIIIII 1. [10 Marks] Circle the correct answer for each of the following statements. Each correct answer is worth one mark.
Each incorrect answer will result in a deduction of one mark. (a) True or False: (b) True or False: (c) True or False: ((1) True or False: (e) True or False: (f) True or False: (g) True or False: (h) True or False: (i) True or False: (j) True or False: The part of a computer that performs arithmetic is called the ALS. The output of the Java compiler is known as Java script. A double value is stored using 32 bits. The relational expression "sam" . compareTo( " sum") > 0 evaluates to true. Methods may have more than one return statement. Java arrays are always indexed from zero. A method is said to be overloaded if it has more than one parameter. The value of the expression "Toronto" . substring(2) . index0f( ’0’) is l. A queue is a linear list in which all insertions and deletions are done at one
end of the list. Any algorithm used to traverse a binary tree must be recursive. 2. {20 Marks] Each part of this question is worth two marks. (a) Evaluate: ((int) 1.2*7) / 4 1 .
(b) Write —e"‘m2 as a Java expressmn. M (c) Write a statement that will assign to the int variable rounded the value of the double variable
x rounded to the nearest 100. As an example, if x = 7654.321, then the statement should assign
rounded the value 7700. (d) Simplify the expression !(p != q && r >= s) as much as possible. (e) Trace a binary search as it seeks the value 54 in the array called list shown below. To show
your trace, print the value of the array element examined at each stage of the search. "numanua
mum (f) Suppose that a class Person has been deﬁned. Write a fragment that will create an array of 20
Person objects. (g) Suppose that an array initially contains the values {6,8,9,2,4}. If the array is to be sorted
into ascending order using selection sort, show the contents of the array after each of the ﬁrst two passes of the sort. (h) Suppose that an array initially contains the values {27,40,54, 13,60, 15,20}. If the array is
to be sorted into ascending order using quicksort as discussed in class, show the contents of the array after the ﬁrst pass of the sort. (i) Linear lists can be stored in arrays or linked lists. Give one advantage of linked lists over arrays
and one advantage of arrays over linked lists. (j) The diagram shows a binary tree. A preorder traversal of the tree, printing values at the nodes,
would print those values in the following order: 9 8 2 6 4 7 3 1. Place the values in their correct positions in the tree. 3. [15 Marks] Write a Java method checkSum that has a single int parameter n which you may assume is positive.
The method should return the value found by adding the ﬁrst, third, ﬁfth, . . . digits of n and subtracting
the second, fourth, sixth, . . .digits of n. The table below gives examples. ‘ n Evaluate Return
827463 8—2+7—4+6—3 12
5 5 5
73 7 — 3 4
142 1 — 4 + 2 —1 4. [10 Marks] Suppose that we want to create a class Pet in which each object has the following ﬁelds: private String species; // "cat", "dog", etc.
private int age; // in years
private char sex; // ’F’ or ’M’ (a) Write an appropriate constructor with three parameters. (b) Write an equals method for the class. Two Pet objects should be considered equal if they are
of the same species, have the same sex, and have ages no more than one year apart. (c) Write a toString method for the class. Sample strings for objects of the class are: "3 year
old male fish" and "5 year old female dog". 5. [15 Marks] Complete the deﬁnition of the method longestMord whose header is shown below. The method should
return the longest word in the string s. Assume that 5 consists only of words separated by single
blanks and that there are no blanks at the beginning or end of s. If there is more than one word of the maximum length, return the ﬁrst one. public static String longestWord (String s) [15 Marks] Complete the deﬁnition of the method occurrences whose header is shown below. The method should
return a count of the number of times that the sequence stored in the array pattern occurs in the array list. As an example, if pattern = {4,7,4} and list = {1,4,7,4,7,4,3,8,4,7,4} then the method
should return the value 3. public static int occurrences (int pattern, int list) [5 Marks] A function f is deﬁned for nonnegative integers m and positive integers n as follows: m ifm<n
f(m,n)= 0 ifm=n
f(m—n,n) ifm>n (a) Find the value of each expression. i. f(2, 5) ii. f(7, 1) iii. f(11,4) (b) State in a few words (no more than ten) the value of f (m, n). [10 Marks] A palindrome is a string that is identical to its reversal. As examples, "radar", "deed", and "$$* *$$"
are all palindromes. Write a deﬁnition of a recursive method isPalindrome that returns true if and
only if its single String parameter s is a palindrome. Note that your method must be recursive; no
credit will be given for a non—recursive solution. 9. [15 Marks] Suppose that linked lists are represented in the usual way seen in class (as outlined below). Assuming
that lists are maintained in increasing order of info ﬁelds and that all entries in a list are unique,
complete the deﬁnition of the method intersection so that it returns the list that contains the items
found in both the implicit List object and the list other. If the lists have no elements in common,
the method should return a List object whose head ﬁeld has the value null. You should not assume
the existence of any other methods. class List { private Node head; class Node {
int info;
Node link; Node (int 1, Node n)
{
info = 1;
link = n;
}
} public List intersection (List other) 10 10. [15 Marks] A binary search tree can be organized in ascending order with all keys in the left subtree of any node
less than those in the right subtree but it can also be organized in descending order with all keys in
the left subtree greater than those in right subtree. Complete the deﬁnitions of the methods reverse
whose headers are shown below. The methods should assume that the tree on which they are operating
is organized as a binary search tree in ascending order by info ﬁelds. The methods should alter the tree so that it becomes a binary search tree organized in descending order. class Tree { private Node root; public void reverse 0 { } class Node {
int info;
Node lChild;
Node rChild; void reverse () { ll Extra Space Please specify which question(s) you are answering on this page. 12 ...
View
Full Document
 Fall '08
 Maclean

Click to edit the document details