{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2006S2ExamQuestions - COMPSCI 101 THE UNIVERSITY OF...

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

View Full Document Right Arrow Icon
COMPSCI 101 THE UNIVERSITY OF AUCKLAND CONTINUED SECOND SEMESTER, 2006 Campus: City COMPUTER SCIENCE Principles of Programming (Time allowed: TWO hours) NOTE: Attempt ALL questions Write your answers in the space provided There is space at the back for answers that overflow the allotted space No calculators are permitted Surname: Forenames: Student ID number: Login name: Q1 (/20) Q4 (/12) Q7 (/9) Q10 (/10) Q2 (/10) Q5 (/8) Q8 (/6) Q3 (/5) Q6 (/10) Q9 (/10) TOTAL (/100)
Image of page 1

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

View Full Document Right Arrow Icon
- 2 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 1 (20 marks) There are 10 parts to this question, each worth 2 marks. Write your answer in the space provided for each part. What is “printed” by each of the following pieces of Java program? a) String s = "sesquipedalian"; System.out.println(s.substring(6,11)); System.out.println(s.substring(s.length()-2)+"tic"); (2 marks) b) int i = 4; int j = 6; System.out.println(i + "+" + j+ " == " +(i+j)); (2 marks) c) int i=6, j=3; int k = j/i; System.out.println(k > 0.3); double d = ((double)j)/((double)i); System.out.println(d > 0.3); (2 marks) d) int x = Math.min(Math.max(5, 4), 3); int y = Math.max(Math.min(5, 4), 3); System.out.println(x); System.out.println(y); (2 marks)
Image of page 2
- 3 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED e) String[] s1 = { "hay", "bee", "sea"}; String[] s2 = { "horses", "mutton", "swimming"}; for (int i=0; i<s1.length; i++) { System.out.println(s1[i] + " for " + s2[i]); } (2 marks) f) int[] numbers = {2,4,10,6,1,2,3,4}; int i = 0; while ((numbers[i] % 2) == 0) { System.out.println(numbers[i]); i = i+1; } (2 marks) g) int i = 6, j = 10; if (i < 8) { if (j < 15){ System.out.println( " Line 1"); } else { System.out.println("Line 2"); } } System.out.println("Line 3"); (2 marks)
Image of page 3

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

View Full Document Right Arrow Icon
- 4 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED h) int answer = 0; for (int i = 1; i <= 4; i++) { answer = Math.max(10*i,20); System.out.print(answer + " " ); } (2 marks) i) String[] s = {"You", "have", "no", "chance", "of", "your", "best", "final", "exam", "ever", "performed"}; for (int i=0; i<s.length; i++) { if (s[i].length()==4) { System.out.print(s[i]+ " " ); } } (2 marks) j) for (int i = 10; i>0; i--){ if ((i%2) == 0){ System.out.println((12-i)/2); } } (2 marks)
Image of page 4
- 5 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 2 (10 marks) Complete each of the methods below. a) // method to decide whether the first parameter String is // longer than the second, assuming neither String is null private boolean longer(String s1, String s2){ } (2 marks) b) // method to decide whether a double number passed as a // parameter may be cast to an int that represents // exactly the same value private boolean isExactlyTheSameAsAnInt(double d){ } (2 marks) c) // method to produce a new String that is the same as the // String parameter s, but with the character at // position i deleted (assume i is a valid position in // the String) private String delete(String s, int i){ String firstPart = s.substring(0,i); } (2 marks) d) // method to shift its int array parameter, a, so that // a[i] is set to the value of a[i+1] for all a[i]. The //last element in the array is set to 0 private void shiftLeft(int[] a){ } (2 marks)
Image of page 5

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

View Full Document Right Arrow Icon
- 6 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED e) // method to return a double array that is the sum of its // two double array parameters, i.e. sum[i] is set to // a1[i]+a2[i] (assume a1 and a2 have the same length) private double[] add(double[] a1, double[] a2){ double[] sum = new double[a1.length]; } (2 marks) Question 3 (5 marks) Below is the definition of the class Horse . What is the output from the following code that makes
Image of page 6
Image of page 7
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