2008S1ExamQuestionsV1

Continued question 22 6 marks consider the following

This preview shows page 18 - 24 out of 31 pages.

CONTINUED Question 22 (6 marks) Consider the following code segment: String[] words = { "a cat", "hi", "comp ", "sci", "ex am" }; int count = countSpaceWords(words); System.out.println(count); The output of this code is: 3 The countSpaceWords () method is passed an array of Strings as a parameter, and returns the number of Strings in the array that contain at least one space character. The following list of 12 statements is broken into 6 groups of 2 (i.e. 6 pairs). The correct definition of the countSpaceWords () method can be formed using exactly ONE statement from each pair. The pairs themselves are given in a random order, so you will need to select the correct statement from each pair as well as determine the correct order for the statements. return i; return count; int count = 0; int count; private int countSpaceWords( String[] words ) private int countSpaceWords( String words ) if (words[i].indexOf(' ') == -1) if (words[i].indexOf(' ') > -1) for (int i = 0; i < words.length; i++) for (int i = 0; i < words.length(); i++) count = count + words[i].indexOf(' '); count = count + 1;
Image of page 18

Subscribe to view the full document.

VERSION 18832768 - 19 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED In the space provided below, give the correct definition of the countSpaceWords () method. You must only use the statements that were listed on the previous page , but arranged in the correct order. You will need to put all of the opening and closing braces into the code, and you must indent the code correctly. (6 marks)
Image of page 19
VERSION 18832768 - 20 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED Question 23 (4 marks) What is printed by the following? public void start() { lotsOfIfs(10, 9); } private void lotsOfIfs(int number, int value) { System.out.print("A "); if (number < value) { System.out.print("B "); number = 10; } else if (value < 10) { System.out.print("C "); if (number < 20) { System.out.print("D "); } if (value > 5) { System.out.print("E "); } value = number / 2; } if (number >= 6) { System.out.print("F "); number = 5; } else if (value >= 3) { System.out.print("C "); value = 5; } else { System.out.print("H "); number = value; } if (number < 6) { System.out.print("I "); } System.out.println(number + " " + value); } (4 marks)
Image of page 20

Subscribe to view the full document.

VERSION 18832768 - 21 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED Question 24 (6 marks) What is printed by the following? public class MyProgram { public void start() { System.out.print(" 1"); methodOne(); System.out.print(" 2"); } private void methodOne() { System.out.print(" 3"); methodTwo(); System.out.print(" 4"); methodTwo(); } private void methodTwo() { System.out.print(" 5"); } } (6 marks)
Image of page 21
VERSION 18832768 - 22 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED Question 25 (6 marks) Consider the following definitions of the Alien class and the Bullet class: import java.awt.*; public class Alien { private Rectangle pos; public Alien() { pos = new Rectangle(50, 50, 100, 50); } public boolean intersects(Rectangle b) { return pos.intersects(b); } public void setOffScreen() { pos.y = 1000000; } public void draw(Graphics g) { g.fillRect(pos.x, pos.y, pos.width, pos.height); } } import java.awt.*; public class Bullet { private Rectangle pos; public Bullet() { pos = new Rectangle(100, 300, 5, 10); } public Rectangle getPos() { return pos; } public void move() { pos.y -= 2; } public void draw(Graphics g) { g.drawRect(pos.x, pos.y, pos.width, pos.height); } }
Image of page 22

Subscribe to view the full document.

VERSION 18832768 - 23 - COMPSCI 101 Question and Answer Booklet ID ..........................................
Image of page 23
Image of page 24
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