2010S2ExamAnswers - THE UNIVERSITY OF AUCKLAND SECOND...

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

View Full Document Right Arrow Icon
THE UNIVERSITY OF AUCKLAND SECOND SEMESTER, 2010 Campus: City COMPUTER SCIENCE Principles of Programming (Time Allowed: TWO hours) NOTE: You must answer all questions in this exam. No calculators are permitted Answer in the space provided in this booklet. There is space at the back for answers that overflow the allotted space. Surname Forenames Student ID Login (UPI) Q1 (/6) Q4 (/33) Q7 (/6) Q10 (/10) Q2 (/10) Q5 (/6) Q8 (/6) Q3 (/10) Q6 (/6) Q9 (/7) 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 - - CompSci 101 ID: ........................................................... 2 Question 1 (6 marks) a) Complete the method header for the following method (i.e. complete the first line of the method definition). The method, methodA() , is called in the following way: boolean result = methodA(““); private boolean methodA( String word ) { return word.length() == 0; } (2 marks) b) Complete the method header for the following method (i.e. complete the first line of the method definition). The method, methodB() , is called in the following way: methodB(123.45); private void methodB( double price ) { final double GST_RATE = 15.0; System.out.println (“GST included: $” + price * GST_RATE); } (2 marks) c) What is the output when the following code is executed? String name1 = “Java”; String name2 = name1.toLowerCase(); System.out.println (name2 == “java”); false (2 marks)
Image of page 2
Question/Answer Sheet - - CompSci 101 ID: ........................................................... 3 Question 2 (10 marks) a) What output is produced by the following code? boolean a = false; boolean b = true; boolean c = true; System.out.println(a && b || c); true (2 marks) b) Write a Java boolean expression which tests whether the int variable, age , is between 18 and 65 (both inclusive). age >= 18 && age <= 65 (2 marks) c) Write the statement which is the increment part of the following loop. String input = “temp”; while (input.length() > 0) { System.out.println(““ + input.charAt(0)); System.out.print(“Enter the word:”); input = Keyboard.readInput(); } input = Keyboard.readInput(); (2 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 - - CompSci 101 ID: ........................................................... 4 d) What output is produced by the following code? String a = new String(“Hello World!”); String b = new String(“Hello World!”); if (a == b) { System.out.println(“reference equal”); } else if (a.equals(b)) { System.out.println(“value equal”); } else { System.out.println(“not equal”); } value equal (2 marks) e) Assume that the array, numbers , is an array of int s. Write a for loop which prints every element of the array (one value on each line). int[] numbers = { … }; for(int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } (2 marks)
Image of page 4
Question/Answer Sheet - - CompSci 101 ID: ........................................................... 5 Question 3 (10 marks) a) Complete the forecastWeather() method which is passed an int parameter representing today’s temperature. The method returns an int value which is a forecast of tomorrow’s temperature. The forecast value is obtained by randomly adding or subtracting a random int value between 0 and 5 (both inclusive) to today’s temperature. For example, the following calls to the completed forecastWeather() method: System.out.println(forecastWeather(20)); System.out.println(forecastWeather(20)); System.out.println(forecastWeather(20)); might produce the output: 22 17 24 private int forecastWeather(int temperatureToday) { int temperatureTomorrow = temperatureToday; int number = (int) (Math.random() * 6);
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