Linked Lists Flashcards
What is the primary characteristic of a doubly-linked list?
a) Items are only linked to their next item
b) Items have references to both the next and previous items
c) Items are stored in a fixed-size array
d) Each item has a unique index
b) Items have references to both the next and previous items
What does the setWithOneItem()
method in the LinkedList
class do?
a) Adds multiple items to the list
b) Links the first and last items together
c) Sets the first and last items to the same single item
d) Removes an item from the list
c) Sets the first and last items to the same single item
What is returned by the findItemByName()
method if the specified item is not found?
a) An empty item
b) The first item in the list
c) Null
d) An exception is thrown
c) Null
How does the addItem()
method handle adding an item to a non-empty list?
a) Inserts the item at the last position
b) Replaces the current first item with the new item
c) Links the new item to the first item and updates references
d) Creates a new array to store the items
c) Links the new item to the first item and updates references
What does the removeItemByName()
method do if the item is successfully removed?
a) It throws an exception
b) It decrements the list count and returns true
c) It resets all list properties to null
d) It appends the item to the end of the list
b) It decrements the list count and returns true
What will be the value of the listCount
property after adding three items to an empty LinkedList
?
a) 0
b) 1
c) 2
d) 3
d) 3
What is the purpose of the getNextItem()
method in the Item
class?
a) It adds a new item to the list
b) It removes the next item from the list
c) It retrieves the reference to the next item in the list
d) It resets the next item to null
c) It retrieves the reference to the next item in the list
In a linked list, what happens when addItemAtBack()
is called on an empty list?
a) It does nothing since the list is empty
b) It calls setWithOneItem()
to initialize the list with the new item
c) It throws a NullPointerException
d) It creates a new array to store the item
b) It calls setWithOneItem()
to initialize the list with the new item
What does the toString()
method in the LinkedList
class output?
a) A list of item names in the order they are stored
b) The memory addresses of all items in the list
c) The number of items in the list
d) A graphical representation of the linked list
a) A list of item names in the order they are stored
What does the addItem()
method check before calling setWithOneItem()
?
a) If the list contains duplicate items
b) If the listCount
is zero
c) If the list contains null values
d) If the first item is equal to the last item
b) If the listCount
is zero
How does the findItemByName()
method iterate through the linked list?
a) By using a for loop with an index
b) By following the references using the getNextItem()
method
c) By sorting the items first and then searching
d) By traversing both next and previous references simultaneously
b) By following the references using the getNextItem()
method
What happens if you try to remove an item from the list using removeItemByName()
and the item is not found?
a) An exception is thrown
b) The method returns false
c) The list is reset to its initial state
d) The first item is removed by default
b) The method returns false
What happens when you try to add an item to the back of the list using addItemAtBack()
?
a) The list is cleared before adding the item
b) The new item is linked to the last item and becomes the new last item
c) The list is sorted after adding the item
d) The method removes all previous items from the list
b) The new item is linked to the last item and becomes the new last item
What advantage does a linked list have over an array?
a) It stores items contiguously in memory
b) It allows quick access to items using indices
c) It does not need resizing when adding items
d) It can only store primitive types
c) It does not need resizing when adding items
In which scenario would a circular linked list be useful?
a) When managing a browser’s back and forward buttons
b) When creating a looping playlist in a music player
c) When storing items with unique indices
d) When working with arrays of fixed size
b) When creating a looping playlist in a music player