CS 180 Final Exam Practice.pdf - CS 180 Practice Questions 1 Consider the following method public static int mystery(int arr cfw int x = 0 for(int k = 0

CS 180 Final Exam Practice.pdf - CS 180 Practice Questions...

This preview shows page 1 - 5 out of 64 pages.

CS 180 Practice Questions _____ 1. Consider the following method. public static int mystery(int[] arr) { int x = 0; for(int k = 0; k < arr.length; k = k + 2) { x = x + arr[k]; } return x; } Assume that the array numshas been declared and initialized as follows. int[] nums = {3, 6, 1, 0, 1, 4, 2}; What value will be returned as a result of the call mystery(nums)? (A)5 (B)6 (C)7 (D)10 (E)17
Background image
Questions 2-3 refer to the following information. Consider the following partial class declaration. public class SomeClass { private int myA; private int myB; private int myC; //Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB = value; } } _____ 2. The following declaration appears in another class. SomeClass obj = new SomeClass(); Which of the following code segments will compile without error? SomeClasswill allow other classes to access but not modify the value of myC?
Background image
_____ 4. Consider the following code segment. int x = 7; int y = 3; if((x < 10) && (y < 0)) { System.out.println(“Value is: “ + x * y); } else { System.out.println(“Value is: “ + x / y); } What is printed as a result of executing the code segment? _____ 5. Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for(int k = 1; k <= n; k++) { seq.add(new Integer(k * k + 3)); } return seq; } What is printed as a result of executing the following statement? System.out.println(mystery(6)); (A)[3, 4, 7, 12, 19, 28] (B)[3, 4, 7, 12, 19, 28, 39] (C)[4, 7, 12, 19, 28, 39] (D)[39, 28, 19, 12, 7, 4] (E)[39, 28, 19, 12, 7, 4, 3]
Background image
_____ 6. Consider the following method that is intended to determine if the doublevalues d1and d2close enough to be considered equal. For example, given a toleranceof 0.001, the values 54.32271and 54.32294would be considered equal. /** @return true if d1 and d2 are within the specified tolerance, * false otherwise */ public boolean almostEqual(double d1, double d2, double tolerance) { /* missing code */ } Which of the following should replace /* missing code */so that almostEqual will work as intended? are
Background image
Image of page 5

You've reached the end of your free preview.

Want to read all 64 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture