York University all 2001 / est #1 Department of Computer Science COSC1020.01 his is a closed book, 90-minute test. ill in your personal data below and wait. You may use pen or pencil but answers written in pencil will not be re-marked. Once the test starts, verify the number of pages and write your name on each. Last Name Return the marked test to (circle one): irst Name A 14:30MW D 19:00 H 12:30MW Login cs B 13:00/R E 19:00W J 15:30MW York ID C 10:30MW G 14:30R K 19:00M String Methods (invoke on a string s) boolean equals(string t) Returns true if s and t have equal contents. int indexof(string t, int f) Looks for the string t within s, starting at position# f in s. Returns the position in s where the match was found. Returns -1 if no match was found. int indexof(string t) Looks for the string t within s (as above), starting at the beginning of s. String substring(int f,int t) Returns all characters in s with position numbers f and < t. String substring(int f) Returns a substring of s that begins at f and extends to the end of s. String replace(char x,char y) Returns a string with all occurrences of character x in s replaced by y. String toupper/lowercase() Returns a string of all characters in s converted to upper / lower case. char charat(int p) Returns the character at position# p in s. int compareo(string t) Returns a negative number if s<t, zero if s=t, and a positive number if s>t. Integer.parseInt(s) Double.parseDouble(s) Static methods to convert a string s that contains a number to a primitive type. String trim() Returns the same content as s but with any leading/trailing white-space removed. Static methods in Math double abs(double x) Returns the absolute value of x. double pow(double x, double y) Returns x raised to y. double rint(double a) Returns the closest double value to a that is equal to a mathematical integer. Do not write below this line A (30%) B (30%) C (40%) O
GROUP - A <30 points > here are 6 questions in this group each containing 5 statements. Circle if a statement is true, if it is false, and leave blank if you re not sure. You get +1 for each correct circle, 1 for each wrong one, and 0 for blanks. Your total mark in any of the 6 questions is never less than zero. Note that there could be no true statement or more than one true statement in each question. A.1 he API of a class: a. lists all private fields and methods in the class b. specifies, for each listed field, whether it is static or not c. specifies, for each listed method, whether it is static or not d. specifies the data type of each listed field e. specifies the return type of each listed method A.2 he output of the following statement is 8 a. York.println("7+1"); b. York.println(Integer.parseInt("7") + 1); c. York.println("7" + 1); d. York.println(7 + 1); e. York.println(Math.pow(2,3)); A.3 A constructor: a. does not have to have the same name as its class b. does not have to have parameters c. must appear in the API of its class d. does not have a return type in the API e. can be overloaded COSC1020.01 / ALL 2001 ES #1 Pg. 2 of 10
GROUP - A, continued A.4 he following statements are syntactically correct: a. int k = -5(2 + 8); b. int k = 3 2.5; c. int k = 5 / 2; d. double d = 5.5 / 3 * 2.5; e. double d = "5.5"; A.5 In Java: a. static fields cannot be changed once set b. if x,y,z are boolean variables, then the following boolean is always true: x && y == (x && z) && (y && z) c. he statement: double avg = (a + b) / 2; correctly computes the average of two initialised int variables a and b. d. he expression a*b*c/d is evaluated as ((a*b)*(c/d)) e. If a and b are two initialised int variables, then the following statement will generate a syntax error: boolean x = a < b; A.6 Assume that Quark is a valid class and that it has a default constructor. Assume also that x and y are declared to be of type Quark. a. Whenever the boolean (x!= null && x == y)is true, the boolean x.equals(y) must also be true b. Whenever the boolean (x == y)is false, the boolean x.equals(y) must also be false c. x = new Quark(); y = new Quark(); imply that the boolean (x == y)must be true d. he statement: x = new Quark(); may assign null to x. e. he statement: y = y + x; will generate a syntax error COSC1020.01 / ALL 2001 ES #1 Pg. 3 of 10
GROUP - B <30 points > here are 6 multiple-choice questions in this group with a number of answers listed under each. Put a checkmark in the box to the right of the one that best answers the question. You get +5 if you checked the correct answer, and zero if you checked the wrong one or checked more than one box per question. B.1 int age = 20; String s = "oo Young"; String t = age + " " + s; s.touppercase(); York.println(s + " " + t); he output of the above Java fragment is: a. oo Young 20 oo Young b. OO YOUNG 20 OO YOUNG c. OO YOUNG 20 oo Young B.2 he API of the Quark class indicates that it has a default constructor and lists an int field called charm. Quark x = new Quark(); Quark y = new Quark(); Quark z = new Quark(); x.charm = 5; y.charm = 7; z.charm = x.charm; x = y; y.charm = 10; York.print("" + x.charm + y.charm + z.charm); he output of the above Java fragment is: a. 101010 b. 5105 c. 10105 COSC1020.01 / ALL 2001 ES #1 Pg. 4 of 10
GROUP - B, continued B.3 int x = 3; int y = 27; if (x % 5 == 0) { y = y 7; } else if (x > -5) { y = y / 7; } else if (x == 3) { y = y + 3; } York.println(y); he output of the above Java fragment is: a. 3 b. 30 c. 20 B.4 int x = 5; for (int y = 2; y < 7; y++) { x = x + y; } York.println(x); he output of the above Java fragment is: a. 5 b. 19 c. 10 COSC1020.01 / ALL 2001 ES #1 Pg. 5 of 10
GROUP - B, continued B.5 Assume that the file data.txt exists, and that it has just one record (line) that contains the word: york YorkReader yr = new YorkReader("data.txt"); String rec = YorkReader.readLine(); York.println(rec); he output of the above Java fragment is: a. york b. data.txt c. 1 B.6 he API of the String class includes the following method: int lastindexof(string str, int fromindex) Returns the index within this string of the last occurrence of the specified substring str. he returned index indicates the start of the substring, and it must be equal to or less than fromindex. If str occurs one or more times as a substring within this object at a starting index no greater than fromindex, then the index of the first character of the last such substring is returned. If it does not occur as a substring starting at fromindex or earlier, -1 is returned. String str = "Each of them offered to pay it off"; int k = str.lastindexof("of", 18); York.println(k); he output of the above Java fragment is: a. -1 b. 5 c. 13 COSC1020.01 / ALL 2001 ES #1 Pg. 6 of 10
GROUP - C <40 points > C.1 <20 points> Given a portfolio object reference pf, write a code fragment that performs the following task: if the portfolio has exactly one investment, compute the difference between the current and the book value of the portfolio, and then output a sentence that depends on the computed difference: if the difference is positive, say 12545.55, then output: Your profit is: $12,545.55 Otherwise (for negative or zero difference), output: You didn't make any profit. On the other hand, if the given portfolio has more than (or less than) one investment, your fragment must terminate the program and issue an appropriate error message like "Assertion ailure. Portfolio must have a single investment". Your output must have the same layout / format as the samples shown above. Recall that the API of the Portfolio class includes: Investment getirst() Determine the first investment in this portfolio. Investment getnext() Determine the next investment in this portfolio. int getcount() Determine the number of investments in this portfolio. And that the API of the Investment class includes: double getbookvalue() Determine the book value of the stock held in this investment. Stock getstock() Determine the stock held in this investment. int getqty() Determine the quantity of the stock held in this investment. And that the API of the Stock class includes: String getname() Determine the name of this stock. double getprice() Determine the price of this stock. String getsymbol() Determine the symbol of this stock. Write your fragment on the next page. COSC1020.01 / ALL 2001 ES #1 Pg. 7 of 10
C.1, continued Write the fragment here assuming pf refers to a valid Portfolio object: COSC1020.01 / ALL 2001 ES #1 Pg. 8 of 10
GROUP - C C.2 <20 points> Write a program that reads a string from the user containing a date in the form: d/m/y and outputs the corresponding day name, exactly as shown in the following log: Enter the date: 21/10/2001 his is a Sunday. You can assume that the input is valid (a day number followed by a slash followed by a month number followed by a slash followed by the year) and that it represents a valid date. Note that there is a period immediately after the day name in the output. Note also that the day/month in the entered string may be made up of one digit. October 1 st, 2001, for example, is entered as 1/10/2001, not 01/10/2001, and May 1 st of that year is entered as 1/5/2001, not 01/05/2001. Your program must use the services of the DiaryPage class whose API is shown (in part) below: Constructor Summary DiaryPage(int d, int m, int y) Construct a diary page object having the passed day d (1 d 31), month m (1 m 12), and year y (y>0). Method Summary int getdayofweek() Determine the day of the week of this diary page object. Return an integer between 1 and 7, with 1=Monday, 2=uesday, and 7=Sunday Higher marks will be given to programs that don't use selection (such as if or switch statements), make proper use of the language constructs, and respect the style guide. Nevertheless, partial credit will be given to all correct programs. Write the program on the next page. COSC1020.01 / ALL 2001 ES #1 Pg. 9 of 10
C.2, continued import york.*; public class C2 { public static void main(string[] args) { COSC1020.01 / ALL 2001 ES #1 Pg. 10 of 10