Stacks Flashcards
In a stack data structure, what is the primary characteristic of how items are accessed?
a) First-in, first-out (FIFO)
b) Last-in, first-out (LIFO)
c) Random access
d) First-in, last-out (FILO)
b) Last-in, first-out (LIFO)
What operation is used to add an item to the top of the stack?
a) Insert
b) Pop
c) Push
d) Append
c) Push
When an item is removed from a stack, what operation is being performed?
a) Push
b) Pop
c) Peek
d) Delete
b) Pop
What does the peek operation do in a stack?
a) Removes the top item from the stack
b) Adds an item to the stack
c) Views the top item without removing it
d) Clears all items from the stack
c) Views the top item without removing it
In the Item class, what is the purpose of the getter and setter for the name property?
a) To increase security
b) To allow controlled access and modification of the name
c) To store multiple data types
d) To define static variables
b) To allow controlled access and modification of the name
What is the maximum size of the Stack initially set to if no parameter is passed?
a) 5
b) 10
c) 15
d) 20
b) 10
Which method resizes the items array if the stack exceeds the maximum count?
a) resizeItemsArray()
b) resizeStack()
c) expandArray()
d) extendStack()
a) resizeItemsArray()
In Java, why is it necessary to create a new array and copy items when resizing?
a) Arrays in Java are immutable
b) Java does not allow array resizing
c) Collection classes are not available
d) Arrays have fixed data types
b) Java does not allow array resizing
How many additional slots are added to the stack’s item array during a resize operation?
a) 3
b) 5
c) 7
d) 10
b) 5
What happens to the stackCount variable when an item is pushed onto the stack?
a) It is decremented
b) It remains unchanged
c) It is incremented
d) It is reset to zero
c) It is incremented
In the Stack class, which method returns the current number of items in the stack?
a) getMaxCount()
b) getStackCount()
c) stackCount()
d) countItems()
b) getStackCount()
What is the purpose of overriding the toString() method in the Stack class?
a) To change the stack’s data structure
b) To modify how the stack is displayed when printed
c) To reset the stack
d) To perform operations on stack elements
b) To modify how the stack is displayed when printed
Why does the Stack class not provide a setter for stackCount?
a) It does not store any values
b) stackCount should only be changed internally by the Stack class
c) stackCount is public and can be accessed directly
d) stackCount is a constant
b) stackCount should only be changed internally by the Stack class
In a stack, what happens when stackCount exceeds maxCount during a push operation?
a) The stack is reset
b) A new item cannot be added
c) resizeItemsArray() is called
d) Items are shifted downward
c) resizeItemsArray() is called
What does the toString() method in the Stack class display?
a) Only the top item
b) All items in reverse order with indices
c) The size of the stack
d) Only the first item
b) All items in reverse order with indices