{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

aps105s_2004_exam - 1 UNIVERSITY OF TORONTO FACULTY OF...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

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

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

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

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

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

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

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

View Full Document Right Arrow Icon
Background image of page 8
Background image of page 9

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

View Full Document Right Arrow Icon
Background image of page 10
Background image of page 11

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

View Full Document Right Arrow Icon
Background image of page 12
Background image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1! UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APSIOS — Computer Elndamentals Final Examination —— April, 2004 Examiner: John Carter Duration: 2.5 h Exam Type: A This is a. “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 insufficient, 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 13 pages, including this one. The marks allocated to the questions, out of a. total of 135, are shown in the question headings. You must use the Java programming language to answer programming questions. Unless specifically directed otherwise, you may use any of the methods from the Math, String, and In classes. Name Student Number ________________. ecf login l. [10 Marks] Circle the correct answer f0r 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: A chat value is stored using 16 bits. In Java, the expression 5 < n < 2 will have the value false for any value of the int. variable 11. If we fail to write a toString method for a class, Java will provide one auto- matically. In Java, one cannot create an array of objects, only an array of references to objects The declaration int list = new int[10]; autOmatically initializes the elements of list. Methods that have recursive calls are always slow. For a sequential search to work properly, the list must be sorted. Multiple constructors for an object can be created provided that they each have a different return type. Selection sort is always faster than bubble sort. Array indexes in Java must always start at zero. 2. [‘20 Marks] Each part of this question is worth two marks. (a) Evaluate (int) 2.5 * 4 7. 3 (b) Write a statement that will assign to the int variable x a random value in the set {5,10,15,20}. (C) Write a Java expression whose value is sin 10°. ((1) What is meant by the scope of an identifier? (e) 'Ilraee a binary search as it seeks the value 70 in the array called list shown below. To show your traoe, print the value of the array element examined at each stage of the search. “manna mmmfl (f) Suppose that an array initially contains the values {8,2,6,9,5}. If the array is to be sorted into ascending order using insertion sort, Show the contents of the array after each of the first two passes of the sort. (g) Suppose that an array initially contains the values {5,7,3,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 first two passes of the SOrt. (h) Suppose that an array initially contains the values {15,23,14,19,11 28,12}. If the array is to be sci-ted into ascending Order using quicksort as discussed in class, show the contents of the array after the first pass of the sort. (i) What is the primary difference between an instance field and a class field? (j) The diagram shows a binary tree. In what order would the nodes be visited by a postorder traversal? [l5 Marks] Complete the definition of the method natLog that computes the value of 1n :5 without using the log method from the Math class. Instead, the method should use the following power series: 55—1 (1—1)2 (az— 1)3 (3—1)‘1 13: =——————+_———+... “( l 1 2 3 4 The formula is valid for 0 < I S 2. If a: is Outside this range, your method should print the message: argument out of range and return the value zero. In computing the value of lnm for valid values of :5, your method should continue to add terms of the power series until it reaches a term whose absolute value is less than 10‘”. Your method should not use any method from the Math class other than Math. abs. public static double natLog (dOuble x) 4. [15 Marks] A circle can be defined as a set of points in a plane that are a given distance (the radius) from a given point (the centre). Suppose we want to create a class Circle whose objects represent circles in the Cartesian plane. Each Circle object will have fields: private double x; // x-coordinate of centre private double y; // y-coordinate of centre private double r; // radius of circle (3.) Write a constructor method for the Circle class. The constructor should have three parameters representing the coordinates of the centre and the radius. It should create a new Circle object with the given centre and radius. (b) Write a toString method for the Circle class. For the circle with centre at (2.5, 3.0) and having radius 6.25, the method should return the string: "circle: centre (2.5.3.0) and radius 6.25" (c) Write an instance method area that returns the area of its implicit Circle object, reunded to two decimal places. (d) Write an instance method isIneide with one explicit Circle parameter. The method should return true if and only if the implicit Circle parameter is entirely within the explicit Circle parameter. (e) Write a class method inContact with two Circle parameters. The method should return true if and onlyr if the two circles have any points in common. 5. [15 Marks] Complete the definition of the method indefo whose heading is shown below. The method is similar to the indexOf method discussed in the text except that the second parameter is a string instead of a character. If pattern is a substring of s, then indexOf should return the index in s at which the first occurrence of pattern begins. If s does not contain pattern, then indexOf should return —1. If either 5 or pattern are empty strings, tuen indexOf should return —1. You may use any of the methods from the String class that are discussed in the text but you may not use any others. As examples, indexOf ("Hississauga" , "133") should return 1 indefo("Calgary" , "gar") should return 3 indefo("Toronto" , "orb") should return —1 indexflf ("0ttaua" ,”") should return —1 public static int indexOf (String :3, String pattern) { [15 Marks] Suppose that an array price contains a sequence of daily closing prices of a stock. We define a rally as a period of time in which the closing price of the stock either increases or stays the same. Complete the definition of the method bestRally so that it returns the difference in the price of the stock for the rally having the largest price increase. If the stock price never rises, the method should return the value zero. public static double bestRally (double price) 7. [7 Marks] Consider the recursive method whose definition is: public static int :6 (int n) { int result = 0’ if (n != 0) if (n X 10 == 0) result = f(n/10); else result = 1 + f(n-1); return result; } (a) Evaluate f(3). (b) evaluate f (203). (c) State in a few words (no more than ten) what f (n) determines. 8. [8 Marks] Complete the definition of the recursive method reverse so that it returns the reversal of its argument. For example, reverse (”Indie") Should return the string "eian". Your method must be recursive. No credit will be given for a. non—recursive solution. public static String reverse (String s) 10 [15 Marks] Suppose that linked lists are represented in the usual way seen in class with List objects containing a field: private Node head and Node objects containing the fields: int info; Node link; Assuming that lists are maintained in increasing order of info values with no repetitions, complete the definition of the method printUnion (of the Class List) 50 that it prints, in ascending order, the info fields that would be contained in the union of the two lists, one value per line, with no repetitions. Your solution should not modify either list, write or call any other methods except System. out_println, or create any new objects. (The union of two lists contains the elements that are only in the first list. only in the second list, or in both lists.) public void printUnion (List other) 11 10. [15 Marks] Complete the definitions of the methods pr intRange 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 which all info fields are unique. The methods should print the values of the info fields that fall in the range 10H 3 info 3 high. Values should be printed one per line. The methods should visit as few nodes as possible. class Tree { private Node root; public void printRange (int low, int high) { } class Node { int info; Node 1Child; Node rChild; void printRange(int low, int high) { 12 Extra Space Please specify which questionh‘) you are answering on this page. 13 ...
View Full Document

{[ snackBarMessage ]}