ap comp-sci a unit 7

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How does Selection Sort work to sort an array? Selection Sort swaps the first value of an array with the last index of an array, then swaps the second value in an array with the second to last value, until all values have swapped places. Selection Sort iterates through each index and swaps the current index with the minimum value that exists in the indices greater than the current index. Selection Sort sets the first index as sorted, and shifts each minimum value into the correct position by finding the lowest value on the sorted side of the array at indices lower than the current index. Selection Sort finds the lowest value in an array and shifts all elements to the right of that value.

2

True or False: Attempting to add an int or double primitive value to an ArrayList will result in an error. True False Answered

2

True or False: Linear Search becomes more efficient as more values are added to a particular data set. True False

2

What is one of the primary differences between an ArrayList and an Array? ArrayLists have a fixed number of values, while Arrays can change in size. Arrays have a fixed number of values, while ArrayLists can change in size. Arrays are only able to store objects, while ArrayLists can store primitive and object values. ArrayLists are lists, while arrays are data structures.

2

What is wrong with the following code? ArrayList<int> list = new ArrayList<int>(); list.add(1); System.out.println(list.get(0)); It will throw an IndexOutOfBoundsException. You cannot use int as the type in an ArrayList. list is a reserved word, so you can't call the variable that. You need to define the new ArrayList on a new line of code. Nothing. The above code is correct.

2

What value will be printed in the console after this series of commands? Assume that the ArrayList package has been imported. ArrayList<Integer> array = new ArrayList<Integer>(); array.add(3); array.add(9); array.add(10); array.remove(1); array.set(0, 3); int num = array.get(1); System.out.println(array.size()); 1 2 3 There will be an error caused by an incorrect use of ArrayList Methods.

2

Which of the following are some good ways to protect your own personal data? I. Use privacy settings to limit exposureII. Review posts you are tagged in and take action if neededIII. Permanently delete old social media accounts you don't useIV. Google yourself on a regular basisV. Use strong passwordsVi. Stay on sites with http:// I, II, and III I-V all IV, V, and VI I- VI all

2

Will this method correctly traverse an ArrayList and remove all multiples of 3? public void remove3s(ArrayList<Integer> array) { int counter = 0; while(counter < array.size()) { if(array.get(counter) %3 == 0) { array.remove(counter); counter++; } else { counter++; } } } Yes, this method is written correctly, and will remove all multiples of 3 in an ArrayList. No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList. No, this method is not written correctly, as the counter in the else statement will skip the next value, as the values will shift down in the ArrayList. No, this method will not work because the methods used to access the ArrayList are incorrect

2

rue or False: Enhanced for loops should not be used to traverse ArrayLists. True False

2

An ArrayList nums, is initialized with the values [1, 2, 4, 3, 4, 5]. What value would be returned from the call search(nums, 4)? -1 2 4 This search method is written improperly, and will result in an error. 3

3

ArrayList<Integer> array = new ArrayList<Integer>(); array.add(3); array.add(9); array.add(10); array.remove(1); array.set(0, 3); int num = array.get(1); System.out.println(num); 3 9 10 2

3

ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); nums.add(20); nums.add(30); nums.add(40); nums.add(50); int x = nums.remove(3); int y = x + nums.remove(0); int z = x + y; nums.add(2, z); Which of the following represents the value of nums after the code segment has been executed? [20, 40, 90, 50] [10, 20, 30, 40, 50] [20, 30, 90, 50] [20, 40, 50, 90] [20, 30, 80, 50]

3

How many times is the statement on Line 13 executed as a result of the call to insertionSort? 5 30 15 16 6

3

What is printed when the code segment is executed? [9, 8, 7, 6, 5] [10, 9, 8, 7, 6] [9, 8, 7, 6] [9, 8, 7, 6, 4, 3] Nothing will print, as the while loop condition occurs infinitely.

3

What is printed when the code segment is executed? [] ["Two", "Four"] ["Two", "Four", "Six"] ["Four", "Five", "Six"] ["Six"]

3

What will the following code print? ArrayList<String> list = new ArrayList<String>(); list.add("Hello"); list.add("World"); System.out.println(list[0]); "Hello" "World" "list[0]" There will be a compiler error None of the above

3

new ArrayList<String>; II. new ArrayList(); III. new ArrayList<String>(); I only III only I and III II and III I, II, and III

3

ArrayList<String> names = new ArrayList<String>(); names.add("Michael"); names.add("Lebron"); names.add("Serena"); names.add("Shaq"); String firstName = names.remove(0); String secondName = names.set(0, "Venus"); names.add(2, firstName); System.out.println("Names:" + secondName + " and " + names.get(1)); Names: Lebron and Michael Names: Venus and Serena Names: Michael and Shaq Names: Lebron and Serena This will cause an error, as names.remove cannot be assigned to a value.

4

How many comparisons will the selection sort algorithm make on an array of 8 elements? 3 8 24 28 64

4

Some good and practical ways to avoid a phishing attempt are to I. Not click through any links in an email that seem suspiciousII. Not download any attachments in an email that seem suspiciousIII. Not use email services at allIV. Consider the source of each emailV. Consider if the email contents sound too good to be true, like you have won a prize or something I and II IV and V I - V all I, II, IV and V

4

Which of the following only has beneficial impacts on our society? I. CollaborationII. CommunicationIII. Sharing of informationIV. Anonymity I and II III IV None of these

4

ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(10); list.add(15); list.add(20); list.add(25); int counter = 0; while(counter < list.size()) { counter++; list.set(counter, list.get(counter)+5); } System.out.println(list.toString()); What is printed as a result of this code segment? [10, 15, 20, 25, 30] [5, 15, 20, 25, 30] [1, 2, 3, 4, 5] [5, 10, 15, 20, 30] An IndexOutofBoundsException occurs. Continue

5

The method as written, however, is incorrect. Which ArrayList(s) list would prove that this method was written incorrectly? I.[1, 2, 3, 4, 5]II.[2, 4, 5, 6, 7]III.[2, 4, 6, 8, 10]IV.[2, 5, 6, 7, 8] III only II and IV II only I and IV II and III

5

ArrayList<String> scales = new ArrayList<String>(); scales.add("DO"); scales.add("RE"); scales.add("MI"); scales.add("FA"); scales.add("SO"); String swap = scales.get(2); scales.remove(2); String set = scales.remove(scales.size()-1); scales.add(scales.get(0)); scales.set(0,set); scales.add(scales.size()/2, swap); Which of the following represents the value of scales after the code has been executed? [SO, RE, FA, DO] [SO, RE, MI, FA, DO] [SO, RE, MI, DO] [FA, RE, MI, DO] [FA, SO, RE, MI, DO]

2

Given an ArrayList initialized with the values [6, 5, 4, 3, 2, 1], how many times does Line 19 execute when the ArrayList is sorted using the selection sort algorithm? 6 3 5 30 4

2

website

selection sort

Change Line 10 to if(arr.get(j) > arr.get(currentMinIndex))

..

How does Linear Search work? Linear Search traverses an array until the desired value is found, or until the end of the array. Linear Search traverses an array from the last value to the first value until the desired value is found. Linear Search uses a while loop to traverse an array for the desired value, or until the end of the array. Linear Search searches for desired values by searching the the next highest value in an array, and seeing if that is the desired value.

1


Kaugnay na mga set ng pag-aaral

Maternity and Women's Health- Pregnancy, Uncomplicated

View Set

Upper Limb Questions (Chapter 6)

View Set

Clinical Microbiology Exam 1 (CH 9)

View Set