2011SSExamAnswers

2011SSExamAnswers - THE UNIVERSITY OF AUCKLAND SUMMER...

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

View Full Document Right Arrow Icon
THE UNIVERSITY OF AUCKLAND SUMMER SEMESTER, 2011 Campus: City COMPUTER SCIENCE Principles of Programming (Time Allowed: TWO hours) NOTE: You must answer all questions in this exam. No calculators are permitted Answer in the space provided in this booklet. There is space at the back for answers that overflow the allotted space. Surname Forenames Student ID Login (UPI) Q1 (/8) Q4 (/8) Q7 (/13) Q10 (/7) Q2 (/9) Q5 (/6) Q8 (/9) Q11 (/7) Q3 (/8) Q6 (/5) Q9 (/10) Q12 (/10) 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 - 30 - CompSci 101 ID: . .......................................................... Question 1 (8 marks) a) What output is produced by the following code? int a = 4; int b = 6; double c = b % a; System.out.println(a + a + ", " + b * c + 1); 8, 12.01 (2 marks) b) Complete the output of the following code. int a = 4; int b = a + 6; double c = 1.0 * b / a; a = a + (int) (a * c) + 1; b++; System.out.println("a: " + a + " b: " + b + " c: " + c); a: 15 b: 11 c: 2.5 (2 marks)
Background image of page 2
Question/Answer Sheet - 30 - CompSci 101 ID: . .......................................................... c) What output is produced by the following code? int x = 13; int y = 5; if (x < 10) { if (y != 5) { System.out.println("A"); } else { System.out.println("B"); } } else if (y > 10) { System.out.println("C"); } else { if (y != 5) { System.out.println("D"); } else { System.out.println("E"); } } E (2 marks) d) In the space provided below, assign a value for the variable, y , so that the output of the section of code is: int x = 8; int y = 6; //any even integer < 7 if (x < 10 && !(y > 7 || y == x)) { if (y % 2 == 0) { System.out.println("AA"); } else { System.out.println("BB"); } } else if (y > 2) { System.out.println("CC"); } (2 marks) AA
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 - 30 - CompSci 101 ID: . .......................................................... Question 2 (9 marks) a) What output is produced by the following code? boolean[] a1 = { true, false, false, true }; boolean[] a2 = { false, false, true, true }; for (int i = 0; i < a1.length; i++) { System.out.print((a1[i] || !a2[i]) + " "); } true true false true (3 marks) b) Complete the output when the following code is executed. String[] s = new String[4]; s[0] = "0"; for (int i = 1; i < s.length; i++) { s[i] = s[i-1] + s[i-1].length(); } System.out.print("s: "); for (int i = 1; i < s.length; i++) { System.out.print(s[i] + " "); } s: 01 012 0123 (3 marks)
Background image of page 4
Question/Answer Sheet - 30 - CompSci 101 ID: . .......................................................... c) What output is produced by the following code? String[] array = { "3", "to", "One" }; for (int i = 0; i < array.length; i++) { array[i] = "-" + array[i].length(); } for (int i = 0; i < array.length; i++) { System.out.print(i + array[i] + ","); } 0-1,1-2,2-3, (3 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 - 30 - CompSci 101 ID: . ..........................................................
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 / 28

2011SSExamAnswers - THE UNIVERSITY OF AUCKLAND SUMMER...

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