{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2009S1ExamQuestions

# 2009S1ExamQuestions - CompSci 101 THE UNIVERSITY OF...

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

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) (/40) Q4 (/5) Q7 (/5) Q10 (/8) Q2 (/7) Q5 (/7) Q8 (/7) Q3 (/7) Q6 (/7) Q9 (/7) TOTAL (/100)

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

View Full Document
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 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 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. 2. (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. 2. (2 marks)

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

View Full Document
Question/Answer Sheet - Page 4 - CompSci 101 ID: ........................................................ CONTINUED e) Complete the header of the following method. private methodE ( Rectangle , ) { if (rect.contains(pt)) { return true; } return false; } (2 marks) f) Complete the header of the following method. private methodF ( ) { 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. 2. (2 marks) h) Complete the output produced by the following code. Rectangle rect1 = new Rectangle(20, 15, 30, 10); Rectangle rect2 = new Rectangle(rect1); Rectangle rect3 = new Rectangle(10, 10, 20, 20); System.out.println("1. " + (rect1 == rect2)); System.out.println("2. " + rect1.equals(rect2)); System.out.println("3. " + rect1.intersects(rect3)); 1. 2. 3. (2 marks)

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

View Full Document
Question/Answer Sheet - Page 6 - CompSci 101 ID: ........................................................ CONTINUED i) What is printed by the following code? int[] list = {1, 2, 3, 4, 5, 6}; System.out.println(list[list.length - list[0]]); (2 marks) j) Complete the method, printTotal() , which prints the total of all the numbers in an integer array. For example, the following code: int [] list1 = {3, 7, 0, 4, 9, 12}; int [] list2 = {0, 3, 5}; printTotal(list1); printTotal(list2); would give the following output: 35 8 public printTotal ( ) { } (2 marks)
Question/Answer Sheet - Page 7 - CompSci 101 ID: ........................................................

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

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern