2011S2ExamQuestions

2011S2ExamQuestions - CompSci 101 THE UNIVERSITY OF...

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

View Full Document Right Arrow Icon
CompSci 101 CONTINUED THE UNIVERSITY OF AUCKLAND SECOND SEMESTER, 2011 Campus: City COMPUTER SCIENCE Principles of Programming (Time Allowed: TWO hours) NOTE: You must answer all questions in this test. No calculators are permitted Answer in the spaces provided in this booklet. There is space at the back for answers that overflow the allotted space. Surname Forenames Student ID Login (UPI) Q1 (/10) Q4 (/4) Q7 (/8) Q10 (/10) Q2 (/6) Q5 (/15) Q8 (/7) Q11 (/10) Q3 (/6) Q6 (/15) Q9 (/9) 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 - 2 - CompSci 101 ID: . .......................................................... CONTINUED Question 1 (10 marks) a) Complete the output produced when the following code is executed. String a = "3"; String b = "2"; int y = 5; int z = 4; System.out.print("1. "); System.out.println(a + b + y + z + (y + z)); 1. (2 marks) b) Complete the output produced when the following code is executed. int a = 2; int b = a; a++; b = a + b; a = b + a; b = a + b; System.out.println("a: "+ a + " b: "+ b); a: b: (2 marks)
Background image of page 2
Question/Answer Sheet - 3 - CompSci 101 ID: . .......................................................... CONTINUED c) Complete the statement below which assigns a random number between 1 and 12 inclusive to the variable, monthNumber . int monthNumber = (2 marks) d) The testCompile() method below does not compile. Give the line number where the compile error occurs (the line numbers are shown on the left hand side). 1 private void testCompile(String value) { 2 int result1 = value + 1; 3 String result2 = result1 + "2"; 4 System.out.println(result2); 5 } Compile error occurs at line: (2 marks) e) Give the output produced when the following code is executed. String a = "56"; int x = (Integer.parseInt(a) / 7 + 2) * 6 - 4; String b = "" + x; System.out.println(b.equals(a)); (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 - 4 - CompSci 101 ID: . .......................................................... CONTINUED Question 2 (6 marks) a) Complete the definition of the Rectangle , r2 , so that the following code prints the letter B. Rectangle r1 = new Rectangle(20, 40, 100, 50); r1.x = r1.x + 10; (2 marks) if (r1.intersects(r2)) { System.out.println("A"); } else { System.out.println("B"); } b) Complete the definition of the Point , p , so that the following code prints the letter A. private void testRectPointMethod() { Rectangle r = new Rectangle(20, 40, 100, 50); (2 marks) if (isTheCentrePoint(r, p)) { System.out.println("A"); } else { System.out.println("B"); } } private boolean isTheCentrePoint(Rectangle r, Point p) { int cX = r.x + r.width / 2; int cY = r.y + r.height / 2; if (cX == p.x && cY == p.y) { return true; } return false; } Rectangle r2 = new Rectangle(25, 40, , ); Point p = new Point( , );
Background image of page 4
Question/Answer Sheet - 5 - CompSci 101 ID: . .......................................................... CONTINUED c) Give the output produced when the following code is executed. Point p1 = new Point(20, 65); Point p2 = p1; Point p3 = p2; p1.x = 25; if (p1.equals(p3)) { System.out.print("A "); } else { System.out.print("B "); } if (p1 == p3) { System.out.print("C "); } else { System.out.print("D "); } (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 - 6 - CompSci 101 ID: . .......................................................... CONTINUED Question 3 (6 marks) a) Give the output produced when the following manipulatePoints() method is executed. private void manipulatePoints() { Point p1 = new Point(5, 2); Point p2 = p1; manipulate(p1); System.out.println(p1.x + " " + p1.y + " " + p2.x + " " + p2.y); } private void manipulate(Point p) { p.x = 1; p.y = 3; } (2 marks) b) Give the output produced when the following manipulatePoints() method is executed.
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 / 32

2011S2ExamQuestions - CompSci 101 THE UNIVERSITY OF...

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