Question: 12. Refer to the following code: private int[] arr; //precondition: arr.length > 0 public void mystery() { int s1 = 0; int s2 = 0;

12. Refer to the following code:

private int[] arr;

//precondition: arr.length > 0 public void mystery() { int s1 = 0; int s2 = 0;

for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) s1 += num; else if (num < 0) s2 += num; } System.out.println (s1); System.out.println (s2);

Which of the following best describes the value of s2 output by the method mystery?

Question 12 options:

The sum of all negative even values in arr

The sum of all positive even values in arr

The sum of all negative values in arr

The sum of all positive values in arr

The sum of all negative odd values in arr

13. Consider the following two data structures for storing several million words.

I. An array of words, not in any particular order

II. An array of words, sorted in alphabetical order

Which of the following statements most accurately describes the time needed for operations on these data structures?

Question 13 options:

Finding a given word is faster in I than in II.

Finding the first word in alphabetical order is faster in I than in II.

Inserting a word is faster in II than in I.

Finding a given word is faster in II than in I.

Finding the longest word is faster in II than in I.

14. Consider the following static method.

public static int calculate(int x) { x = x + x; x = x + x; x = x + x;

return x; }

Which of the following can be used to replace the body of calculate so that the modified version of calculate will return the same result as the original version for all x?

Question 14 options:

return 8 * x;

return 3 * x;

return 3 + x;

return 4 * x;

return 6 * x;

15. Consider the following static method.

private static void recur(int n) { if (n != 0) { recur (n - 2); System.out.println (n + " " ); } }

What numbers will be printed as a result of the call recur(6)?

Question 15 options:

6 4 2 0

No numbers will be printed because of infinite recursion.

-2 0 2 4 6

0 2 4 6

Many numbers will be printed because of infinite recursion.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!