Data Structures Ozbirn Exam 1 Super Set

Ace your homework & exams now with Quizwiz!

What are the four basic rules when writing recursive routines (in summary)?

Base case, making progress towards the base case, assume recursive calls work without tracing it all out, don't duplicate work already performed in a previous call.

What is the first basic rule of recursion

Basic case: which can be solved without recursion

Upper bound (possibly equal).

Big O

What is the fourth basic rule of recursion?

Compound interest rule: don't duplicate work already performed in a previous call

What is the third step of an inductive proof?

Conclusion: by induction, the theorem holds for all cases i.e. the minimal case and all its successors

What is Big-O rule #3?

Consecutive statements add (which means the maximum one is the one that counts).

What is the third basic rule of recursion?

Design rule: assume recursive calls work without tracing it all out

A queue is known as a

FIFO- First in first out; a queue is another kind of simplified list; add at back, delete/peek at the front; Its like a line at a grocery store; the first one in is the first one that comes out.

Traversing a list of N items using the following loop is O(N) if wordlist is an ArrayList, but O(N2) if it is a LinkedList. for (String str: wordlist) { System.out.println(str); }

False

What is Big-O rule #1?

For-loops. Number iterations (n) times the statements inside.

Why is this good code? public class GenericMemoryCell<AnyType> { public AnyType read() { return storedValue; } public void write(AnyType x) { storedValue = x; } private AnyType storedValue; } public class TestGenericMemoryCell { public static void main(String [] args) { GenericMemoryCell<Integer> m = new GenericMemoryCell<Integer> ( ); m.write( 37 ); int val = m.read(); System.out.println("Cell contents: " + val ); } }

If this is anything but an integer you get a compile error as opposed to runtime error

What is Big-O rule #4?

If-else statements are the test plus the larger of the two branches.

What is the second step of an inductive proof?

Inductive step: a. inductive hypothesis: assume theorem holds for all cases up to some limit k, b. prove the next case for example, k+1

What is inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

A Stack is known as

LIFO- Last in First out; can only add, remove and delete from one end

Upper bound (not equal)

Little o

What is the second basic rule of recursion?

Making progress: each recursive call makes progress towards the base case

What is faster N log N or N?

N

What is faster: N^2 or N log N

N log N

What is faster N^3 or N^2?

N^2

What is faster: 2^N or N^3

N^3

What is Big-O rule #2?

Nested for-loops. Statements inside times the product of loop sizes.

Consider this method header: public static <AnyType extends Comparable<? super AnyType>> AnyType findMax(AnyType [] arr)

Now if Square extends Shape and implements Comparable<Shape>, it is accepted, since Shape is a superclass of Square. Super says the comparable can be on my super class

What is the big-Oh of a merge and quick sort?

O (N log N), faster than N^2, so doubling the list size increases the work by a little more than double

f(n) = 4

O(1)

What methods does the LinkedList use from the List interface?

O(1) add/remove at beginning/end, add/remove if have access to location O(N) add/remove from middle, access/search

What methods does the ArrayList use from List interface?

O(1) add/remove at end, Access O(N) search, add/remove from beginning/middle

What is the big-Oh of a sequential search?

O(N)

What is the Big-O notation of 2N^2 + N + 5

O(N^2)

What is the big-Oh of a insertion and selection sort?

O(N^2), faster than log N so doubling the list size quadruples the amount of work

what is the Big-O notation of N^3 + N^2

O(N^3)

What is the big-Oh of a binary search?

O(log N), faster than O(N), if the number of operations required to solve a problem is proportional to the logarithm of the size of the problem

f(n) = 4log(n)

O(log(n))

f(n) = 3n + 1

O(n)

f(n) = n

O(n)

f(n) = 60n^2 + 5n + 1

O(n^2)

Lower bound (possibly equal).

Omega

What is the difference between Arraylists vs. LinkedLists?

Shifting indexes in array lists will affect the O(N), a linked lists will not do this.

