2010SSExamQuestions

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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)
Background image of page 2
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)
Background image of page 3

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

View Full DocumentRight Arrow Icon
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)
Background image of page 4
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)
Background image of page 5

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

View Full DocumentRight Arrow Icon
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;
Background image of page 6
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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 26

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online