Exam 3 Review Flashcards

1
Q

Recursion requires what two cases?

A

A base case and a recursive case

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

Recursive method for calculating a factorial

A
int fact(int n) {
        int result;
   if(n==1)
     return 1;

   else {
        return n * fact(n-1);
 } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a heap?

A
  • a balanced binary tree

- created using a binary tree

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

Characteristics of a binary search tree

A
  • only 2 children per node

- left subtree must be less, right subtree must be more

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

Pre order

A

root, left subtree, right subtree

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

In order

A

left subtree, root, right subtree

IN COMPARABLE ORDER SMALLEST TO LARGEST

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

Post Order

A

left subtree, right subtree, root

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

Breadth-first

A

based on level… root level, then next level on the left, same level on the right

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

Difference between binary search tree and binary tree?

A

The ordering of the values… for a binary tree it doesn’t matter the order as long as each node only has two nodes at most… for a binary search tree the left node must be less than the root and the right must be greater

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

When adding a value to a BST, what is the first operation? What happens next?

A
  1. run find() to determine where it should be

2. add the node at that position

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

Know how to write the recursive find method

A

Find an element in the tree
• Compare with root, if less traverse left, else traverse right;
repeat
• Stop when found or at a leaf

.. compexity is log n

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

2 properties that heaps have?

A
  • Shape Property ( all leaf nodes are either on the bottom level or the second most bottom level)
  • Order property (the data value stored at a node is less than or equal to the data values stored at all of its descendants)

(root value is always the smallest value int he heap)

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

What are heaps good for?

A
  • finding the maximum or minimum value
  • sorting values into ascending or descending order
  • implementing another data structure (priority queue)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What interface must a class implement to be storable in a heap?

A

comparable

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

What must you do when you add nodes to or remove nodes from a heap?

A

add -> add to the heap then maintain the shape and heap properties (swap around values until the order properties are met)

remove -> remove and return the root then rebuild the heap to SELECT a new root that is the smallest value

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

Why is it important to keep the heap balanced?

A

to maintain the properties

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

What must be there before the CFG is finished?

A

start symbol
variables
terminal symbols
production symbols

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

Know CFGs!

A

a

19
Q

What can the compiler tell right away by loading commands into a parse tree?

A

a

20
Q

Is it good or bad if more than one parse tree can be created from the same String? What is this called?

A

bad ! ambiguity!

21
Q

Look over slides about what will this print in slides

A

aa

22
Q

What is UML? What goes in each box?

A

top: class name
middle: variables
bottom: methods

23
Q

All classes inherit from ____?

A

Object

24
Q

To inherit from a parent class, use the ____ keyword.

A

super

25
Q

Classes that inherit from a parent object have an _____ relationship. What is the difference between this and the other king of relationship?

A

inheritance?

implementation?

26
Q

What is the substitutability principle?

A

child can do what a parent can do

27
Q

Do interfaces extend object?

A

No!

28
Q

When you implement an interface, what are you promising to do?

A

form a contract between the class and the outside world

29
Q

What does an interface allow your class to do?

A

implement various other methods

30
Q

What does the Comparable interface define?

A

a

31
Q

What method must be implemented for a comparable interface?

A

compareTo(Object other)

32
Q

How do interfaces support polymorphism?

A

Yes ! you can implement mulltiple different interfaces

33
Q

Can you write a working program using only abstract classes?

A

a

34
Q

If a class has one abstract method, can it be created?

A

nope… it has to be implemented first

35
Q

If you create a child class that extends the abstract class, can it be implemented?

A

a

36
Q

Which of these algorithms would be a better choice?

O(lg n)
O(n g n)

A

O(lg n) is much more efficient!

37
Q

What is race condition?

A

occurs if the effect of multiple threads on shared

data depends on the order in which the threads are scheduled…

38
Q

What tool is used to handle potential race conditions?

A

lock

39
Q

What is a deadlock?

A

two or more threads are blocked forever waiting for one another

40
Q

Does a single threaded application need locks?

A

no

41
Q

Why does the call unlock() go in the finally code block?

A

so that the last thing that occurs is the lock is taken off so that the next thread can run

42
Q

Review threads!!

A

a

43
Q

min heap vs max heap

A

min heap: root is smalest

max heap: root is largest