{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2002SCExamAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document Right Arrow Icon
COMPSCI 101 CONTINUED THE UNIVERSITY OF AUCKLAND SECOND SEMESTER, 2002 Campus: City and Tamaki 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 There is also a page at the back for rough working. This page will not be marked. No Calculators are permitted Surname Forenames Student ID Login name Question Marks Out of Question 1 (Short answer) 10 Question 2 (Writing methods) 10 Question 3 (Desk-checking methods) 10 Question 4 (Classes) 15 Question 5 (ActionListener, MouseListener) 15 Question 6 (Bresenham's line algorithm) 20 Question 7 (Ethics) 20 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
Candidate's Name:___________________ 2 COMPSCI 101 CONTINUED 1. What is the output shown by each of the following code fragments? (a) System.out.println(2+3+"^2+3^"+2+3); 5^2+3^23 (1 mark) (b) System.out.println(2/4*4.0+"^"+2.0/4*4); 0.0^2.0 (1 mark) (c) int a = 7; int b = 3; if( a % 3 == b % 2 ) System.out.println("Equal"); if( !(a != 3) || !(b == 3) ) System.out.println("Not"); Equal (2 marks) (d) int a = 7; int b = 3; if( a==7 && ( a!=4 && b+4==a) ) System.out.println("And"); if( a==3 || (a==7 && b==4) || (b!=7 || b==7) ) System.out.println("Or"); And Or (2 marks) (e) System.out.println("Before"); for(int i=0; i<0; i++) System.out.println("Test" + i); System.out.println("After"); Before After (1 mark)
Image of page 2
Candidate's Name:___________________ 3 COMPSCI 101 CONTINUED (f) int a = 4; int b = 5; int c = 6; int d = 7; if( a > b && b > c ) System.out.println("a>c"); else if( (a+d) == (b+c) ){ if( a-b < d-c) if( a == d ) System.out.println("b > c"); else if( a > d ) System.out.println("c > b"); else System.out.println("unknown"); } else System.out.println("Confused yet?"); System.out.println("I am"); unknown I am (2 marks) (g) int k=0; int i=0; for(i=0; i<3; i++); { System.out.println("Test" + i); if(k == 0) k = 2; else k = 0; System.out.println(k); } Test3 2 (1 mark)
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
Candidate's Name:___________________ 4 COMPSCI 101 CONTINUED 2. Write a method called scramble() which is used to rearrange the letters in a given String . The letters should be rearranged into random order, based on a "key" value which is used as the "seed" for the pseudo-random number generator used by Java. Your method should accept a String and an int as parameters, and should return a String which contains the same letters, but rearranged into random order. If the scramble() method is called on two separate occasions with the same parameters, then the characters will be arranged in the same order on both occasions. All of the letters in the original String should be used (including all spaces and punctuation). Example: Executing the statements: System.out.println( scramble( "Hello", 100 ) ); System.out.println( scramble( "Hello", 200 ) ); System.out.println( scramble( "Have a nice day", 1234 ) ); System.out.println( scramble( "Hello", 100 ) ); would produce the output: Hlloe olelH ie Hd ncyvaeaa Hlloe You should use the Random class in order to select the letters in the same order given the same key value. The following extract from the Java API might prove useful here (note that you do not have to use all the methods listed below): java.util.Random Constructor Summary Random () Creates a new random number generator.
Image of page 4
Image of page 5
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