CSC108H-m07-part1 - (6‘ UNIVERSITY OF TORONTO O Faculty...

Info iconThis preview shows pages 1–10. 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 DocumentRight 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 DocumentRight 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 DocumentRight 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 DocumentRight 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 DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (6‘ UNIVERSITY OF TORONTO O Faculty of Arts and Science 194 April—May 2007 EXAMINATIONS (0‘3‘ 4 C80 108HIS v9 0 - ‘0 Instructor: H. Ghaderl I4, Duration — 3 hours Examination Aids: None Student Number: I I I I I I I I I I Last (Family) Name(s): First (Given) Name(s): Do not turn this page until you have received the signal to start. (In the meantime, please fill out the identification section above, and read the instructions below carefully.) MARKING GUIDE #1; /20 # 2: / 20 This final examination consists of 7 questions on 18 pages (in- cluding this one). When you receive the signal to start, please make # 35 _/ 25 sure that your copy of the examination is complete. # 4. / 25 Comments are not necessary except where we ask for them. ' *‘ Write your student number in the space provided at the bottom # 5: / 25 of both sides of every page of this exam. # 6: / 25 # 7: / 20 TOTAL: /160 Good Luck! Student #:I I I I I I I I I Pagel CONT’D... CSC 108H FINAL EXAMINATION April 2007 Question 1. [20 MARKS] Part (a) [4 MARKS] Write a single statement that declares and instantiates a 2x3 array of String objects called months, and initializes its first row to "Jan", "Feb", and "Mar"; and its second row to "Apr" , "May"y aIld "Jun". Part (b) [6 MARKS] Given a 2D float array gridNums, write 2 statements that declare integer variable rowN and colN and initializes them to the number of rows and columns of gridNums, respectively. Then, write a 3rd statement that prints the element in the last row and the last column of gridNums. Assume the array is not empty. Part (c) [4 MARKS] Assume that you have a class Car. Write a class called SportCar that customizes the Car class, and contains a double instance variable called wheelDiameter, and an integer instance variable called totalSportCars that will store the total number of SportCar objects ever created (do not write a constructor or any other method!) . Part (d) [6 MARKS] Given a StringTokenizer object called words and integer variable countIs, write a code using while loop that stores in countIs the number of tokens that are equal to "Is". StudentgiéfizI , , , I , , , I . Page2of18 CONT’D... April 2007 FINAL EXAMINATION CSC 108H Question 2. [20 MARKS] Some of the code fragments below have errors. For each fragment: 0 If the fragment has compile error, then place a checkmark \/ in the first column. 0 If the fragment compiles, but has run-time error, then place a checkmark f in the second column. a If there is no error, then in the third column write down the value of the requested variable. If there is an error do not write in the last column. If you know there is no error, but you are unsure of the result, then place a checkmark in the last column. You should assume all neccessary import statements have been done. Also, assume all code fragments are independent of each other. Code Fragment Compile Run-time Result, error? error? if successful? String c; c’s value: c="Easy"+“Exam" . substring(2, 3) = new int [5] ; e’s value: 0; i = 0; i < 5; i++) a[i] * a[i+1]; StringTokenizer st ; f ’5 value: st=new StringTokenizer( "Thisisatest" , "ai" ,true) ; int f = st.countTokens(); Iiiiliiiillllll else int [1 a new int [5]; - i’s value: -_i; a[0] = new int[5] int n = 50+~10*2-+1; j’s value: long j = n; int g = a[0] [0]; String s = new String[10] ; boolean h = s [0] .equals(""); int i= 10; if (i++ < 11) i++ - Student#:I , I I I , . I . , Page3of18 CONT’D... CSC 108H FINAL EXAMINATION April 2007 Question 3. [25 MARKS] Part (a) [20 MARKS] Complete the following method: public class Exam { /** GiVen an unsorted array of integers that may contain duplicate values, return an * array with all these elements but duplicate values removed. The relative order of * elements in the result MUST remain the same as input. For example, for input * array {1,3,8,3,2,22,2,3,1,2,19,17}, the result should be array {1,3,8,2,22,19,17}. * You may assume that there is at least one element in the array. */ public static int removeDuplicates(int a) { Student #:I I I I I I I I I I Page4of18 CONT’D... April 2007 FINAL EXAMINATION CSC 108H Part (b) [5 MARKS] Complete the JUnit test method below, by writing four meaningful assertEquals calls to test removeDuplicates(int) fionlpart(a) public class ExamTester extends TestCase { public void testRemoveDuplicates() { Student#:I I I I I I I I I I Page50f18 CONT’D... CSC 108H FINAL EXAMINATION April 2007 Question 4. [25 MARKS] Complete the following methods (all three methods are in the same class). You will be marked on correct- ness, and design. Part (a) [6 MARKS] /** compute the inner product of given two arrays, i.e. the sum of product of corresponding * elements. E.g., the inner product of {1, 5, 2} and {10, 4, 10} is 1*10+5*4+2*10 == 50. * Assume arrays a and b have the same length and have each at least one element. */ public int innerProduct(int a, int b) { Part (b) [6 MARKS] /** return (as an int array) the elements on column i of int matrix m1 (columns are * numbered from 0) . Assume m1 is a matrix with at least i+1 columns. */ public int getColumn(int m1, int i) { StudentalfizI , , l l . I I , I Page6of18 CONT’D... April 2007 FINAL EXAMINATION CSC 108H Part (c) [13 MARKS] /** Given int matrices m1 and m2, return the product of m1 and m2. If m1 is an MxK * matrix and and m2 is a KxN matrix then their product is an MxN matrix where each * element [i,j] is the inner product of ith row of mi and jth column of m2. If the * number of columns of m1 is NOT the same as the number of rows of m2, they are NOT * multiplicable, in this case your method must return null. Assume m1 and m2 are valid * matrices with at least one element. * You should use the "innerProduct" and "getColumn" methods from part (a) and (b). */ public int product(int m1, int m2) { Student#:. I I I I I I I I Page7of18 CONT’D... CSC 108H FINAL EXAMINATION April 2007 Question 5. [25 MARKS] Complete the following method. You may assume that all necessary import statements have been made. /** Create a new file called "out.txt" that contains the lines of input file fName, but certain * lines are processed and moved to the end as follows: if a line starts with "MoveToEnd:" then * it (after discarding the header "MoveToEnd:") is moved to the end of the file. * The relative order of moved lines MUST be preserved. Here is an example: * ------- -- input file: *this is A *MoveToEndzthis is 3 *this is C *this is D *MoveToEndzthis is E *this is F * The method must return the number of moved lines (so, for the above example return 2). * Assume the input file with name fName exists and there is no IO error. */ public int moveToEnd(String fName) throws IDException { //continue on the next page if needed ... Student-#2:. . . . . . , l I . Page80f18 CONT’D... April 2007 FINAL EXAMINATION CSC 108H } / / end of moveToEnd method Student #:, . . I , I . I I , PagerflS CONT’D... CSC 108H FINAL EXAMINATION April 2007 Question 6. [25 MARKS] Complete the following methods. Part (a) [12 MARKS] There is a very fast sort method called merge sort. The idea is to break the list from middle, sort each half, and then merge the two sorted lists. Here, you only implement the merge part. /** * Given two sorted arrays x and y, return a sorted array by merging x and y into a single * sorted array. For example, the result of merging {1,3,5,11} and {2,3,7,11,25} is * {1,2,3,3,5,7,11,11,25}. Assume x and y have at least one element and are both sorted. */ public static int merge(int x, int y) { Part (b) [13 MARKS] /**Return the location of an item in the sorted matrix In by doing BINARY search. * A matrix is called sorted if laying out all the rows one after another results in a * sorted flat list. Here is an example of a sorted 3x4 matrix: *11 13 14 16 *19 25 4O 41 *80 83 94 97 *(the flat list is {11, 13, 14, 16, 19, 25, 40, 41, 80, 83, 94, 97} which is sorted) Student #:I I I I I I I I I I Page 100f18 CONT’D... ...
View Full Document

Page1 / 10

CSC108H-m07-part1 - (6‘ UNIVERSITY OF TORONTO O Faculty...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online