What output is produced when the following code is

This preview shows page 3 - 9 out of 30 pages.

4) What output is produced when the following code is executed? String word = "SPLENDID"; int pos1 = word.indexOf("S"); int pos2 = word.indexOf("word"); int sum = pos1 + pos2; System.out.println(sum); (a) -2 (b) 1 (c) 0 (d) -1 (e) None of the above
Image of page 3

Subscribe to view the full document.

VERSION 00000001 - 4 - COMPSCI 101 CONTINUED 5) What output is produced when the following is executed? String word = "JOYFUL"; char c = word.charAt(word.length() - 2); word = word.substring(0, 1) + word.substring(3) + c; System.out.println(word); 6) Consider the following variable declarations: Point p1, p2; Rectangle r1, r2; p1 = new Point(10, 20); p2 = new Point(30, 40); r1 = new Rectangle(10, 20, 30, 40); r2 = new Rectangle(50, 60, 70, 80); Given these variable declarations, only ONE of the following statements will COMPILE. Which one?
Image of page 4
VERSION 00000001 - 5 - COMPSCI 101 CONTINUED 7) What is the output of the following code? boolean a = true; boolean b = true; boolean c = false; if (c) { System.out.println("first"); } else if (a && c) { if (a || b) { System.out.println("second"); } else { System.out.println("third"); } } else if (a || c) { System.out.println("fourth"); } else { System.out.println("fifth"); }
Image of page 5

Subscribe to view the full document.

VERSION 00000001 - 6 - COMPSCI 101 CONTINUED 8) Given the isPerfect() method defined below, which of the following sections of code prints “yes”? private boolean isPerfect(int n1, int n2, int n3) { int sum = n1 + n2 + n3; int product = n1 * n2 * n3; if (sum == product) { return true; } return false; } (a) if (isPerfect(0, 3, 3)) { System.out.println("yes"); } (b) if (isPerfect(-1, 6, 1)) { System.out.println("yes"); } (c) if (isPerfect(1, 2, 3)) { System.out.println("yes"); } (d) if (isPerfect(2, 2, 2)) { System.out.println("yes"); } (e) None of the above
Image of page 6
VERSION 00000001 - 7 - COMPSCI 101 CONTINUED 9) What is the output of the following code? public void start() { int a, b; a = 1; b = 2; exam(a, b); System.out.print("a: " + a + ", "); System.out.print("b: " + b); } private void exam(int a, int other) { a = a + 1; other = other + 1; }
Image of page 7

Subscribe to view the full document.

VERSION 00000001 - 8 - COMPSCI 101 CONTINUED 10) What output is produced by the following code segment? int counter = 1; int sum = 0; while (counter <= 3) { sum += counter; } System.out.println(sum);
Image of page 8
Image of page 9
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