Why is this bad code? public class MemoryCell { public Object read() { return storedValue; } public void write(Object x) { storedValue = x; } private Object storedValue; } public class TestMemoryCell { public static void main(String [] args) { MemoryCell m = new MemoryCell ( ); m.write(new Integer(5)); Integer wval = (Integer) m.read(); int val = wval.intValue(); System.out.println("Cell contents: " + val ); } }

There would be a runtime error if you cast the object the wrong way with m.read();

Growth rates are equal

Theta

T/F T(N) = O(f(N)) means that T(N) <= cf(N) for some constant c and for N >= n0.

True

T/F T(N) = Θ (f(N)) means that T(N) = cf(N) for some constant c and for N >= n0.

True

T/F T(N) = Ω(f(N)) means that T(N) >= cf(N) for some constant c and for N >= n0.

True

The cost of executing merge sort on a list of N items can be characterized by the recursive function: Cost(N) = 2*Cost(N/2) + C1*N + C2

True

Traversing a list of N items using the following loop is O(N) if wordlist is an ArrayList, but O(N2) if it is a LinkedList. for (int i = 0; i < wordlist.size(); i++) { System.out.println(wordlist.get(i)); }

True

T/F GenericMemoryCell<Integer> m = new GenericMemoryCell<Integer> ( ); can be written as GenericMemoryCell<Integer> m = new GenericMemoryCell< > ( );

True, this shows a use of the diamond operator, once the integer has already been specified it does not need typing again

X^a*X^b =

X^(a+b)

X^a/X^b

X^(a-b)

(X^a)^b

X^(ab)

If a Person class has a subclass of Employee, then it is possible to write: Person x = new Employee(); But what about arrays? Person x[] = new Employee[5]; Can you make a person x array to be a new array of employee? Yes, this is allowed

Yes because of the "is a" relationship, these arrays in Java are "covariant", so the assignment works

What would happen if a string and not 30 were passed? public class GenericMemoryCell<AnyType> { public AnyType read() { return storedValue; } public void write(AnyType x) { storedValue = x; } private AnyType storedValue; } public class TestGenericMemoryCell { public static void main(String [] args) { GenericMemoryCell<Integer> m = new GenericMemoryCell<Integer> ( ); m.write( 37 ); int val = m.read(); System.out.println("Cell contents: " + val ); } }

You would get a compile time error

What is a class?

a class defines a new type of object

What is the Java Collections framework?

a collection is an object (i.e., data structure) that holds other objects

What does a List Interface do?

an interface defines a generic template for a class --specifies the methods that the class must implement --but, does not specify fields nor method implementations

What is the first step of an inductive proof?

base case

What is the fastest function?

c Constant

You ______ create a new instance of a generic type

can't

You ________ create an array of generic type

can't

You _________ use a class's type variable in static field's or methods

cannot

What is cohesion?

describes how well a unit of code maps to an entity or behavior

What is coupling?

describes the interconnectedness of classes in a loosely coupled system: each class is largely independent and communicates with other classes via a small, well-defined interface

What are things that are in a class?

fields are variables that belong to the object (and maintain its state) note: "this." is optional, but instructive methods define the actions that can be performed on an object typically public, so can be called by client code a constructor is a special method that automatically initializes the object when it is created can have more than one constructor

Is GenericMemoryCell<int> is legal or illegal?

illegal because you can't use primitive types for generics

For big O model we pretend memory is ________

infinite

public static <AnyType extends Comparable<AnyType>> AnyType findMax(AnyType [] arr) If Shape implements Comparable<Shape>, it is accepted, but if Square extends Shape and implements Comparable<Shape>, _____________

it is not

What can the Collections interface be used for?

java.util.Collections provides a variety of static methods on Lists static int binarySearch(List<T> list, T key) static T max(List<T> list); static T min(List<T> list); these methods can be called on both ArrayLists and LinkedLists

What is faster: log N or log^2 N

log N

what is faster: log^2 N or N

