{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2008S2ExamAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

COMPSCI 101 THE UNIVERSITY OF AUCKLAND CONTINUED SECOND SEMESTER, 2008 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 (/32) Q4 (/7) Q7 (/7) Q10 (/6) Q2 (/11) Q5 (/7) Q8 (/7) Q11 (/7) Q3 (/6) Q6 (/7) Q9 (/3) 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 (32 marks) a) What is the output produced by the following code? System.out.println("Min is: " + Math.min(1, Math.min(0, -1))); Min is: -1 (2 marks) b) What is the output produced by the following code? int value = 13; System.out.println( ! (6 < value) ); false (2 marks) c) What is the output produced by the following code? int i = 5; int[] numbers = {4, 2, -7, 5, 1, 6, 3}; System.out.println(numbers[i] + numbers[i+1]); 9 (2 marks)
- 3 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED d) What is the output produced by the following code? String name = "intuitive"; name = name.substring(2); int position = name.indexOf('i'); System.out.println(name.substring(0, position + 1)); tui (2 marks ) e) What is the output produced by the following code? double d = 23.7, e = 3.1; System.out.println((int)(Math.round(d - e))); 21 (2 marks) f) Complete the output which is produced by the following code. Point p1 = new Point(4, 6); Point p2 = new Point(3, 5); p1.move(10, 4); p2.translate(10, 4); System.out.println("1: " + p1.x + ", " + p1.y); System.out.println("2: " + p2.x + ", " + p2.y); 1: 10, 4 2: 13, 9 (2 marks)

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

View Full Document
- 4 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED g) Complete the output which is produced by the following code. Point p1 = new Point(4, 6); Point p2 = new Point(4, 6); Point p3 = p2; p2 = new Point(p3.x, p3.y); System.out.println("1: " + p1.equals(p2)); System.out.println("2: " + (p2 == p3)); 1: true 2: false (2 marks) h) What is the output produced by the following code? Rectangle rect = new Rectangle(20, 30, 50, 40); Point p = new Point(rect.x + 5, rect.y + 2); if (rect.contains(p)) { System.out.println("Yes"); } else { System.out.println("No"); } yes (2 marks)
- 5 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED i) Complete the following code so that it produces the output: No Rectangle rect1 = new Rectangle(20, 30, 50, 40); Rectangle rect2 = new Rectangle(10, 20, , ); One of the values less than 10 if (rect1.intersects(rect2)) { System.out.println("Yes"); } else { System.out.println("No"); } (2 marks) j) What is the output when the start() method below is executed? public void start() { Point p1 = new Point(3, 5); doSomething(p1); System.out.println("1: " + p1.x + ", " + p1.y); } private void doSomething(Point p) { p.x = p.x + 10; System.out.println("2: " + p.x + ", " + p.y); } 2: 13, 5 1: 13, 5 (2 marks) 8 5

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

View Full Document
- 6 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED k) Declare a constant for the Greek letter pi and assign to it the value 3.14159. final double PI = 3.14159; (2 marks) l) Write a while loop that adds together the first n positive integers, i.e., 1 + 2 + 3 + ... + n and stores the result in the variable, sum .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 3

2008S2ExamAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document
Ask a homework question - tutors are online