What output is produced by the following code segment

This preview shows page 8 - 14 out of 30 pages.

11) What output is produced by the following code segment? int i = 7; int counter = 0; while (i >= 0) { counter++; i = i - 3; } System.out.println(counter); (a) 3 (b) 4 (c) 2 (d) Nothing, because the variable counter is out of scope. (e) Nothing, because the loop never stops.
Image of page 8

Subscribe to view the full document.

VERSION 00000001 - 9 - COMPSCI 101 CONTINUED 12) A for loop has the following basic structure: for ( initialisation ; condition ; increment ) { ... } Which of the following is the correct order in which the three component parts of the loop would be encountered if the loop executed exactly twice ? 13) What output is produced by the following code? String[] words = {"one", "two", "buckle", "my", "shoe"}; for (int i = 0; i < words.length; i++) { if (words[i].length() <= 3) { System.out.print(words[i].charAt(0)); } }
Image of page 9
VERSION 00000001 - 10 - COMPSCI 101 CONTINUED 14) What output is produced by the following code segment? int sum = 0; for (int i = 1; i < 5; i++) { sum += i; } System.out.println(sum); 15) Consider the following while loop: int i = 10; while (i > 0) { System.out.println(i * i); i -= 2; } Which of the following for loops is equivalent to this while loop? (a) for (int i = 0; i < 10; i++) { System.out.println(i * i); } (b) for (int i = 10; i > 0; i--) { System.out.println(i * i); } (c) for (int i = 0; i <= 10; i=i+2) { System.out.println(i * i); } (d) for (int i = 10; i >= 0; i=i-2) { System.out.println(i * i); } (e) for (int i = 10; i > 0; i=i-2) { System.out.println(i * i); }
Image of page 10

Subscribe to view the full document.

VERSION 00000001 - 11 - COMPSCI 101 CONTINUED 16) Consider the Circle class as defined below: public class Circle { private int radius; private Point centre; public Circle(int rad, int x, int y) { radius = rad; centre = new Point(x, y); } public int getRadius() { return radius; } } What would be the output of the following code that creates two Circle objects? Circle c1, c2; c1 = new Circle(10, 20, 30); c2 = c1; c1 = new Circle(100, 200, 300); System.out.println(c1.getRadius()); System.out.println(c2.getRadius());
Image of page 11
VERSION 00000001 - 12 - COMPSCI 101 CONTINUED 17) Consider a window that is 400 pixels wide and 200 pixels high, as shown below: Which of the following method calls would correctly draw a circle that is positioned exactly in the centre of the window , and has a radius of 50 pixels ? (a) g.drawOval(200, 100, 100, 100); (b) g.drawOval(50, 150, 100, 100); (c) g.drawOval(150, 50, 100, 100); (d) g.drawOval(200, 100, 50, 50); (e) g.drawOval(150, 50, 50, 50);
Image of page 12

Subscribe to view the full document.

VERSION 00000001 - 13 - COMPSCI 101 CONTINUED
Image of page 13
Image of page 14
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