CS 2114 Exam 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

When to use abstract classes?

If you want to provide a method definition Declare a private data field that your classes will have in common

Adding a node at the beginning

Node newNode = new Node(); newNode.data = 8; newNode.next = head; head = newNode;

Removing a node

p.next = q.next; q = p.next; for when q is right after p and you want to remove what q references

Assume a Queue class has been implemented as a circular array with one unused location. Next assume an instance of the Queue has been constructed with a default capacity of six, storing String references. Then the following six strings are inserted, (i.e., enqueued), into the Queue: "!", "@", "#", "$", "%", "?". Now the front of the queue is deleted, (i.e., dequeued) and another string, "&", is inserted (i.e., enqueued) Again the front of the queue is deleted and another string is immediately inserted. Assume this pattern, (dequeue, enqueue ...) paired operations is repeated an arbitrary number of times. Which of the following expressions best describes the resulting relationship between the frontIndex (first element in the queue) and backIndex (last element in the queue) indices of the queue after the final enqueue operation?

((backIndex + 2) % 7) == frontIndex

Of the two sorting algorithms we studied in lecture, which would do less work, in terms of comparisons and swaps, if executed upon an already sorted array?

--> Insertion Sort Not Selection Sort

What index does a stack's topINdex start at

-1

Lever-order traversal

Begin at root and visit nodes one level at a time

Where should you add nodes in a list

Beginning

Binary search efficiencies

Best case: O(1) Worst/average case: O(log n)

Insertion Sort Cases

Best: O(n) Average: O(n^2)

Protected methods

Can only be accessed in their own class, classes in the same package, or by any subclass

Downcast

Cast superclass to subclass type; checked with ClassCastException

Universal O(1) methods

Clear getLength isEmpty

Node class fields

Data = int Next = node

To implement a Deque(double ended queue) with a firstNode and a lastNode reference that only adds and removes data on the ends, which of the following underlying representations would result in the most efficient implementation?

Double linked chain

Easiest part of a stack array to access

End of the array

Abstract class vs interface

Extend class Implement interface

Fairness

How can I act equitably and balance legitimate interests?

Liberty

How does respect for freedom, personal autonomy, or consent apply?

Interface vs Abstract Class

Interface is not a class Class can implement many interfaces but one class

2 approaches for allowing an instance of a class to have multiple types

Interfaces and Inheritance

The use of a generic data type is preferred over using Object as a general class in a collection because

It ensures that all the members of the collection are objects related by inheritance.

Advantage(s) of an inner iterator class over a separate, (non-inner), iterator class:

It has direct access to the ADT's data so it can execute more efficiently

Extensibility

New types can be added later without having to change code that references only the interface or parent class types

Confidentiality Goal

No information leaking

Availability Goal

No interruption of service

Assurance

No lying

Authenticity Goal

No lying

Integrity Goal

No tampering

Anonymity

No tracking

Is the bubble sort useful

No, O(n^2)

Does casting change an object

No, it creates an anonymous reference to an object

In a circular array-based implementation of a queue, what is the efficiency performance of the dequeue operation ?

O(1)

Efficiency of a method that iterates through an array with 2 separate loops

O(n)

For an array implementation of a list, the worst case efficiency of remove(index) is:

O(n)

What is the big-Oh complexity of getEntry(position) on a list that is implemented with a singly linked chain?

O(n)

Sorted List efficiency

O(n) for everything except getEntry(position) which is O(1) for the array sorted list

Casting

Obtains a reference of a different type, but does not change the object

Does an iterative loop or recursive method use more memory?

Recursion

.remove

Removes first node

.remove()

Removes last element in a array

Inorder Tree Traversal

Sorts from left to right

Postorder Tree Traversal

Start with lowest level on left, then right, then work up

Object()

The superclass of every class

What does JVM look at during runtime

The type of the objet to determie which method implementation to call

What is checked at compile time

The variable type to make sure that any methods invoked actually exist

Pre-Order Traversal

Visit root before we visit root's subtrees

Post order traversal

Visit root of a binary tree after visiting nodes in root's subtrees

In-Order Traversal

Visit root of a binary tree between visiting nodes in root's subtrees.

