2009S1ExamAnswers

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CompSci 101 THE UNIVERSITY OF AUCKLAND CONTINUED FIRST 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) Q4 (/5) Q7 (/5) Q10 (/8) Q2 (/7) Q5 (/7) Q8 (/7) Q3 (/7) Q6 (/7) Q9 (/7) TOTAL (/100) Question/Answer Sheet - Page 2 - CompSci 101 ID: ........................................................ CONTINUED Question 1 (40 marks) a) Write ALL the possible numbers which could be printed by the following statement. System.out.print((int)(Math.random() * 4 + 2)); 2, 3, 4, 5 (2 marks) b) What is printed by the following code? int a = 5; int b = a / 3; double c = a / 4; a = a % 3; System.out.println(a + " " + b + " " + c); 2 1 1.0 (2 marks) Question/Answer Sheet - Page 3 - CompSci 101 ID: ........................................................ CONTINUED c) Complete the output produced by the following code. public class Program { public void start() { Point point1 = new Point(20, 15); System.out.println("1. " + point1.x + "," + point1.y); methodC(point1); System.out.println("2. " + point1.x + "," + point1.y); } private void methodC(Point pt2) { pt2.x = 5; pt2.y = 3; pt2 = new Point(2, 4); } } 1. 20 15 2. 5 3 (2 marks) d) Complete the output produced by the following program. public class Program { public void start() { Point point1 = new Point(20, 15); System.out.println("1. " + point1.x + "," + point1.y); methodD(point1); System.out.println("2. " + point1.x + "," + point1.y); } private void methodD(Point pt2) { pt2 = new Point(2, 4); pt2.x = 5; pt2.y = 3; } } 1. 20 15 2. 20 15 Question/Answer Sheet - Page 4 - CompSci 101 ID: ........................................................ CONTINUED (2 marks) e) Complete the header of the following method. private boolean methodE ( Rectangle rect , Point pt ) { if (rect.contains(pt)) { return true; } return false; } (2 marks) f) Complete the header of the following method. private int methodF ( int numbers ) { int b = new int[numbers.length]; int limit = 100; for (int i=0; i<numbers.length; i++) { if (numbers[i] < limit) { b[i] = numbers[i]; } else { b[i] = -1; } } return b; } (2 marks) Question/Answer Sheet - Page 5 - CompSci 101 ID: ........................................................ CONTINUED g) Complete the output produced by the following code. int xPos = 20; int yPos = 15; int width = 50; int height = 8; Rectangle rect1, rect2, rect3; rect1 = new Rectangle(10, 11, 30, 7); rect2 = new Rectangle(xPos , yPos, width, height); rect3 = new Rectangle(rect1); rect3.x = rect1.y; rect1.y = rect2.height; System.out.println("1. " + rect1.x + "," + rect1.y + "," + rect1.width + "," + rect1.height); System.out.println("2. " + rect3.x + "," + rect3.y + "," + rect3.width + "," + rect3.height); 1. 10 8 30 7 2. 11 11 30 7...
View Full 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 / 30

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

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

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