Int a 7 2 11 28 12 9 int b b a b3 79 a3 result 79 3

This preview shows page 13 - 19 out of 26 pages.

int[] a = {7, 2, 11, 28, 12, 9}; int[] b; b = a; b[3] = 79; System.out.println("Result: " + a[3]); Result: 79 (3 marks)
Image of page 13

Subscribe to view the full document.

Question/Answer Sheet - Page 14 - CompSci 101 ID: ........................................................... continued e) Complete the output produced when the following code is executed. String[] words = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" }; for (int i = 0; i < words.length - 1; i++) { if (words[i].length() < 5) { System.out.println(words[i]); } } the fox over the lazy (3 marks)
Image of page 14
Question/Answer Sheet - Page 15 - CompSci 101 ID: ........................................................... continued Question 6 (6 marks) a) Complete the output produced when the following code is executed. String str = "Java Programming!"; System.out.println("1. "+ str.length()); System.out.println("2. "+ str.indexOf("m")); System.out.println("3. "+ str.charAt(6)); 1: 17 2: 11 3: r (3 marks) b) Complete the output produced when the following code is executed. String str = "Java Programming!"; System.out.println("1. " + str.substring(5, 8)); System.out.println("2. " + str.equals("Java Programming!")); String rearranged = str.charAt(str.length() - 1) + str.substring(0,str.length() - 1); System.out.println("3. " + rearranged); 1: Pro 2: true 3: !Java Programming (3 marks)
Image of page 15

Subscribe to view the full document.

Question/Answer Sheet - Page 16 - CompSci 101 ID: ........................................................... continued Question 7 (13 marks) Parts a), b) and c) refer to the display() method below: public void display(int temp) { if (temp > 90) { System.out.println("Hot"); } if (temp < 70) { System.out.println("Cold"); } System.out.println("Just right"); } a) What is the output produced when the following call is made to display() method? display(75); Just right (2 marks) b) What is the output produced when the following call is made to display() method? display(100); Hot Just right (2 marks)
Image of page 16
Question/Answer Sheet - Page 17 - CompSci 101 ID: ........................................................... continued c) What is the output produced when the following call is made to display() method? display(40); Cold Just right (2 marks) d) Complete the getFeeling() method which is passed an int parameter and returns a String. The method returns the String: "Sad" when the parameter, score, is less than 20, "Happy" when the parameter, score, is greater than 80, "So-So" when the parameter, score, is between 40 and 70 (both inclusive), and "Not sure" at all other times. private String getFeeling(int score) { if (score < 20) { return "Sad"; } if (score > 80) { return "Happy"; } if (score >= 40 && score <= 70) { return "So-So"; } return "Not sure"; (7 marks) }
Image of page 17

Subscribe to view the full document.

Question/Answer Sheet - Page 18 - CompSci 101 ID: ........................................................... continued Question 8 (10 marks) Complete the PrintYearlyRent program which: • prompts the user for the weekly rent amount, • reads the number entered by user, • converts the weekly rent amount into yearly rent amount and • finally prints the information. Below is an example showing how the program should execute. The user input is shown in bold. Enter weekly rent: $ 100 Yearly rent $5200 Write your code in the start() method below.
Image of page 18
Image of page 19
You've reached the end of this preview.

{[ 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