Preorder Tree Traversal

Visiting any given node before visiting its children Start at root, keep going left until there are no left nodes, then go right at lowest point, then keep going up

Outcomes

What achieves the best short- and long-term outcomes for me and all others?

Character

What action best reflects who I am and the person I want to become?

Authority

What do legitimate authorities (e.g. experts, law, my religion/god) expect of me?

Responsibilities

What duties and/or obligations apply?

Rights

What rights (e.g. innate, legal, social) apply?

Empathy

What would I do if I cared deeply about those involved?

For the list, [20, 40, 10, 30], what will the array look like after the second pass of selection sort?

[10, 20, 40, 30]

generic data type

a type for which the operations are defined but the types of the items being manipulated are not enable you to write a placeholder instead of an actual class type

Which of the following List member methods should not be implemented for a SortedList?

add (int newPosition, T newEntry)

Which bag behavior(s) would need to be modified when implementing a set?

add()

Different methods in sorted lists

add() remove(anEntry) getPosition(anEntry) no replace() method

Implementation

all the data fields and details of the method definitions

O(1)

an action is independent of the problem size

A key benefit of inheritance is

an object can have several types, it is type compatible with any of its superclasses

Wrapping around an array queue

backIndex = (backIndex + 1) % queue.length to increment backIndex

Encapsulation

design principle that encloses data and methods within a class, thereby hiding the class' implementation details

Abstraction

focus on what and not how

Dequeueing a full array queue

frontIndex = (frontIndex + 1) % queue.length

Bag Efficiencies

getFrequencyOf - O(n) clear - O(n)

O(n^2)

happens when there is a nested for loop, among other things

To remove a specified entry of a bag that is implemented with a linked chain, first get a reference to the node to remove, then the most efficient approach is to:

move the data in the first node to the referenced node , then remove the first node

compareTo values

negative if this is less than parameter value

The pro(s) of using an array are

o Adding an entry to the bag is fast o Removing an unspecified entry is fast

Pros of using a chain

o Bag can grow and shrink in size as necessary o Remove and recycle nodes that are no longer needed o Adding new entry to end of array or to beginning of chain both relatively simple o Similar for removal

The con(s) of using an array are

o Removing a particular entry requires time to locate the entry o Increasing the size of the array requires time to copy its entries

Cons of using a chain

o Removing specific entry requires search of array or chain o Chain requires more memory than array of same length

Benefit of no node class

o Simplicity o Implement all operations recursively

Which of the following visibility modifiers should be used for a field in a class C if the programmer does not want the subclass D to have direct access to the field?

private

The most efficient approach to dealing with a gap left in an array after removing an entry from a bag is to

replace the entry being removed with the last entry in the array and replace the last entry with null

When too many recursive calls are made, creating more activation records than the allocated program memory can handle, what kind of error/exception occurs?

stack overflow

For an array implementation of a stack, the most efficient choice for the top location is

the last element in the array

If a generic method is declared with type parameter <T extends Comparable<T>> and local variables value and target, both of type T, are given, then the following code can be used within the method:

value.compareTo(target)

Client Interface

what client code needs to know to use a class

Null Pointer Exception

when a program attempts to run a method with a null object Most common exception

Polymorphism

when the type of the instance is determined at runtime

Recursive vs Iterative Search

· Iteration saves time and space · Recursion does not require much additional space for recursive calls · Coding binary search recursively is somewhat easier

generic placeholder

• A generic data type • A type parameter

Tree efficiency

• Add, remove, and getEntry are O(h) for height h • Full and complete trees give O(log n) performance

Why use an iterator

• getEntry(index) can be O(n^2) with a linked chain, but an iterator makes it O(n) • Can traverse all the data in a collection like a bag or set, even without access to indexed elements


Kaugnay na mga set ng pag-aaral

ATI Questions for Meds, Older Adult, Sensory, and CAT

View Set

Network+Guide Chapter-6 Review Question

View Set

decimal, percentage and fractions

View Set

Chapter 58: Caring for Clients with Disorders of the Kidneys and Ureters

View Set

hesi evolve questions (respiratory, cardio)

View Set