Short Answer questions Flashcards
How to create an array of objects of different type
We declare the reference variable as the supertype in order for us to be able to call on the objects of its subtypes due to polymorphism.
Difference between Abstract classes and interfaces
Abstract classes can have constants, members and methods without a body and defined methods whereas interfaces can only have constants and method stubs.
What is exception propagation
If no handler is found in a method, Java exits this method, passes the exception to the method that invoked the method, and continues this process to find a handler. Else, the program will terminate and print in error message on the console
How to implement methods to add/remove/etc elements to a collection
What are the different types of linked lists
LinkedLists
Doubly LinkedList
What are the benefits of recursive helper methods
Helper methods recursively solves a problem similar to the original problem and can be used to solve the original problem thus making it more efficient.
Difference between tail-recursive and non-tail methods
Tail: A method that has no pending operations to be performed on return from a recursive call while non-tail methods do.
How are ArrayLists implemented
Internally they are implemented using an array. If needed a larger new array is created to replace the current array.
What is the default type of ArrayList list3 = new ArrayList();
Object
When to use ArrayLIst?
Efficient for getting/setting elements by index
Good for inserting/removing elements near the end(but not the beginning)
Access elements using “Random Access”
When to use LinkedList
Efficient for inserting/removing elements near beginning and end
Efficient when using iterators
Not good for getting/setting elemnts away from beginning/end (Sequential access)
When to use an Array
Array is the most efficient data structure when application doesn’t require insertion or deletion of elements.
Merge sort algorithm
Divide the array into two halves and apply a merge sort on each half recursively. After the two halves are sorted, merge them.
Merge sort runtime efficiency
Average/worst case: O(nlogn)
Quick sort
Select a pivot that divides array into:
part1(elements <= pivot) & part2(elements > pivot)
Then recursively repeat for each part.