{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2006S1ExamQuestions - COMPSCI 101 THE UNIVERSITY OF...

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

View Full Document Right Arrow Icon
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)
Image of page 1

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

View Full Document Right Arrow Icon
- 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); (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. (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? (2 marks)
Image of page 2
- 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}; (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 + " "); } (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 + " "); } (2 marks)
Image of page 3

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

View Full Document Right Arrow Icon
- 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 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]; { " " , " " , " " , " " } (2 marks)
Image of page 4
- 5 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED i) What is the output of the following code? String word = "ADDITIONS"; if (! word.equals("")) { System.out.println("ABC"); if (word.length() > 4 && word.length() < 9) { System.out.println("DEF"); } else { System.out.println("GHI"); } } else if (word.charAt(3) == word.charAt(5)) { System.out.println("JKL"); if (word.length() > 4 && word.length() < 9) { System.out.println("DEF"); } else { System.out.println("GHI"); } } (2 marks) j) What is the output of the following code? public class Program { public void start() { int result = method1(6, 4); System.out.println(result); } private int method1(int number1, int number2){ int result = number2 / number1; System.out.println(number2 + "/" + number1); return result; } } (2 marks)
Image of page 5

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

View Full Document Right Arrow Icon
- 6 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED The next 3 questions relate to the Computer class below. public class Computer { private int memory; private int processor; /** * Constructs a Computer object.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern