2004FCExamAnswers - CompSci 101 THE UNIVERSITY OF AUCKLAND...

This preview shows pages 1–5. Sign up to view the full content.

CompSci 101 THE UNIVERSITY OF AUCKLAND CONTINUED First Semester, 2004 Campus: City and Tamaki COMPUTER SCIENCE Principles of Programming (Time allowed: TWO HOURS) Surname: MODEL Forenames: ANSWERS Student ID number: 123456789 Login name (UPI): abcd001 INSTRUCTIONS: Attempt ALL questions, calculators are NOT permitted There is space at the back for answers that overflow the allotted space Examiner to complete: Question Mark Question Mark 1 (/9) 8 (/11) 2 (/5) 9 (/8) 3 (/5) 10 (/10) 4 (/5) 11 (/6) 5 (/5) 12 (/6) 6 (/10) 13 (/5) 7 (/5) 14 (/10) TOTAL: 100 (/100)

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

View Full Document
Question/Answer Sheet - Page 2 - CompSci 101 SURNAME: . ..................................................... FORENAMES: . .......................................................... CONTINUED Question 1 (9 marks) a) What is printed by the following? System.out.println(2 + 3 * 6 + "7 * 8"); 207 * 8 (2 marks) b) What is printed by the following? String s = "How now brown cow?"; System.out.println(s.substring(s.lastIndexOf("ow")-1)); cow? (2 marks) c) Consider the class FishRecord below: public class FishRecord { // instance variables private String type; private int amount; //default constructor public FishRecord() { type = ""; amount = 0; } public FishRecord(String inputString, int inputNum) { type = inputString + " " + inputNum + "\n"; amount = inputNum; } public void addRecord(String inputString, int inputNum) { amount += inputNum; type = type + inputString + " " + inputNum + ",\n"; } public String toString() { return(type + "total: " + amount) ; } }
Question/Answer Sheet - Page 3 - CompSci 101 SURNAME: . ..................................................... FORENAMES: . .......................................................... CONTINUED What is the output of the application Q1 which uses FishRecord ? public class Q1 { public static void main(String[] args) { FishRecord record1 = new FishRecord("guppy", 8); System.out.println(record1.toString()); FishRecord record2 = new FishRecord(); record2.addRecord("betta", 5); record2.addRecord("catfish", 10); System.out.println(record2.toString()); } } guppy 8 total: 8 betta 5, catfish 10, total: 15 (5 marks)

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

View Full Document
Question/Answer Sheet - Page 4 - CompSci 101 SURNAME: . ..................................................... FORENAMES: . .......................................................... CONTINUED Question 2 (5 marks) Write a static method called keepUpperCase() which accepts a String as a parameter and returns a new String containing only the characters from the original String which were uppercase. For example, if the following code was executed: System.out.println(keepUpperCase("Hello World")); The result would be: HW The following sections of the Java API might be useful: java.lang Class Character java.lang.Object java.lang.Character public static boolean isUpperCase (char ch) Determines if the specified character is an uppercase character.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 24

2004FCExamAnswers - CompSci 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document
Ask a homework question - tutors are online