{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 2005SSExamAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

COMPSCI 101 THE UNIVERSITY OF AUCKLAND CONTINUED SUMMER SEMESTER, 2005 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 (/10) Q4 (/10) Q7 (/10) Q10 (/10) Q2 (/10) Q5 (/10) Q8 (/6) Q11 (/12) Q3 (/5) Q6 (/12) Q9 (/5) TOTAL (/100)

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

View Full Document
- 2 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 1 (10 marks) a) What is printed by the following? System.out.println( 5 + 5 + "5" + 5 + 5 ); 10555 (1 mark) b) What is printed by the following? int a = 5; int b = 2; double d = (double)(a / b); System.out.println( d ); 2.0 (1 mark) c) What is printed by the following? System.out.println( 9 + 3 * (10 % 6) / 2 ); 15 (1 mark) d) What is printed by the following? System.out.println( "n\"n\n\\"+"\"" ); n"n \" (1 mark)
- 3 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED e) What is printed by the following? String[] words = {"cat", "go", "a"}; System.out.println( words[words[1].length()] ); a (1 mark) f) What is printed by the following? Point p = new Point(100, 100); p.translate(101, 99); System.out.println(p.x + " , " + p.y); 201 , 199 (1 mark) g) What is printed by the following? int[] nums = {1,2,3,4,5}; int[] moreNums; moreNums = nums; moreNums[3] = 1000; System.out.println(nums[3]); 1000 (1 mark)

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

View Full Document
- 4 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED h) What is printed by the following? int i = 5; while (i > 0) { i = i - 2; System.out.println(i); } 3 1 -1 (1 mark) i) What is printed by the following? boolean x = false; boolean y = (x && !x) || (x || !x); System.out.println(y); true (1 mark) j) What is printed by the following? int z = Math.max(Math.max(4,5), Math.min(7,6)); System.out.println(z); 6 (1 mark)
- 5 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 2 (10 marks) a) What is the output produced by the following code? String s1 = new String("Happiness"); String s2 = s1.substring(2, 4); System.out.println( s1.indexOf(s2) ); 2 (2 marks) b) What is the output produced by the following code? String s1 = new String("apple"); String s2 = new String("apple"); if( s1.equals(s2) && s2 == s1) System.out.println( "line 1" ); else if( s1.equals(s2) && s2 != s1) System.out.println( "line 2" ); else System.out.println( "line 3" ); line 2 (2 marks) c) What is the output produced by the following code? String s1 = new String("a"); for (int i=0; i<3; i++) s1 = s1 + s1; System.out.println( s1.length() ); 8 (2 marks)

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

View Full Document
- 6 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED d) What is the output produced by the following code? Point[] pts = new Point[3]; pts[0] = new Point(1,2); pts[1] = new Point(3,4); pts[2] = new Point(5,6); Point temp = pts[1]; pts[1] = pts[0]; pts[2].x = temp.y; System.out.println(pts[0].x + pts[1].x + pts[2].x); 6 (2 marks) e) What is the output produced by the following code? Rectangle r1 = new Rectangle(40, 30, 20, 10); Rectangle r2 = new Rectangle(10, 30, 20, 40); System.out.println(r1.intersects(r2)); false (2 marks)
- 7 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 3 (5 marks) For this question, you need to complete the isPrime() method shown below. This method should return true if the value passed to the method as a parameter is a prime number (i.e. a number greater than 1 which can be divided by only 1 and itself without leaving a remainder) and false otherwise.

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 ]}