2006S1ExamAnswers

2006S1ExamAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMPSCI 101 THE UNIVERSITY OF AUCKLAND CONTINUED FIRST 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 (/40) Q4 (/5) Q7 (/7) Q10 (/10) Q2 (/5) Q5 (/10) Q8 (/7) Q3 (/6) Q6 (/6) Q9 (/4) TOTAL (/100)- 2 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 1 (40 marks) There are 20 parts to this question, each worth 2 marks. Write your answer in the space provided for each part. a) What is the output of the following code? System.out.println(2+2*2); 6 (2 marks) b) Consider the following code: int x; x = ((int)(Math.random()*10)) / 3 + 1; Write down all of the possible values that x may contain after the assignment statement. 1, 2, 3 or 4 (2 marks) c) Consider the following code: int i = 0; int s = 0; int n = 10; while (i < n) { s = s + i; } Assuming this code is contained inside a method in a valid class, will it compile without error? yes (2 marks)- 3 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED d) Is the following code a valid way of creating and initialising an array? int array; array = {1, 2, 3, 5}; No (array constants can only be used in initializers) (2 marks) e) What is the output of the following code? int answer = 0; for (int i = 1; i <= 6; i++) { for (int j = 0; j < 5; j++) { answer++; } System.out.print(answer + " "); } 5 10 15 20 25 30 (2 marks) f) What is the output of the following code? int answer = 0; for (int i = 1; i <= 6; i++) { for (int j = 0; j < i; j++) { answer++; } System.out.print(answer + " "); } 1 3 6 10 15 21 (2 marks)- 4 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED g) In the start() method below, assign a value to the variable number so that the output produced by the start() method will be: sum: 1 Complete the assignment in the space provided below: public void start() { int sum = 0; int number = 2; (or any even number <= 40 and > 0) (2 marks) if (number > 40) { sum++; if(number <= 50) { sum++; } sum++; } if (number%2 == 1) { sum++; } sum++; if (number > 50 || number <= 0) { sum++; } System.out.println("sum: " + sum); } h) What is stored in array a after the following statements have been executed? String a = {"one", "two", "three", "four"}; String b; b = a; a[0] = a[1]; b[1] = a[2]; { " two " , " three " , " three " , " four " } (2 marks)- 3 - COMPSCI 101 Question/Answer Sheet ID: ........................................................................
View Full 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 / 14

2006S1ExamAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online