2010SSExamQuestions

# 2010SSExamQuestions - Question/Answer Sheet CompSci101 THE...

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 marks) b) Give ALL the possible outputs when the following code is executed? int random = (int) (Math.random() * 4 + 3); System.out.println(random); (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: b: (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: word: (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: word: (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; } (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; System.out.print(i + " "); (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; } (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] + " "); } } (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;
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.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern