CS 445

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

What are the two points of that we want to consider a specification from?

1) What is the purpose / effect of the operation in the normal case? 2)What unusual / erroneous situations can occur and how do we handle them?

What can an interface variable be used to reference?

Any object that implements that interface.

Shallow copy

Assign references to the new objects

Preconditions

Can indicate what is assumed to be the state of the ADT prior to the methods execution

Advantages of abstract class

Can use superclass reference to access all subclass objects in a polymorphic way. We can still define any common data and operations in the superclass.

Immutable classes

Classes that do not contain mutator methods (Strings and Wrapper objects).

Two primary methods for creating a class

Composition (Aggregation) and Inheritence

Queue

Data is added to the (logical) end and removed from the (logical) front

Doubly linked list

Each node has two links - one to the successor and one to the predecessor.

How does the remove link bag implementation of remove work?

Find the entry in question. Store a reference to the actual node so that we can do something with it. Then remove t by unlinking the node from the list.

What must be true about this statement with this to be legal? C1.compareTo(C2)

If the object is C1 is class T then the object in C2 must be either class T or a superclass of T.

Postconditions

Indicate what is assumed to be the state of the ADT after the methods execution.

Where is node insertion in the author version of the Linked Bag Implementation

It is at the front of the bag as BagInterface does not specify where to insert.

relationship between ADTs and classes.

There is not a one-to-one mapping from ADTs to classes. A given ADT can be implemented in different ways using different classes. A given class can in fact be used to represent more than one ADT

What are Queues useful for

To set up simulations of real life scenarios.

How to copy java objects

Use a copy constructor or a clone() method.

how does the contains() method work in the authors linked bag implementation?

Uses sequential search like the arraybag, start at the beginning and proceed down the bag. How we move down the bag, How we know when we have reached the end are different.

Inheritence

We build a new class (subclass) by extending a previously defined class (superclass)..

Double-Ended Queue (Deque)

We can add or remove from the "front" of the deque. We can add or remove from the "back" of the deque. We still do not have direct access to the "middle" of the deque

What can we think of the objects we want to sort with the comparable interface as?

We can think of the objects as black boxes - we don't need to know anything else about them.

What must we know in order to sort comparable data with the compareTo interface.

We don't need to know anything about the data other than whether A[i] is less than, equal to or greater than A[j]

Benefits of contiguous memory

We have direct access to individual items

Method Overriding

When a child class overwrites a method inherited from a parent class.

Assume that abstract class Foo has an abstract method called fooMethod(). Also assume that class SubFoo extends Foo. Which statement below is true? a) Class SubFoo must override method fooMethod() b) Class SubFoo must also be an abstract class c) Class SubFoo must either override fooMethod or be declared to be abstract.

c) Class SubFoo must either override fooMethod or be declared to be abstract.

Consider two Java classes, Foo and SubFoo, where SubFoo is a subclass of Foo. Also consider the following Java statements: ArrayList<Foo> L1 = new ArrayList<Foo>(); ArrayList<Foo> L2; Indicate which of the statements below are LEGAL. Indicate ALL legal statements. a) L1.add(new Foo()); b) L1.add(new SubFoo()); c) L1.add(new String("Hello")); d) L2 = new ArrayList<SubFoo>();

a) L1.add(new Foo()); b) L1.add(new SubFoo());

Which of the following are true wrt Java interfaces? Indicate ALL true statements. a) Regular instance methods are all abstract b) Can have instance variables c) Can instantiate objects of an interface type d) A class may implement multiple interfaces

a) Regular instance methods are all abstract d) A class may implement multiple interfaces

In which of the following data types can the data and operations be physically encapsulated together? Select all correct answers. a) String b) int c) BigInteger d) double

a) String c) BigInteger

Consider the SimpleQueue<T> implementation of QueueInterface<T> as discussed in lecture. Assuming that we have a back index to represent the end of the queue, which operations will cause the back index to be changed / modified in the SimpleQueue<T>? Indicate all methods where back will be modified. a. enqueue() b. dequeue() c. getFront() d. clear()

a. enqueue() b. dequeue() d. clear()

Consider the Java code segment below: StringBuilder ​[] A = new StringBuilder​[3]; A​[0] = new StringBuilder("Data"); A​[1] = new StringBuilder("Data"); A​[2] = A​[0]; A​[0].append("Structures"); A​[1].append("Structures"); A​[2].append("Structures"); System.out.println(A​[0].toString() + " and " + A​[1].toString()); What is the output from this code segment? a) DataStructures and DataStructures b) DataStructuresStructures and DataStructuresStructures c)DataStructuresStructures and DataStructures d)DataStructuresStructuresStructures and DataStructuresStructuresStructures

c)DataStructuresStructures and DataStructures

Based on the BagInterface discussed in lecture, if we add() an object that already exists in the bag, what will we do? a. Do nothing -- the item will not be added b. Replace the old copy of the item with the new one c. Add the new object as a separate value in the bag d. Throw an exception

c. Add the new object as a separate value in the bag

How will we represent ADTs in this course? a. As regular Java classes b. As abstract Java classes c. As Java interfaces

c. As Java interfaces

Consider a Java array reference A which is accessing a full array. We want the array accessed by A to be doubled in size. How can we do this? a. A.length = A.length * 2; b. A.resize(2*A.length); c. Assign A to a new array that is double the size of the old and copy the data to the new array.

c. Assign A to a new array that is double the size of the old and copy the data to the new array.

Consider the remove(T anEntry) method as implemented in the LinkedBag<T> class. Choose the best answer below. a. In the normal case, the method removes the Node containing anEntry, thereby removing anEntry b. In the normal case, the method actually removes the front Node rather than the Node containing anEntry. It does this because it saves a lot of time. c. In the normal case, the method actually removes the front Node rather than the Node containing anEntry. However, removing the actual Node containing anEntry would take a comparable amount of time.

c. In the normal case, the method actually removes the front Node rather than the Node containing anEntry. However, removing the actual Node containing anEntry would take a comparable amount of time.

Based on the BagInterface<T>, in the add() method where in the bag should the new item be placed? a. At the back of the array b. At the front of the array c. The BagInterface does not specify where an item should be added.

c. The BagInterface does not specify where an item should be added.

In the remove(T anElement) method in the ArrayBag<T>, how do we fill in the gap created by the removed element? a. We shift the items over to fill in the gap b. We copy the front item of the array into the location of the deleted item c. We copy the back item of the array into the location of the deleted item d. We don't fill in the gap -- we leave the gap there.

c. We copy the back item of the array into the location of the deleted item

Inside our LinkedBag<T> class, we have full access to the Node class. However, outside the LinkedBag<T> class (ex: from the main program) a. We also have full access to the Node class b. We can only access the public methods of the Node class c. We don't even know that the Node class exists

c. We don't even know that the Node class exists

A special case when adding to a LinkedBag<T> occurs a. when adding to an empty bag b. when adding at the end of the bag c. never - the add() method is the same in all cases

c. never - the add() method is the same in all cases

Assume that variable curr is a reference to the current Node within our LinkedBag. We move curr down to the next node in the list with the statement: curr = __________ ;

curr.next

Being able to use a data type without having to know how its data are stored or how its methods are implemented is best described as a) Data Encapsulation b) Composition c) Functional Abstraction d) Data Abstraction e) Polymorphism

d) Data Abstraction

Which is different in the author's LinkedBag<T> versus the author's LList<T> implementations? a. LinkedBag<T> is a singly linked list and LList<T> is a doubly linked list b. LinkedBag<T> and LList<T> have different structures for the Node inner class c. LinkedBag<T> has a reference to the front of the list and LLIst<T> has a reference to the back of the list d. LinkedBag<T> always removes the front Node while LList<T> may remove an arbitrary Node

d. LinkedBag<T> always removes the front Node while LList<T> may remove an arbitrary Node

What is the best description of the relationship between Abstract Data Types (ADTs) and Java classes? a. One ADT can be implemented by one class and one class can implement one ADT b. One ADT can be implemented by many classes and one class can implement one ADT c. One ADT can be implemented by one class and one class can implement many ADTs d. One ADT can be implemented by many classes and one class can implement many ADTs

d. One ADT can be implemented by many classes and one class can implement many ADTs

What is the output for the Java code segment below? StringBuilder B1 = new StringBuilder("Fun"); StringBuilder B2 = B1; B1.append(" Trace"); B2.append(" Code"); System.out.println(B1.toString() + " and " + B2.toString()); a) Fun and Code b) Fun Trace and Code c) Fun Trace and Fun Code d) Fun Trace and Fun Trace Code e) Fun Trace Code and Fun Trace Code f) [ Error -- the code will cause an exception ]

e) Fun Trace Code and Fun Trace Code

Dynamic binding

making a run time decision about which instance method to call.

Which best describes the idea of encapsulation? a) User of a class does not have to know its implementation details b) A class contains both data and methods and access to them can be restricted c) A class can contain instance variables of previously defined classes d) A subclass contains all of the data and methods of its parent (or super) class

b) A class contains both data and methods and access to them can be restricted

Which of the following statements are true about ADTs and classes? Indicate all true statements. a) A given ADT can be implemented by only single class b) A given ADT can be implemented by more than one class c) A given class can implement only a single ADT d) A given class can implement more than one ADT

b) A given ADT can be implemented by more than one class d) A given class can implement more than one ADT

What does the term "dynamic binding" mean or imply? Indicate all that apply. a) A method call is associated with its code (method body) when the program is compiled b) A method call is associated with its code (method body) when the program is executed c) A single method call can result in several different versions of the method actually being invoked. d) Methods with the same name but different parameters can be disambiguated by the compiler

b) A method call is associated with its code (method body) when the program is executed c) A single method call can result in several different versions of the method actually being invoked.

What is different in a method to sort an array of String and a method to sort an array of Integer? a) Nothing is different about them b) The data is compared differently in an array of String than it is in an array of Integer c) The fundamental approach to sorting is different in the two types

b) The data is compared differently in an array of String than it is in an array of Integer

Consider the Java interface Comparable<T> What does T represent in this interface? a) The type of the object from which compareTo() will be called b) The type of the object passed in to compareTo() ​[i.e. the argument type] c) The type of the return value of the compareTo() method

b) The type of the object passed in to compareTo() ​[i.e. the argument type]

Which of the following are NOT legal in Java? ​[Note: To get credit for this question you must indicate ALL responses that are NOT legal] a) static constants in an interface b) instance data in an interface c) A class extending more than once superclass d) A class implementing more than one interface

b) instance data in an interface c) A class extending more than once superclass

In the author's ListInterface<T>, the front position in the list is index a. 0 b. 1 c. getLength() d. ListInterface<T> does not have positional ordering

b. 1

In the normal case, in order to delete item k in an LList<T>, we a. Copy the data from the first Node into Node k, then delete the first Node b. Go to Node k-1, then link Node k-1 to Node k+1, thereby removing Node k c. Shift all of the Nodes from Node k+1 to the end of the list down one location to fill the gap left by Node k d. Set the data value of Node k to null, but leave the Node in the list

b. Go to Node k-1, then link Node k-1 to Node k+1, thereby removing Node k

In the add(T newElement) method in the ArrayBag<T> class, why do we add a new item at the end of the array? a. This is required by the specification in the BagInterface<T> b. It is convenient and efficient to add it there and the interface does not care c. In fact the new item is not added a the end of the array -- it is added in the index to maintain proper ordering of the data

b. It is convenient and efficient to add it there and the interface does not care

Based on our discussion of the resizing process for the ResizableArrayBag, which statement is most appropriate? a. Resizing is transparent to the user and negligible in cost b. Resizing is transparent to the user and potentially time-consuming c. Resizing is visible to the user and negligible in cost d. Resizing is visible to the user and potentially time-consuming

b. Resizing is transparent to the user and potentially time-consuming

In the author's remove(T anElement) method in the LinkedBag<T> class, which node in the list is removed? a. The node where anElement is found b. The front node c. The back node d. No node is actually removed from the list

b. The front node

To delete node i in the author's LList<T> (in the normal case), what do we do? a. Traverse to node i, then get the previous node (i-1) and the next node (i+1) and then link node i-1 to node i+1. b. Traverse to node i-1, then get the next node (i) and the one after that (i+1) and link node i-1 to node i+1 c. Traverse to node i, then copy the data from the front node into node i, then delete the front node.

b. Traverse to node i-1, then get the next node (i) and the one after that (i+1) and link node i-1 to node i+1

Consider an ArrayBag<T> implemented using the following variables: T ​[] bag; int numberOfItems; Choose all correct statements below. a. bag.length is the logical size of the bag and numberOfItems is the physical size of the bag b. bag.length is the physical size of the bag and numberOfItems is the logical size of the bag c. bag.length and numberOfItems must always have the same value d. bag.length is always <= numberOFItems e. bag.length is always >= numberOfItems

b. bag.length is the physical size of the bag and numberOfItems is the logical size of the bag e. bag.length is always >= numberOfItems

References in Java

Behave in a way similar to pointers but with more restriction. Dereferencing is implicit.

Composition

Build a new class using components that are from previously defined classes. "Has a" relationship between new and old classes.

How to implement a Queue and a Deque?

•Both require access / mutation on both ends of a list •Queue: Add on one side, remove on the other Deque:Add or remove on both sides

Fundamental operations of Queue

•enqueue (or offer()) an item to the end of the queue •dequeue (or poll()) an item from the front of the queue •getFront (or peek()) - look at the front item without disturbing it

Drawbacks of contiguous memory

1. Allocation of the memory must be done at once in large blocks 2. Inserting or deleting data "at the middle" of an array may require shifting of the other items.

What is not in an interface?

1. Any specification of the data for the collection 2. Any implementation if the methods.

Characteristics of bag ADT

1. No Rule of how many items to put in 2. No Rule about order of items 3. No rule about duplicate items 4. No Rule about what type of item to put in.

Basics of the list ADT

A collection of objects in a specific order and having the same data type. Does not mean the data is sorted.

Abstract Data Types (ADTs)

A data type whose functionality is seperated from its implementation.

Java interface

A named set of abstract method. Static constants are allowed. Default methods are allowed. Static methods are allowed

Single inheritance

A new class can be a subclass of only one parent (super) class.

Complications of being Immutable

Actions that could be simple as a mutation require more work if a new object must be created..

Deep copy

All nested objects must be copied

Java Memory Use

All objects in Java are allocated dynamically. It is allocated using the new operator. If objects have no more references then they are marked for garbage collection.

Polymorphism

Allows superclass and subclass objects to be accessed in a regular, consistent way. Done Through Method overriding and Dynamic binding,

To put a new node "after" some node X

Link the newNode to the node after X. Link the next value for X to the newNode.

Singly Linked list

Links go in one direction only. We can easily traverse list from front to back. We cannot go backwards through the list. Relatively easy to implement

Contiguous Memory

Location are located next to each other in memory

Node as separate (non-inner) class

Making Node separate allows it to be reused. Node class must also be a parameterized type. Access to next and data fields must now be done via accessors and mutators, so these must be included in the Node<T> class

Logical size of the Bag

Number of items being stored in the bag

Physical size of the Bag

Number of locations within the array.

Idea of linked list/2 parts to a linked list

One part for the data it is storing. Another part to store the location of the next piece.

We say that our Node class is a ___________ data type, because a Node has within it an instance variable of the same type (Node)

Self Referential

Benefits of Node being a private inner class

Since Node is declared within LinkedBag, methods in LinkedBag can access private declarations within Node.

Consider a class Foo with the following instance variables: private StringBuilder ​[] data; private int size; Now consider a copy constructor which does the following: public Foo(Foo oldFoo) { data = new StringBuilder​[oldFoo.length]; for (int i = 0; i < oldFoo.size; i++) data​[i] = oldFoo.data​[i]; size = oldFoo.size; } Is this copy constructor, deep, shallow, or somewhere in between?

Somewhere in between

interfaces

Specify a set of methods, or, more generally a set of behaviors or abilities. Do not specify how those methods are actually implemented. Do not even specify the data upon which the methods depend

What are some restrictions with generic operations

The arguments must be compatible for the objects that are being compared. You cannot compare apples to oranges.

Complications of being mutable

When we add an object to a collection, it doesn't we give up outside access to the object. If we subsequently alter the object "external" to the collection, we could destroy a property of the collection.

Consider an LList<T> with three items in it. Now consider the following operations: 1) remove(1) 2) remove(2) 3) remove(3) Which of the following operations is a special case (i.e. requiring code different from the normal case)? Consider each operation by itself, independent of the other two. Recall that the LList<T> class starts indexing at 1. a 1) only b 3) only c 1) and 3) only d All of 1) 2) and 3) e None of them are special cases

a 1) only

Abstract Class

a class that only exists in a model so subclasses can inherit from it.

Consider the BigInteger class in Java. Which of the following must a user of the class know in order to use it appropriately? Choose all correct answers. a) Basic idea of the data b) Method specifications (ex: parameters and return types) c) Instance variable names and types d) Method implementations (code)

a) Basic idea of the data b) Method specifications (ex: parameters and return types)

Consider the declarations below: public class Foo { public void method1() {} } public class SubFoo extends Foo { public void method2() {} } // main program Foo F = new SubFoo(); SubFoo S = new SubFoo(); Which of the statements below are legal? Indicate all statements that are legal. a) F.method1(); b) F.method2(); c) S.method1(); d) S.method2();

a) F.method1(); c) S.method1(); d) S.method2();

Consider Java classes Foo and SubFoo, where SubFoo is a subclass of Foo. Which of the following Java statements are LEGAL? ​[Note: You must indicate ALL legal statements to get credit for this problem] a) Foo F = new Foo(); b) SubFoo S = new SubFoo(); c) Foo F = new SubFoo(); d) SubFoo S = new Foo();

a) Foo F = new Foo(); b) SubFoo S = new SubFoo(); c) Foo F = new SubFoo();

Consider the following statement:"In the ArrayBag<T> we add() a new item at the end of the array, while in the LinkedBag<T> we add() a new item at the front of the list".Indicate which of the following statements are true ​[Note: All answers must be correct to get credit for this question] a. Accessing both underlying structures from front to rear will show the data in opposite orders (i.e. one will be the reverse of the other) b. Accessing both underlying structures from front to rear will show the data in the same order c. A user of BagInterface<T> needs to know where the add() occurs in order to use the bag correctly. d. A user of BagInterface<T> does not need to know where the add() occurs in order to use the bag correctly

a. Accessing both underlying structures from front to rear will show the data in opposite orders (i.e. one will be the reverse of the other) d. A user of BagInterface<T> does not need to know where the add() occurs in order to use the bag correctly

Consider a circular, singly-linked list, with a single reference to the last node (back) of the list. Which of the following operations can be done simply, without traversing the list? Indicate all that apply. a. Add to the front b. Remove from the front c. Add to the back d. Remove from the back

a. Add to the front b. Remove from the front c. Add to the back

Consider a Java reference A to an array that is storing N items. Which of the actions below MAY require up to N operations to complete? Indicate all correct answers. a. Adding a new item at some index i without changing the relative ordering of any of the existing items b. Adding a new item at the logical end of the array c. Deleting an item from some index i without changing the relative ordering of the remaining items

a. Adding a new item at some index i without changing the relative ordering of any of the existing items b. Adding a new item at the logical end of the array c. Deleting an item from some index i without changing the relative ordering of the remaining items

In the LinkedBag<T> implementation, the add() method puts a new item a. At the front of the list b. At the back of the list c. In the correct positional location within the list

a. At the front of the list

Which of the following is NOT a potential drawback of contiguous memory? a. Direct access to individual locations b. Allocation done in blocks c. Shifting may be needed to fill in gaps d. "Resizing" of arrays

a. Direct access to individual locations

What are some limitations of the ArrayBag<T> (fixed array) implementation of BagInterface<T>? Indicate all limitations. a. May waste memory with a very large array b. May run out of room in the bag c. Overhead in copying data when resizing array

a. May waste memory with a very large array b. May run out of room in the bag

Which of the following are benefits of having the Node class as a separate, public class? Indicate all benefits. a. The same Node class can be used within more than one data structure implementation. b. The encapsulation / access of Node objects can be preserved (so private data is not directly accessed) c. The data and next fields of a given Node can be accessed directly by the LinkedBag<T> class

a. The same Node class can be used within more than one data structure implementation. b. The encapsulation / access of Node objects can be preserved (so private data is not directly accessed)

Considering a linked implementation of the ListInterface<T> with a firstNode and a lastNode reference, which of the following are special cases when removing node i in this modified linked list? Answer all that apply. a. When removing the first (front) node in the list. b. When removing the last (end) node in the list. c. When removing a node in the middle of the list d. When the list has only 1 node remaining

a. When removing the first (front) node in the list. b. When removing the last (end) node in the list. d. When the list has only 1 node remaining

Which of the following are special cases when removing node i in an LList<T>? Answer all that apply. a. When removing the first (front) node in the list. b. When removing the last (end) node in the list. c. When removing a node in the middle of the list d. When the list has only 1 node remaining

a. When removing the first (front) node in the list. d. When the list has only 1 node remaining

If I implement a deque with the logical front being fixed at index 0, which operations will require shifting? Indicate all that apply. a. addToFront() b. addToBack() c. removeFront() d. removeBack()

a. addToFront() c. removeFront()


Kaugnay na mga set ng pag-aaral

Psychology Chapter 5: Sensation and Perception

View Set

Bible 110 (New Testament) GENED Extra Credit

View Set

B. Traditional Whole Life Insurance

View Set

Priority Setting and Frameworks Beginning & Advanced Test

View Set

Chapter 18 Organizational Change and Stress Management

View Set