What output is produced when the following testfor

This preview shows page 7 - 12 out of 33 pages.

12. What output is produced when the following testFor() method is executed? private void testFor() { int toAdd = 2; int num; for (num = 10; num <= 70; num += toAdd) { toAdd = toAdd * 2; } System.out.println("num: " + num + ", toAdd: " + toAdd); } (a) num: 134, toAdd: 64 (b) num: 128, toAdd: 128 (c) num: 70, toAdd: 32 (d) num: 124, toAdd: 64 (e) None of the above false false false false true true true true true true false false false false false true false false false true false false true false true
Image of page 7

Subscribe to view the full document.

VERSION 1 - 8 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED 13. The definition of a class, Numbers , is shown below: public class Numbers { private static int counter = 10; private int number; public Numbers(int number, int toAdd) { this.number = number; counter = counter + toAdd; } public String toString(){ return "Number: " + number + " Counter: " + counter; } } What output is produced when the following testNumbers() method is executed? private void testNumbers() { Numbers num1, num2, num3; num1 = new Numbers(1, 2); num2 = new Numbers(2, 4); num3 = new Numbers(3, 6); System.out.println(num1.toString()); System.out.println(num2.toString()); System.out.println(num3.toString()); }
Image of page 8
VERSION 1 - 9 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED 14. What is the output of the following code? int[] nums = { 1, 2, 3, 4, 5 }; nums[0] = nums[2]; nums[2] = nums[1] + nums[3]; nums[3] = nums[nums.length-4]; nums[nums[2] - nums[0]] = nums[2] + nums[0]; for (int i = 0; i < nums.length; i++) System.out.print(nums[i] + " "); 15. What is the output of the following code? int[] nums = { 4, 5, -3, 1, 7, 2, 2, 9, 11, -7, 8, 4 }; for (int i = 1; i < nums.length; i++) if (nums[i] >= nums[i-1]) System.out.print(nums[i] + " ");
Image of page 9

Subscribe to view the full document.

VERSION 1 - 10 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED 16. Given an array of Point s initialised as follows: Point[] ps = new Point[3]; ps[0] = new Point(1,2); ps[2] = new Point(3,4); Which of the following statements would generate a NullPointerException ? (a) ps[2].move(-999, -999); (b) ps[0] = null; (c) ps[1] = null; (d) ps[1].translate(1,1); (e) None of the above 17. Given a two-dimensional array of String s initialised as follows: String[][] words = { {"a", "b"}, {"c", "d"}, {"e", "f"} }; Consider the three statements listed below: A) words[1][2] = new String("g"); B) words[words[0].length][1] = new String("g"); C) words[0][words.length] = new String("g"); Which of these statements would generate an ArrayIndexOutOfBoundsException ? (a) A, B and C (b) A only (c) C only (d) A and C (e) A and B
Image of page 10
VERSION 1 - 11 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED
Image of page 11

Subscribe to view the full document.

Image of page 12
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, ........., Types of chocolate, mouse press

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