{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2006SSExamAnswers - COMPSCI 101-1Question/Answer Sheet THE...

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

View Full Document Right Arrow Icon
COMPSCI 101 THE UNIVERSITY OF AUCKLAND CONTINUED SUMMER 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 (/8) Q7 (/12) Q10 (/6) Q2 (/4) Q5 (/8) Q8 (/9) Q11 (/10) Q3 (/5) Q6 (/8) Q9 (/10) TOTAL (/100) - 1 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 1 (20 marks) a) What is printed by the following? System.out.println(40 % 100); 40 (2 marks) b) What is printed by the following? for (int i = 5; i >= 0; i = i-2) { System.out.println(i); } 5 3 1 (2 marks) c) What is printed by the following? int[] numbers = {1, 2, 3, 4}; System.out.println(numbers[numbers[1]]); 3 (2 marks)
Background image of page 1

Info iconThis 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 d) What is printed by the following? String word1 = new String(" bravo"); String word2 = new String("BRAVO"); String word3 = word2; if ((word1.trim()).equals(word2.toLowerCase())) { System.out.println("1" + word1); } if (word3 == word2) { System.out.println("2" + word2); } 1 bravo 2BRAVO (2 marks) e) What is printed by the following? String name = "Thought is free"; name = name.substring(8); String result = name.substring(3); System.out.println(result); free (2 marks) f) What is printed by the following? String word = "Art is long, life is short"; int position1 = word.indexOf("long"); int position2 = word.indexOf(','); word = word.substring(position1, position2); System.out.println("word: " + word); word: long (2 marks) - 3 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED g) What is printed by the following? Point a = new Point(8, 9); Point b = new Point(8, 9); Point c = a; Point d = new Point(10, 10); System.out.println(a.equals(d)); System.out.println(b == c); System.out.println(c.equals(b)); false false true (2 marks) h) What is printed by the following? boolean result1, result2; int value = 7; result1 = value > 5 && !(value < 0 || value%2 == 1); result2 = (value != 6 && value%2 != 0) || value > 3; System.out.println("result1: " + result1); System.out.println("result2: " + result2); result1: false result2: true (2 marks)
Background image of page 2
- 4 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED i) The Number class is defined as follows: public class Number { private static int number1 = 0; private int number2; public Number(int num) { number1 = number1 + num; number2 = num; } public String toString() { return "number1: " + number1 + " number2: " + number2; } } What is printed by the following? Number num1 = new Number(4); Number num2 = new Number(2); Number num3 = new Number(3); System.out.println(num1.toString()); System.out.println(num2.toString()); number1: 9 number2: 4 number1: 9 number2: 2 (2 marks) j) What is printed by the following? String[] a = new String[10]; System.out.println(a[5]); null (2 marks) - 5 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 2 (4 marks) a) What is the output when the start() method below is executed? public class Q2 { public void start() { lotsOfIfs(24, "HI"); System.out.println(); lotsOfIfs(2, "HI"); } private void lotsOfIfs(int value, String word) { if (value < 4 && !word.equals("HI")) { System.out.println("Line 1"); value = 12; word = word + "HO"; } else if (value > 10) { System.out.println("Line 2"); value = value/2; word = word + word + word; } if (value > 6) { System.out.println("Line 3"); value = -1; } else if (word.length() > 4) { System.out.println("Line 4");
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}