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

Question/Answer Sheet CompSci101 THE UNIVERSITY OF AUCKLAND SUMMER 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 (/10) Q4 (/9) Q7 (/8) Q10 (/10) Q2 (/10) Q5 (/10) Q8 (/12) Q3 (/10) Q6 (/15) Q9 (/6) Total /100

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

View Full Document
Question/Answer Sheet - Page 2 - CompSci 101 ID: . .............................................................................. CONTINUED Question 1 (10 marks) a) What is the output when the following code is executed? String result = 1 + 1 + " + " + 6 / 5 + " 1 " + 8 / (2 + 1); System.out.println(result); 2 + 1 1 2 (2 marks) b) Give ALL the possible outputs when the following code is executed? int random = (int) (Math.random() * 4 + 3); System.out.println(random); 3, 4, 5, 6 (2 marks) c) Complete the output when the following code is executed. double a = 5.0; int b = (int) (2 + a / 2 + 0.5); a = a / 2; System.out.println("a: " + a + " b: " + b); a: 2.5 b: 5 (2 marks)
Question/Answer Sheet - Page 3 - CompSci 101 ID: . .............................................................................. CONTINUED d) Complete the output when the following code is executed. String moon = "Blue moon"; int position = moon.indexOf(" "); String word = moon.substring(position, moon.length() - 1); System.out.println("position: " + position + " word: " + word); position: 4 word: moo (2 marks) e) Complete the output when the following code is executed. String word = "Cosmic"; int position = word.length() / 2; word = word.substring(0, position); word = word.charAt(0) + word; System.out.println("position: " + position + " word: " + word); position: 3 word: CCos (2 marks)

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

View Full Document
Question/Answer Sheet - Page 4 - CompSci 101 ID: . .............................................................................. CONTINUED Question 2 (10 marks) a) What is the output when the following code is executed? int number = 45; for (int i = 70; i > number; i=i-5) { System.out.print(i + " "); number = number + 5; } 70 65 60 (2 marks) b) Rewrite the following code using a while loop. int number = 45; for (int i = 70; i > number; i=i-5) { System.out.print(i + " "); number = number + 5; } int number = 45; int i = 70; while(i > number) { System.out.print(i + " "); number = number + 5; i = i - 5; } (2 marks)
Question/Answer Sheet - Page 5 - CompSci 101 ID: . .............................................................................. CONTINUED c) What is the output when the following code is executed? String word = "HEADACHE!"; char letter; int position = word.length() - 1; while (position > 2) { letter = word.charAt(position); System.out.print(letter + " "); position = position - 2; } ! H A (2 marks) d) What is the output when the following code is executed? int position; int[] numbers = {9, 7, 33, 2, 4, 6, 15 }; for (int i = 0; i < numbers.length; i++) { position = numbers[i]; if (position < numbers.length) { System.out.print(numbers[position] + " "); } } 33 4 15 (2 marks)

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

View Full Document
Question/Answer Sheet - Page 6 - CompSci 101 ID: . .............................................................................. CONTINUED e) The output when the code below is executed should be: 0 2 6 12 20 Complete the for loop. int value; for ( ) { value = i * (i - 1); System.out.print(value + " "); } (2 marks) int i = 1; i < 6; i++
Question/Answer Sheet - Page 7 - CompSci 101 ID: . .............................................................................. CONTINUED Question 3 (10 marks) a) What is the most reliable way to compare two double values, x and y, for equality? Circle the correct answer.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.