Short Answer questions Flashcards

1
Q

How to create an array of objects of different type

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Difference between Abstract classes and interfaces

A

Abstract classes can have constants, members and methods without a body and defined methods whereas interfaces can only have constants and method stubs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is exception propagation

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to implement methods to add/remove/etc elements to a collection

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of linked lists

A

LinkedLists

Doubly LinkedList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the benefits of recursive helper methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Difference between tail-recursive and non-tail methods

A

Tail: A method that has no pending operations to be performed on return from a recursive call while non-tail methods do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are ArrayLists implemented

A

Internally they are implemented using an array. If needed a larger new array is created to replace the current array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the default type of ArrayList list3 = new ArrayList();

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When to use ArrayLIst?

A

Efficient for getting/setting elements by index
Good for inserting/removing elements near the end(but not the beginning)
Access elements using “Random Access”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When to use LinkedList

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When to use an Array

A

Array is the most efficient data structure when application doesn’t require insertion or deletion of elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Merge sort algorithm

A

Divide the array into two halves and apply a merge sort on each half recursively. After the two halves are sorted, merge them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Merge sort runtime efficiency

A

Average/worst case: O(nlogn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Quick sort

A

Select a pivot that divides array into:
part1(elements <= pivot) & part2(elements > pivot)
Then recursively repeat for each part.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Quick sort runtime efficiency

A

Best/Average: O(nlogn)

worst: O(n^2) rare

17
Q

What is the best case for quick sort

A

When the pivot divides the array each time into two parts of about the same size.

18
Q

When does the worst case of quick sort occur

A

When either the smallest or largest elements is always chosen as the pivot.