12. Refer to the following code: private int[] arr; //precondition: arr.length >
ID: 3718318 • Letter: 1
Question
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.
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
Explanation / Answer
Ans:12
The sum of all negative values in arr
================================================================
Ans:13
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.
===============================================================
Ans:14
return 8 * x;
Explantion:
Let x is 2
x = x + x; = 2 + 2 = 4
x = x + x; = 4 + 4 = 8
x = x + x; = 8 + 8 = 16
===============================================================
Ans:15
0 2 4 6
===============================================================
Kindly Check and Verify Thanks..!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.