2004FCExamQuestions - CompSci 101 THE UNIVERSITY OF...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci 101 THE UNIVERSITY OF AUCKLAND CONTINUED First Semester, 2004 Campus: City and Tamaki COMPUTER SCIENCE Principles of Programming (Time allowed: TWO HOURS) Surname: Forenames: Student ID number: Login name (UPI): 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)
Image of page 1

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

View Full Document Right Arrow Icon
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"); (2 marks) b) What is printed by the following? String s = "How now brown cow?"; System.out.println(s.substring(s.lastIndexOf("ow")-1)); (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) ; } }
Image of page 2
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()); } } (5 marks)
Image of page 3

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

View Full Document Right Arrow Icon
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. Parameters: ch - the character to be tested. Returns: true if the character is uppercase; false otherwise. java.lang Class String java.lang.Object java.lang.String public char charAt (int index) Returns the character at the specified index. An index ranges from 0 to length() - 1 . The first character of the sequence is at index 0 , the next at index 1 , and so on, as for array indexing. Parameters: index - the index of the character. Returns: the character at the specified index of this string. The first character is at index 0 .
Image of page 4
Question/Answer Sheet - Page 5 - CompSci 101 SURNAME: ...................................................... FORENAMES: ...........................................................
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern