2009SSExamAnswers

2009SSExamAnswers - CompSci 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document Right Arrow Icon
CompSci 101 THE UNIVERSITY OF AUCKLAND CONTINUED SUMMER SEMESTER, 2009 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 (/40) Q3 (/6) Q5 (/9) Q7 (/6) Q2 (/18) Q4 (/7) Q6 (/6) Q8 (/8) 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 (40 marks) a) What output is produced by the following code? System.out.println("1. " + (Math.max(4, Math.min(5, 3)))); 1.4 (2 marks) b) Which statement in the following program causes a compile error (give the line number)? public class Program{ public void start(){ int x = 3; if(x > 1) { int y = x + 3; x = x + y; } System.out.println(x + ", " + y); } } line 8 (2 marks) 1 2 3 4 5 6 7 8 9 10
Background image of page 2
Question/Answer Sheet - Page 3 - CompSci 101 ID: . ....................................................... CONTINUED c) What output is produced by the following code? String s1 = "I can do this"; String s2 = "I can too"; int pos1, pos2; pos1 = s1.indexOf("th"); pos2 = s2.indexOf("th"); s2 = s1.substring(pos1); System.out.println(pos1 + ", " + pos2 + ", " + s2); 9, -1, this (2 marks) d) What output is produced by the following code? Point pt1 = new Point(3, 2); pt1.x = pt1.y; pt1.y = pt1.x; System.out.println(pt1.x + ", " + pt1.y); 2, 2 (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 e) What output is produced by the following code? Point pt1, pt2; pt1 = new Point(5, 15); pt2 = new Point(10, 25); pt2 = pt1; pt1 = new Point(pt2); System.out.println("1. " + pt1.x + ", " + pt2.x); System.out.println("2. " + (pt1 == pt2)); 1. 5, 5 2. false (2 marks) f) What output is produced by the following code? Point pt1, pt2; pt1 = new Point(5, 15); pt2 = new Point(7, 15); pt1.move(2, 0); System.out.println("1. " + pt1.equals(pt2)); pt2.translate(2, 0); System.out.println("2. " + pt2.x + "," + pt2.y); 1. false 2. 9,15 (2 marks)
Background image of page 4
Question/Answer Sheet - Page 5 - CompSci 101 ID: . ....................................................... CONTINUED g) What output is produced by the following code? Rectangle rect1 = new Rectangle(10, 20, 30, 40); Rectangle rect2 = new Rectangle(20, 30, 10, 10); System.out.println("1. " + rect1.intersects(rect2)); Point pt2 = new Point(70, 10); System.out.println("2. " + rect1.contains(pt2)); 1. true 2. false (2 marks) h) What output is produced by the following program? import java.awt.*; public class Program { public void start() { Rectangle r = new Rectangle(10, 20, 30, 40); questionH(r); System.out.println("1. " + r.x + ", " + r.y + ", " + r.width + ", " + r.height); } private void questionH(Rectangle rect) { rect.x = 33; rect.width = 15; } } 1. 33, 20, 15, 40 (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
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

2009SSExamAnswers - CompSci 101 THE UNIVERSITY OF AUCKLAND...

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

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