Chapter 2 - Bag Implementations that use Arrays
If an array bag contains the entries "lions", "elephants", "otters", "bears", "tigers", "lemurs" and a call to the remove method with the entry "bears" is made, what does the array look like after remove? a. ""lions", "elephants", "otters", "lemurs", "tigers", null b. "lions", "elephants", "otters", null, "tigers", "lemurs" c. "lions", "elephants", "otters", "tigers", "lemurs", null d. "lions", "elephants", "otters", "tigers", "lemurs"
""lions", "elephants", "otters", "lemurs", "tigers", null
Which instruction suppresses an unchecked-cast warning from the compiler? a. @SuppressWarnings("unchecked") b. @SuppressUnchecked() c. @SuppressUncheckedWarnings() d. @Warning("suppress unchecked")
@SuppressWarnings("unchecked")
It is a good practice to identify a group of core methods to implement and test before continuing with the rest of the class definition. What type of methods should you begin with?
Add, toArray, Constructors, isArrayFull
The Java Class Library method to resize an array is called a. Arrays.copyOf b. Arrays.resize c. Arrays.copy d. Arrays.double
Arrays.copyOf
When defining the bag class write methods that have simple implementation first to get them out of the way.
False
When defining the bag class, you should implement the isArrayFull and remove operations first.
False
When testing a method, you only need to check for arguments that lie within the legal range of their corresponding parameter.
False
You should implement all methods of an ADT implementation before testing to make testing easier.
False
You should never suppress compiler warnings.
False
What is the difference between the numberOfEntries data field in the ArrayBag implementation and the DEFAULT_CAPACITY field?
If the client does not specify the size of the bag, the numberOfEntries field will be set to the DEFAULT_CAPACITY value in the constructor. Otherwise, the numberOfEntries field will be set to the size specified by the client.
What is fail-safe programming?
It is the practice of checking for anticipated errors in your program. Or checking input/validating input
Why doesn't the contains method return the index of a located entry?
Only the programmer needs to know this, client only needs to know that it exists.
Why is it a good security practice to declare the ArrayBag to be a final class?
So that it cannot be tampered with
Why is it a safer practice for the toArray method to return a copy of the array instead of a reference to the array?
So that they cannot access the private variables in the array
A class should not return a reference to an array that is a private data field.
True
A final class is more secure than one that is not final
True
Any methods that a core method might call are part of the core group.
True
When comparing objects in a bag, we assume that the class to which the objects belong defines its own version of equals.
True
Write stubs early in the implementation of an ADT so you can begin testing early.
True
Why is it better to implement the add operation in a collection before implementing the remove operation?
You need to be able to add stuff before being able to remove stuff, its pointless if you can't add.
When implementing the bag ADT, which scenario could result in a security problem? a. a client attempts to create a bag whose capacity exceeds a given limit b. a SecurityException is thrown in the constructor c. an IllegalStateException is thrown in the constructor d. the delete method is implemented before the add method
a client attempts to create a bag whose capacity exceeds a given limit
When implementing the bag ADT, which scenario could result in a security problem? a. a constructor throw an exception or error before completing its initialization b. the programmer validates input data to a method c. generics are used to restrict data types of the entries in the collection d. a group of core methods is not defined
a constructor throw an exception or error before completing its initialization
Which of the following methods is a good candidate for a core method: a. add() b. clear() c. contains() d. remove()
add()
Which of the following is an advantage of using an array to implement the ADT bag? a. adding an entry to a bag is fast b. removing a particular entry requires time to locate the entry c. increasing the size of the array requires time to copy its entries d. the client has control over the size of the bag
adding an entry to a bag is fast
A fixed size array a. has a limited capacity b. can waste memory c. prevents expansion when the bag becomes full d. all of the above
all the above
A stub should a. report that it was invoked by displaying a message b. include a return statement that returns a dummy value c. be written early for testing programs d. all of the above
all the above
In order to resize an array to accommodate a larger bag, you must a. define an alias that references the original array b. create a new array that is larger than the original array and make the alias reference it c. copy the contents of the original array reference by the alias to the new array and discard the original array d. all of the above
all the above
In the remove method, setting the last entry of the array to null a. flags the removed object for garbage collection b. prevents malicious code from accessing it c. is a good security practice d. all of the above
all the above
What are the consequences of returning a reference to the bag array in the toArray method? a. the return variable is an alias for the private instance array variable b. the client will have direct access to the private instance array variable c. the client could change the contents of the private instance array variable without using the public access methods d. all of the above
all the above
When calling the remove method with an argument if there are multiple entries a. exactly which occurrence removed is unspecified b. only one occurrence is removed c. the first occurrence is removed d. all of the above
all the above
Which of the following methods is a good candidate for a core method: a. add() b. toArray() c. isArrayFull() d. all of the above
all the above
A test driver for the bad add method should check for which one of the following a. an over capacity condition b. printing elements of the bag c. adding elements of the correct type d. an empty bag condition
an over capacity condition
Which one of the following Java statements allocates an array in the bag constructor causing a compiler warning for an unchecked operation? Assume capacity is an integer. a. bag = (T[ ]) new Object[capacity]; b. bag = new T[capacity]; c. bag = new Object[capacity]; d. bag = new (T[ ]) Object[capacity];
bag = (T[ ]) new Object[capacity];
Decrementing the private class variable numberOfEntries in a bag a. causes the last entry to be ignored b. causes an IllegalState exception to be thrown c. destroys the integrity of the bag container d. is a security problem
causes the last entry to be ignored
When the need to expand the size of a bag occurs, the common practice is to a. double the size of the array b. increase the size of the array by one to accommodate the new entry c. use the Fibonacci sequence to determine the incremental size of the new array d. prompt the user for how much larger the bag will need to be
double the size of the array
. Which of the following is a disadvantage of using an array to implement the ADT bag? a. increasing the size of the array requires time to copy its entries b. adding an entry to a bag is fast c. removing an unspecified entry from the array is fast d. all of the above
increasing the size of the array requires time to copy its entries
Which one of the following is considered a safe and secure programming practice? a. making no assumptions about the actions of clients and users b. using @SupressWarning ("unchecked") c. adding the comments and headers of the public methods to the class by copying them from the interface d. all of the above
making no assumptions about the actions of clients and users
The method remove that has no parameter in the bag implementation a. removes the last entry in the array b. removes the first entry in the array c. removes a random entry in the array d. none of the above
removes the last entry in the array
The most efficient approach to dealing with a gap left in an array after removing an entry from a bag is to a. replace the entry being removed with the last entry in the array and replace the last entry with null b. replace the entry being removed with the first entry in the array and replace the first entry with null c. shift subsequent entries and replace the duplicate reference to the last entry with null d. replace the entry being removed with null
replace the entry being removed with the last entry in the array and replace the last entry with null
If a bag is empty before the remove method executes, it a. returns null b. throw an exception c. returns an error message d. all of the above
returns null
An incomplete definition of a method is called a _____. a. stub b. core method c. fail-safe method d. security problem
stub
The remove method replaces the removed entry with null because a. the entry could potentially be scheduled for garbage collection b. the client expects a null return value c. it is a fail-safe programming practice d. otherwise it could be considered a duplicate value
the entry could potentially be scheduled for garbage collection
A return value of -1 from the getIndex method indicates a. the entry was not present in the bag b. the entry was found at the end of the array c. the bag was empty d. the bag was full
the entry was not present in the bag
When resizing an array to increase the bag size, if the copy exceeds the maximum memory specified for the computer, the checkCapacity method should a. throw an IllegalStateException b. throw a MaxSizeExceededException c. throw a MaxMemoryExceededException d. return false
throw an IllegalStateException
A stub is created for what purpose? a. to avoid syntax errors b. to avoid duplicate code c. to protect the integrity of the ADT d. to practice fail-safe programming
to avoid syntax errors
In a ResizableArrayBag class, why does the add method always return true? a. to conform to the bag interface b. because the array will always double in size c. returning void is not a fail-safe programming practice d. all of the above
to conform to the bag interface
Which one of the following is considered a safe and secure programming practice? a. validating input data and arguments to a method b. identifying a group of core methods to implement first c. using generic data types d. none of the above
validating input data and arguments to a method