{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 2009S2TestAnswers - 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 SEMESTER TWO, 2009 Campus: City CompSci 101 TEST COMPUTER SCIENCE Principles of Programming (Time allowed: 75 minutes) 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: Lab time: Q1 (/20) Q4 (/10) Q7 (/6) Q2 (/20) Q5 (/18) Q8 (/6) Q3 (/12) Q6 (/8) 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 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 1 (20 marks) a) What is the output when the following code is executed? int num1 = 4; int num2 = 3; String exp = " a + b "; System.out.println(num1 + num2 + exp + num1 * num2); 7 a + b 12 (2 marks) b) Complete the output when the Q01B program is executed. public class Q01B { public void start() { printAValue(20, 10, 30); } private void printAValue(int num1, int num2, int num3) { int a = Math.max(num1, num2); int b = Math.min(a, num3); System.out.println("Result: " + b); } } Result: 20 (2 marks)
Question/Answer Sheet - Page 3 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED c) Complete the output when the following code is executed. String words1 = "super duper"; String words2 = "mish mash"; String words3 = words1.substring(1, 3) + "XX" + words2.substring(4) + "XX"; System.out.println(words3); up XX mash XX (2 marks) d) Complete the output when the following code is executed. String words1 = "WISHY WASHY"; int pos1 = words1.indexOf(" "); int pos2 = words1.indexOf("SH"); int pos3 = words1.indexOf("DISH"); System.out.println("pos1: " + pos1); System.out.println("pos2: " + pos2); System.out.println("pos3: " + pos3); pos1: 5 pos2: 2 pos3: -1 (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 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED e) Complete the output when the following code is executed. String words1 = "PING PONG"; char a = words1.charAt(3); char b = words1.charAt(5); char c = words1.charAt(8); System.out.println("a: " + a + " b: " + b + " c:" + c); a: G b: P c: G (2 marks) f) Complete the output when the following code is executed. int a = 4; int b = a + 10; int c = a + b; a = a + 4; b = b - a; c = b + a + 1; System.out.println("a: " + a + " b: " + b + " c: " + c); a: 8 b: 6 c: 15 (2 marks)
Question/Answer Sheet - Page 5 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED g) Complete the output when the following section of code is executed. int a = 9; double b = a / 2; double c = a / 2.0; System.out.println("a: " + a + " b: " + b + " c: " + c); a: 9 b: 4.0 c: 4.5 (2 marks) h) Complete the output when the following section of code is executed. int number = 1; int value = Math. min (5, Math. max (number + 2, 4)); System. out .println("H: " + (value > 4) ); H: false (2 marks) i) Complete the output when the following section of code is executed. boolean isAMatch; int number = 38; String numberString = "38"; isAMatch = (number == Integer. parseInt (numberString)); System. out .println("I: " + isAMatch); I: true (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 SURNAME: ...................................................... FORENAMES: ...........................................................
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