CS109: Intro to programming 5
What could you change to make the following code not result in an error? interface A { public int one(); public int two(); public int three(); } public class B implements C { public int one() { return 1; public int two() { return 2; public int three() { return 3; } }
Change ''implements C'' to ''implements A''
What is merge sort based on?
Divide and conquer paradigm
Which type of queue can be used as a queue or a stack?
Double-ended queue
What are the main characteristics of a good Hash Function?
Easy to compute and uniform distribution
When an item is removed from a linked list, what happens to the other element references?
Element indexes are re-ordered.
What is the requirement for an internal sort?
Enough memory to hold all the data.
What does the binarySearch method allow us to do?
Find the position of the item we're looking for in the array
How does Insertion-Sort work?
Finding if each item is in the right position of the array according to the criteria
What are the key things to consider when it comes to using Hash Tables?
Hashing and Collision Resolution
What keyword is used when creating a class that uses an interface?
Implements
Which of the following is a benefit of a binary search?
Increased performance
All the operations on the array have good performance except?
Increasing array size
Which Java language construct is commonly used to create abstract data types?
Interfaces
When you add a new element to the tail of a circularly linked list, what is true of the current tail?
It connects to the head
Why is the Quick Sort considered one of the most efficient sorting algorithms?
It has an average performance of O(n log n)
What is Range sorting?
It's used when you only need some part of an array sorted.
Based on the following Set, which of the following correctly iterates over all items in the Set? Set <Integer> counts = new Treeset ( ) ; counst.add(9343); counts.add(847);
Iterator <Integer> itr = counts.iterator ( ) ; while (itr.hasNext( )) { itr. next( ); }
What is the structure used for storing data in a Hash Table?
Key-value
Stack operation is based on what approach?
LIFO
What is the purpose of the following Java code in context of a doubly-linked list? Node myNode = new Node(element); Node end = head; while(end.next != null) { // Perform some operation or update on the current node System.out.println(end.data); // Example: Print the data of the current node // Move to the next node end = end.next; }
Looping until the last node is found.
If you need to remove an element from the tail of a circular linked list, what special step is needed?
Make sure the tail is really the last item
If you use sorting methods contained in the Java library, do you have to code the sorting algorithm?
No, the library does all the sorting according to the set criteria
Which of the following correctly checks to see if the current node is the head of a doubly linked list?
Node myNode = new Node(element); if(myNode.prev == null) { }
Why would an enhanced for loop not work with a circular linked list?
Nodes are all connected, it would result in infinite loop
The performance of a binary search in terms of Big O Notation is _____, where n is the number of elements in the array.
O(log n)
The average case performance of selection sort is:
O(n^2)
What is Bubble sort performance?
O(n^2)
When computer scientists refer to the Big-O Notation, what are they talking about?
Performance measure of an algorithm
When creating a stack data type in Java, which methods are most appropriate for the interface to the Stack?
Push, pop, peek, size, isEmpty
Which statement is true?
Regular queues use the first-in, first-out structure while priority queues remove elements based on priority.
The following code won't compile. What should be done to correct the problem? Set <String> b = new Hashset ( ) ; b.add("Beetle"); b.add("Spider"); b.add(25.35); b.add("Aphid");
Remove or change the 25.35 to a String
What does the remove method do in regard to priority queues?
Remove the highest priority element in the queue.
What is the function of the following code? tail = head; head = head.next;
Rotating the list
Consider a situation where the minimum number of swap operation is required. Which sorting algorithm is best for this use-case?
Selection Sort
Based on the following snippet of a Set, which of the following declarations would sort it in ascending order? m.add(.25484); m.add(0.0); m.add(3.14159); m.add(-56.75);
Set <Double> m = new Treeset( );
Which implementation method provides the best efficiency?
Sorted list
When would it be most appropriate to create a linked list in Java?
Storing elements in a dynamic array.
The Map studentGrades contains the student IDs and the Student GPAs for the final exam of the year. If you were to store this in a Map data type, what is the key and what is the value?
Student ID is the key and GPA is the value
What is the operation called that is performed by selection sort to move numbers from the unsorted section to the sorted section of an array?
Swapping
The Map data type arrHomes contains home numbers and the number of pets on Broad Street. House number 205 has 3 pets. What code should be executed to print out the number of pets?
System.out.println (arrHomes.get('205'));
The Map busInfo contains bus numbers and the first stop the bus halts at after it leaves the bus depot. The following code was implemented: busInfo.put ('206', 'Maple Street'); busInfo.put ('206', 'Green Town'); What happens after the two lines of code are executed?
The Map data type busInfo() has one key-value pair: 206-Green Town
When do you know that you have completed a binary search?
The key equals the candidate key.
Which is true about an interface definition?
The methods have no bodies.
When it comes to an insertion sort, what does the following really mean? O(N^2)
The processing time for an algorithm is proportional to the square of the data set
Why does an insertion sort take additional system resources to process?
The repeated loops through the array increase processing time
In the nested loop for the insertion sort, why do we decrement?
The sort orders from end to beginning
When using a linked list to represent a stack, which element(s) do you have access to within the stack?
The top (tail)
What does it mean for a data type to be abstract?
The ways its behaviors are implemented are not defined.
The Map data type carPlates contains license plate numbers and car make and model in the state of New York. One of the cars in the Map data type is a Toyota Camry with license plate 12015, and another is a Honda Accord with license plate 50064. When the following code is executed, what gets printed out? System.out.println (carPlates.containsValue('Toyota Camry')):
True
Based on the following code, in what order will the values display after iterating through the set? Set <Integer> j = new Hashset ( ) ; j.add(9343); j.add(847); j.add(-52); j.add(0);
Undetermined until compiled and run
Which of the following statements is true about an unsorted list?
Unsorted lists have no organized order.
What is a collision in Hash Tables?
When the hash function assigns a key that has already been assigned
When selecting a collision resolution method for Hash Tables, when are linked lists better than open addressing?
When the storage utilization is above 80%
Given the following , int mySimpleArray[] ={7,4,6,1,2}; int temp = 0; int n = mySimpleArray.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (mySimpleArray[j] > mySimpleArray[j+1]) { temp = mySimpleArray[j]; mySimpleArray[j] = mySimpleArray[j+1]; mySimpleArray[j+1] = temp; } } } What would the array look like after the complete first pass of the inner for loop of the Bubble Sort algorithm?
[4,6,1,2,7]
The following linked list, called j, exists in your program: [J, U, P, I, T, E, R] If you execute the following code, what will be the final value? j.add(2, "CERES");
[J, U, CERES, P, I, T, E, R]
A Sort Map is _____.
a searchable collection of elements
What method is used to populate a priority queue?
add
Which two methods add an element to the back of the queue?
add, offer
A two-dimensional Java array is:
an array of arrays
When performing a binary search, the search element is compared against a _____.
candidate key
All of the following are associated with queue in Java EXCEPT:
directly defined
A binary search algorithm works on the principle of _____?
divide and conquer
A merge sort _____.
divides an unsorted array into 2 subarrays of equal size.
Bubble sort uses a _____ for-loop structure.
double
What type of sort is Quick Sort?
exchange
Which construct is used by regular queues?
first-in, first-out
Which of the following Java code blocks correctly performs an insertion sort on an array named arr?
for(int i = 1; i < arr.length; i++) { for(int j = i; (j > 0 && arr[j-1] > arr[j]); j--) { int temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } }
What is the test for the end condition in the following recursive piece of code? public void quicksort( int left, int right ) { int i, last; if (left >= right ) return; swap( left, (left + right)/2 ); last = left; for ( i = left+1; i <= right; i++ ) if ( data[i] < data[left] ) swap( ++last, i ); swap( left, last ); quicksort( left, last-1 ); quicksort( last+1, right ); } // quicksort
if (left >= right )
Which of the following Java code snippets correctly checks whether a given Node 'm' is at the end of a linked list?
if(m.next == null)
What is the advantage of selection sort?
it requires no additional memory for operation.
Which is the Java library that will help you access more tools to manipulate and sort arrays?
java.util.Arrays
Which of the following correctly appends a new value specifically to the tail of the following linked list: LinkedList l = new LinkedList(); l.add("M"); l.add("A"); l.add("R");
l.addLast("S");
Which construct is used by stacks?
last-in, first-out
In the following linked list (named m in your code), you need to change the Z to an S. Which code example accomplishes this? [M, A, R, Z];
m.set(3, "S");
If you had a Java class called myDll, how would you create a new doubly linked list instance?
myDll dll = new myDll();
Which of the following Java statements is correct with a queue name of 'myQueue' and the data of type integer?
myQueue.add(319);
In a Java merge sort, the unsorted array is recursively divided into subarrays until the subarray size is _____.
one
Which Queue Interface method retrieves the front element of a queue without removing it?
peek
Which method retrieves but does not remove the element at the head of the deque?
peek
Which Queue Interface method retrieves and removes the front element of a queue?
poll
Which method retrieves and removes the first element from a deque?
pollFirst
Which of the following Java statements best depicts the instantiation of the first element within a stack?
private Node top;
What method is used to add data items to a stack in Java?
push()
Binary search works only on _____ arrays.
sorted
The first step to perform a binary search for a target value in a sorted array is _____?
to identify the middle element of the array
Bubble sort compares _____ elements of an array at a time.
two
Given an unsorted array {28, 32, 4, 15, 12, 45, 25, 33, 22, 56, 47, 11, 6}, what is the correct subarray after 3 recursive calls to merge sort?
{28, 32}
What is the best case complexity of merge sort?
Ω(n log n)
Based on the following Sets and the Iterator method, what will be the first value to display on the screen after the code runs? Set <Double> m = new Treeset ( ) ; m.add(.25484); m.add(0.0); m.add(3.14159); m.add(-56.75); Set <Integer> 1 = new Treeset ( ) ; 1.add(3425359); 1.add(3); 1.add(0); Iterator<Integer> itr = 1. iterator( ); while (itr.hashNext( )) { System.out.println(itr.next( )); }
0
How many times will the outer for loop be executed? int mySimpleArray [] ={1,4,7,5,19,3,11,4,22,8,2,21}; int temp = 0; int n = mySimpleArray.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (mySimpleArray[j] > mySimpleArray[j+1]) { int temp = mySimpleArray[j]; mySimpleArray[j] = mySimpleArray[j+1]; mySimpleArray[j+1] = temp; } } }
11
If we declare a two-dimensional array of size 5 by 5 and the data type stored is 4 bytes long, how much memory will be allocated?
192 bytes
A shop has a number of printers each with a different serial number. Some of the printers are on sale. What gets printed out after the following code is executed: costPrinter.put ("HP101", "$195"); costPrinter.put("HP203", "$204"); costPrinter.put("HP101", "$159"); System.out.println(costPrinter.size()):
2
For a given array of [32, 44, 12, 15], the number of iterations performed using selection sort algorithm are:
3
When we declare a two-dimensional array 10 by 5 of data type 4 bytes long, how much memory will be allocated?
336 bytes
If we declare an array of 12 elements and data type of 2 bytes each, how much memory will be allocated?
40 bytes
What will the list look like if the following code is run: myList.addNodeToHead(75); myList.addNodeToTail(100); myList.addNodeToHead(50);
50, 75, 100
Consider the array [12, 23, 29, 34, 45, 55, 56, 67, 89]. What will be the index of the target value 56 using binary search algorithm?
6
Which of the following is a practical example of a doubly linked list?
A quest in a game that lets users retry stages.
What is missing from the following recursive binary search method? public static int recursiveBinarySearch(int[] sortedArray, int start, int end, int key) { int mid = start + (start - end) / 2; if(key < sortedArray[mid]) { return recursiveBinarySearch(sortedArray, start, mid, key); } else if (key > sortedArray[mid]) { return recursiveBinarySearch(sortedArray, mid+1, end, key); } else { return mid; } return - (start = 1); }
An exit point to break out of the infinite loop
Given the following array, what step is required first before calling the binary search function? int[] scores = {15, 10, 9, 30, 105};
Arrays.sort(scores);
