Question
1. Which of the following is true about the linear search algorithm? The algorithm is very fast for large lists of numbers The algorithm can
1.
Which of the following is true about the linear search algorithm?
The algorithm is very fast for large lists of numbers
The algorithm can work on any list or array, but is quite slow for large lists
The algorithm only works for lists or arrays of numerical data (such as Integers or Doubles)
The algorithm only provides the approximate location of an item
The algorithm only works for ordered lists
7.
Consider the following code which appears in another method in the same class:
ArrayLista = new ArrayList (); a.add(1); a.add(2); a.add(3); a.add(4); ArrayList b = new ArrayList (); b.add(4); b.add(3); b.add(2); b.add(1); sort(a); sort(b);
Which of the two calls sort(a), and sort(b) will result in line 8 being executed more times?
Since either call causes an immediate error, no lines of code are executed for either call.
sort(a) will result in line 8 being executed more times.
It is impossible to tell which call will result in line 8 being executed more times.
The two calls will result in line 8 being executed the same number of times.
sort(b) will result in line 8 being executed more times.
8.
Consider the following code segment.
ArrayListfruit = new ArrayList (); fruit.add("banana"); fruit.add("apple"); fruit.add("orange"); fruit.add(fruit.remove(1)); fruit.set(1, fruit.remove(2)); System.out.println(fruit);
What is printed as a result of executing the code segment?
[banana, apple, orange]
[banana, orange, orange, apple]
[orange, orange]
[banana, orange, apple]
[banana, apple]
9.
Consider the following method
public static int search(ArrayListlist, String target) { for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(target)) { return i; } } return -1; }
The following code appears in the main method of the same class:
ArrayListvals = new ArrayList (); vals.add("define"); vals.add("explain"); vals.add("justify"); vals.add("explain"); vals.add("determine"); vals.add("justify"); System.out.println(search(vals, "explain"));
What is printed when this code is executed?
2
3
-1
1
4
17.
Consider the following code segment:
ArrayListlist = new ArrayList (); list.add("Cookies"); list.add("nachos"); list.add("chips"); list.add("Trail mix"); list.add("Celery"); for (String s : list) { if (s.toUpperCase().substring(0, 1).equals(s.substring(0, 1))) { System.out.print(s + " "); } }
What is printed as a result of executing the code segment?
Cookies Trail mix Celery
Nothing is printed
Cookies nachos chips Trail mix Celery
Cookies chips Celery
nachos chips
18.
Consider the following declaration for an ArrayList:
ArrayListlist = new ArrayList ();
After values have been added to the array, the following segment processes the ArrayList:
list.add(list.get(0)); list.remove(0);
Which of the following best describes what this segment does?
Does not change the Strings in the ArrayList.
Removes the first letter in each String in the ArrayList.
Move the first String in the ArrayList to the end of the ArrayList.
Adds the first letter in the String onto the end.
Adds the last letter in the String onto the beginning.
20.
The following sort method correctly sorts the integers in elements into ascending order.
Line 1: public static void sort(int[] elements) Line 2: { Line 3: for (int j = 1; j < elements.length; j++) Line 4: { Line 5: int temp = elements[j]; Line 6: int possibleIndex = j; Line 7: while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) Line 8: { Line 9: elements[possibleIndex] = elements[possibleIndex - 1]; Line 10: possibleIndex--; Line 11: } Line 12: elements[possibleIndex] = temp; Line 13: } Line 14: }
Consider the following three proposed changes to the code:
Change 1 Replace line 3 with:
Line 3: for (int j = elements.length - 2; j >= 0; j--)
Change 2 Replace line 7 with:
Line 7: while (possibleIndex > 0 && temp > elements[possibleIndex - 1])
Change 3 Replace line 7 with:
Line 7: while (possibleIndex < elements.length - 1 && temp < elements[possibleIndex + 1])
and replace lines 9-10 with:
Line 9: elements[possibleIndex] = elements[possibleIndex + 1]; Line 10: possibleIndex++;
Suppose that you wish to change the code so that it correctly sorts the integers in elements into descending order rather than ascending order. Which of the following best describes which combinations of the proposed changes would achieve this goal?
ONLY enacting changes 1 and 2 together
ONLY enacting change 2 by itself
Enacting any of the three changes individually
Enacting changes 1 and 2 together, or enacting change 3 by itself
Enacting changes 1 and 3 together, or enacting change 2 by itself
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