log^2 N

logaB =

logcB / logcA

X^a = B if and only if ______

logxB = A

What consists of Java arrayLists?

must specify content type when declare, capacity is optional (default is 0) can be dynamically expanded/reduced; can easily add/remove from middle

Can you have an array of a generic class?

no

Analyze the following code: public class GenericMemoryCell<AnyType> { public AnyType read() { return storedValue; } public void write(AnyType x) { storedValue = x; } private AnyType storedValue; } if there were a counter as a method, would it be AnyType as well?

no, it would just be an integer, you want 0 1 2 3 ... not a string or whatever else

What does the following mean? comparable< ? super AnyType>

now any subclass of the type can be passed successfully

There be ___ class for all generic invocations.

one

What is polymorphism?

polymorphism applies to classes in an inheritance hierarchy can pass a derived class wherever the parent class is expected the appropriate method for the class is called

What is public vs. private?

private, so can only be accessed from within the class public can be accessed from outside of the class

instanceof and typecasts work only with ___________

raw types

When is recursion useful?

recursion is useful when a task can be broken down into smaller, similar tasks functional recursion: a method directly or indirectly calls itself

Generic classes are seen by the compiler but are converted to _________________________________, this process is known as type erasure.

regular classes (called raw classes) during compilation.

How to search a list?

sequential search traverses the list from beginning to end check each entry in the list if matches the desired entry, then FOUND (return its index) if traverse entire list and no match, then NOT FOUND (return -1) recall: the ArrayList class has indexOf, contains methods

What consists of java arrays?

stored contiguously, with each item accessible via an index must specify content type when declare, size when create once created, the size cannot be changed (without copying entire contents)

What are the benefits of a LinkedList? What makes it better than an ArrayList?

stores elements in a sequence but allows for more efficient interior insertion/deletion elements contain links that reference previous and successor elements in the list

Why do we need a base case?

to prevent from running out of stack space memory

T/F T(N) > o(f(N)) means that T(N) < cf(N) for some constant c and for N >= n0.

true

Proof by Counterexample is __________

where you simply show a case where it isn't true

We look at _______ case, mostly

worst

what is the base case? public static void print(int n) { if (n >= 10) print( n / 10); printDigit( n % 10); }

9 or less

for the following code what is the output: public static int power(int x, int n) { if (n = = 0) return 1; else return x * power(x, n-1); } power(5, 2)

25, = 5*(5 * power(5, 1)) =5*(5 * ( 5 * power(5, 0)

X^n + X^n

2X^n

2^n + 2^n

2^(n+1)

How many basic rules are for when writing recursive routines

4 basic rules

Suppose it took 10 seconds to sort a list of N items using selection sort (where N is a large number). Recall that selection sort is an O(N2) algorithm. Which of the following would be the most likely timing for selection sorting a list of 2N items?

40 seconds

What does the following code print if 6371 is passed in as n? public static void print(int n) { if (n >= 10) print( n / 10); printDigit( n % 10); }

6 ( printDigit(6%10); ) 3 ( printDigit(63%10); ) 7 ( printDigit(637%10); ) 1 ( printDigit(6371%10); )

What does a single linked lists consist of?

-has a front and a back -great for stacks and queues -each node has a data value and an address

What is the step by step process of figuring out the cost of recursion or Big Oh?

1) find relationship between C and T; as long as n> or = 1 2) Take away the negative terms (bc it makes the equation bigger) 3) Multiply the the terms together 4) Add the constants together and you get the term n^3

Suppose it took 10 seconds to sort a list of N items using merge sort (where N is a large number). Recall that merge sort is an O(N log N) algorithm. Which of the following would be the most likely timing for merge sorting a list of 2N items?

22 seconds


Related study sets

SD Life and Health Ch 7 Federal Tax Considerations and Retirement Plans

View Set

31 Reproductive System Assigment

View Set

Environmental Science -Chapter 10

View Set

Principles of Real Estate Chapter 13

View Set