Question
What are the contents of the array nums after the following code segment has been executed? int[] nums = new int[8]; nums[0] = 0; int
-
What are the contents of the array nums after the following code segment has been executed?
int[] nums = new int[8]; nums[0] = 0; int n = 1; while (n < nums.length) { int k; for (k = n; k < 2*n; k++) nums[k] = nums[k-n] + 1; n = k; }
-
0 1 1 1 1 1 1 1
-
0 1 0 1 0 1 0 1
-
0 1 1 2 1 2 2 3
-
0 1 2 3 1 2 3 4
-
0 1 2 3 4 5 6 7
Question 20
-
ABCDE
-
-
Consider the following method.
/** Rearranges the elements in words according to * the values stored in an integer array indices * so that the element of words at index indices[k] * is moved to the element at index k. * Precondition: words.size() == indices.length */ public void permute(List
words, int[] indices) { ArrayList temp = new ArrayList (); < missing code > } For example, after executing the code segment
List
words = new ArrayList (); words.add("I"); words.add("am"); words.add("Sam"); int[] indices = {2, 0 1}; permute(words, indices); words will become the list ["Sam", "I", "am"]. Which of the following code segments could replace <missing code> in the permute method?
for (String word : words) temp.add(word); for (int k = 0; k < indices.length; k++) words.set(k, temp.get(indices[k]));
for (int j : indices) temp.add(words.get(j)); for (int k = 0; k < indices.length; k++) words.set(k, temp.get(k));
while (words.size() > 0) temp.add(words.remove(0)); for (int j : indices) words.add(temp.get(j));
-
I only
-
II only
-
I and II only
-
II and III only
-
I, II, and III
Question 21
-
ABCDE
-
What is printed as a result of executing the following code segment?
ArrayList
digits = new ArrayList (); for (int k = 0; k <= 9; k++) digits.add("" + k); for (int k = 0; k <= 4; k++) { String d1 = digits.remove(k); String d2 = digits.remove(k); digits.add(k, d1 + "+" + d2); } System.out.println(digits); -
[0+1, 1+2, 2+3, 3+4, 4+5]
-
[0+1, 2+3, 4+5, 6+7, 8+9]
-
[0+1, 1+2, 2+3, 3+4, 5, 6, 7, 8, 9]
-
[0+1, 1+2, 2+3, 3+4, 4+5, 6, 7, 8, 9]
-
[0+0, 1+1, 2+2, 3+3, 4+4, 5, 6, 7, 8, 9]
Question 22
-
ABCDE
-
-
Consider the following method.
/** Returns the number of zeroes in s. * Precondition: s.length() = 31; s holds several * 0's followed by several 1's * (s can also be all 0's or all 1's) */ public int countZeroes(String s) { int i = 0, j = 30; while (i <= j) { int k = (i + j) / 2; if (s.substring(k, k+1).equals("0")) i = k + 1; else j = k - 1; } return i: }
How many iterations through the while loop will be made in the best and the worst case?
Best case Worst case 1 5 1 15 4 5 4 15 5 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started