2006S2ExamQuestions

2006S2ExamQuestions - COMPSCI 101 THE UNIVERSITY OF AUCKLAND SECOND SEMESTER 2006 Campus City COMPUTER SCIENCE Principles of Programming(Time

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

View Full Document Right Arrow Icon
COMPSCI 101 THE UNIVERSITY OF AUCKLAND CONTINUED SECOND SEMESTER, 2006 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 (/20) Q4 (/12) Q7 (/9) Q10 (/10) Q2 (/10) Q5 (/8) Q8 (/6) Q3 (/5) Q6 (/10) Q9 (/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
- 2 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 1 (20 marks) There are 10 parts to this question, each worth 2 marks. Write your answer in the space provided for each part. What is “printed” by each of the following pieces of Java program? a) String s = "sesquipedalian"; System.out.println(s.substring(6,11)); System.out.println(s.substring(s.length()-2)+"tic"); (2 marks) b) int i = 4; int j = 6; System.out.println(i + "+" + j+ " == " +(i+j)); (2 marks) c) int i=6, j=3; int k = j/i; System.out.println(k > 0.3); double d = ((double)j)/((double)i); System.out.println(d > 0.3); (2 marks) d) int x = Math.min(Math.max(5, 4), 3); int y = Math.max(Math.min(5, 4), 3); System.out.println(x); System.out.println(y); (2 marks)
Background image of page 2
- 3 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED e) String[] s1 = { "hay", "bee", "sea"}; String[] s2 = { "horses", "mutton", "swimming"}; for (int i=0; i<s1.length; i++) { System.out.println(s1[i] + " for " + s2[i]); } (2 marks) f) int[] numbers = {2,4,10,6,1,2,3,4}; int i = 0; while ((numbers[i] % 2) == 0) { System.out.println(numbers[i]); i = i+1; } (2 marks) g) int i = 6, j = 10; if (i < 8) { if (j < 15){ System.out.println( " Line 1"); } else { System.out.println("Line 2"); } } System.out.println("Line 3"); (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
- 4 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED h) int answer = 0; for (int i = 1; i <= 4; i++) { answer = Math.max(10*i,20); System.out.print(answer + " " ); } (2 marks) i) String[] s = {"You", "have", "no", "chance", "of", "your", "best", "final", "exam", "ever", "performed"}; for (int i=0; i<s.length; i++) { if (s[i].length()==4) { System.out.print(s[i]+ " " ); } } (2 marks) j) for (int i = 10; i>0; i--){ if ((i%2) == 0){ System.out.println((12-i)/2); } } (2 marks)
Background image of page 4
- 5 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 2 (10 marks) Complete each of the methods below. a) // method to decide whether the first parameter String is // longer than the second, assuming neither String is null private boolean longer(String s1, String s2){ } (2 marks) b) // method to decide whether a double number passed as a // parameter may be cast to an int that represents // exactly the same value
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the 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 / 22

2006S2ExamQuestions - COMPSCI 101 THE UNIVERSITY OF AUCKLAND SECOND SEMESTER 2006 Campus City COMPUTER SCIENCE Principles of Programming(Time

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