Ch2 - Bag Implementations That Use Arrays Complete Test Bank - Data Structures with Java 5e Complete Test Bank by Frank M. Carrano. DOCX document preview.
Chapter 2 - Bag Implementations that use Arrays
True/False (10)
- Any methods that a core method might call are part of the core group.
- You should implement all methods of an ADT implementation before testing to make testing easier.
- You should never suppress compiler warnings.
- When defining the bag class, you should implement the isArrayFull and remove operations first.
- When defining the bag class write methods that have simple implementation first to get them out of the way.
- A class should not return a reference to an array that is a private data field.
- A final class is more secure than one that is not final.
- Write stubs early in the implementation of an ADT so you can begin testing early.
- When testing a method, you only need to check for arguments that lie within the legal range of their corresponding parameter.
- When comparing objects in a bag, we assume that the class to which the objects belong defines its own version of equals.
Short Answer (7)
- 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?
- Why is it better to implement the add operation in a collection before implementing the remove operation?
- What is the difference between the numberOfEntries data field in the ArrayBag implementation and the DEFAULT_CAPACITY field?
- Why is it a safer practice for the toArray method to return a copy of the array instead of a reference to the array?
- What is fail-safe programming?
- Why is it a good security practice to declare the ArrayBag to be a final class?
- Why doesn’t the contains method return the index of a located entry?
Multiple Choice (30) WARNING: CORRECT ANSWERS ARE IN THE SAME POSITION AND TAGGED WITH . YOU SHOULD RANDOMIZE THE LOCATION OF THE CORRECT ANSWERS IN YOUR EXAM.
- Which of the following methods is a good candidate for a core method:
- add()
- clear()
- contains()
- remove()
- Which of the following methods is a good candidate for a core method:
- add()
- toArray()
- isArrayFull()
- all of the above
- 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.
- bag = (T[ ]) new Object[capacity];
- bag = new T[capacity];
- bag = new Object[capacity];
- bag = new (T[ ]) Object[capacity];
- Which instruction suppresses an unchecked-cast warning from the compiler?
- @SuppressWarnings(“unchecked”)
- @SuppressUnchecked()
- @SuppressUncheckedWarnings()
- @Warning(“suppress unchecked”)
- What are the consequences of returning a reference to the bag array in the toArray method?
- the return variable is an alias for the private instance array variable
- the client will have direct access to the private instance array variable
- the client could change the contents of the private instance array variable without using the public access methods
- all of the above
- Which one of the following is considered a safe and secure programming practice?
- validating input data and arguments to a method
- identifying a group of core methods to implement first
- using generic data types
- none of the above
- Which one of the following is considered a safe and secure programming practice?
- making no assumptions about the actions of clients and users
- using @SupressWarning (“unchecked”)
- adding the comments and headers of the public methods to the class by copying them from the interface
- all of the above
- When implementing the bag ADT, which scenario could result in a security problem?
- a constructor throws an exception or error before completing its initialization
- the programmer validates input data to a method
- generics are used to restrict data types of the entries in the collection
- a group of core methods is not defined
- When implementing the bag ADT, which scenario could result in a security problem?
- a client attempts to create a bag whose capacity exceeds a given limit
- a SecurityException is thrown in the constructor
- an IllegalStateException is thrown in the constructor
- the delete method is implemented before the add method
- An incomplete definition of a method is called a _____.
- stub
- core method
- fail-safe method
- security problem
- A stub is created for what purpose?
- to avoid syntax errors
- to avoid duplicate code
- to protect the integrity of the ADT
- to practice fail-safe programming
- A stub should
- report that is was invoked by displaying a message
- include a return statement that returns a dummy value
- be written early for testing programs
- all of the above
- A test driver for the bag add method should check for which one of the following
- an over capacity condition
- printing elements of the bag
- adding elements of the correct type
- an empty bag condition
- The method remove that has no parameter in the bag implementation
- removes the last entry in the array
- removes the first entry in the array
- removes a random entry in the array
- none of the above
- If a bag is empty before the remove method executes, it
- returns null
- throw an exception
- returns an error message
- all of the above
- Decrementing the private class variable numberOfEntries in a bag
- causes the last entry to be ignored
- causes an IllegalState exception to be thrown
- destroys the integrity of the bag container
- is a security problem
- In the remove method, setting the last entry of the array to null
- flags the removed object for garbage collection
- prevents malicious code from accessing it
- is a good security practice
- all of the above
- The remove method replaces the removed entry with null because
- the entry could potentially be scheduled for garbage collection
- the client expects a null return value
- it is a fail-safe programming practice
- otherwise it could be considered a duplicate value
- When calling the remove method with an argument if there are multiple entries
- exactly which occurrence removed is unspecified
- only one occurrence is removed
- the first occurrence is removed
- all of the above
- 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
- replace the entry being removed with the first entry in the array and replace the first entry with null
- shift subsequent entries and replace the duplicate reference to the last entry with null
- replace the entry being removed with null
- 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?
- ““lions”, “elephants”, “otters”, “lemurs”, “tigers”, null
- “lions”, “elephants”, “otters”, null, “tigers”, “lemurs”
- “lions”, “elephants”, “otters”, “tigers”, “lemurs”, null
- “lions”, “elephants”, “otters”, “tigers”, “lemurs”
- A return value of -1 from the getIndex method indicates
- the entry was not present in the bag
- the entry was found at the end of the array
- the bag was empty
- the bag was full
- A fixed size array
- has a limited capacity
- can waste memory
- prevents expansion when the bag becomes full
- all of the above
- In order to resize an array to accommodate a larger bag, you must
- define an alias that references the original array
- create a new array that is larger than the original array and make the alias reference it
- copy the contents of the original array reference by the alias to the new array and discard the original array
- all of the above
- When the need to expand the size of a bag occurs, the common practice is to
- double the size of the array
- increase the size of the array by one to accommodate the new entry
- use the Fibonacci sequence to determine the incremental size of the new array
- prompt the user for how much larger the bag will need to be
- The Java Class Library method to resize an array is called
- Arrays.copyOf
- Arrays.resize
- Arrays.copy
- Arrays.double
- When resizing an array to increase the bag size, if the copy exceeds the maximum memory specified for the computer, the checkCapacity method should
- throw an IllegalStateException
- throw a MaxSizeExceededException
- throw a MaxMemoryExceededException
- return false
- In a ResizableArrayBag class, why does the add method always return true?
- to conform to the bag interface
- because the array will always double in size
- returning void is not a fail-safe programming practice
- all of the above
- Which of the following is an advantage of using an array to implement the ADT bag?
- adding an entry to a bag is fast
- removing a particular entry requires time to locate the entry
- increasing the size of the array requires time to copy its entries
- the client has control over the size of the bag
- Which of the following is a disadvantage of using an array to implement the ADT bag?
- increasing the size of the array requires time to copy its entries
- adding an entry to a bag is fast
- removing an unspecified entry from the array is fast
- all of the above
Document Information
Connected Book
Data Structures with Java 5e Complete Test Bank
By Frank M. Carrano
Test Bank
General
View Product →
Explore recommendations drawn directly from what you're reading
Quick Navigation
Benefits
Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party