Stacks Flashcards

1
Q

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)

A

b) Last-in, first-out (LIFO)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What operation is used to add an item to the top of the stack?
a) Insert
b) Pop
c) Push
d) Append

A

c) Push

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When an item is removed from a stack, what operation is being performed?
a) Push
b) Pop
c) Peek
d) Delete

A

b) Pop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

c) Views the top item without removing it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

b) To allow controlled access and modification of the name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the maximum size of the Stack initially set to if no parameter is passed?
a) 5
b) 10
c) 15
d) 20

A

b) 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which method resizes the items array if the stack exceeds the maximum count?
a) resizeItemsArray()
b) resizeStack()
c) expandArray()
d) extendStack()

A

a) resizeItemsArray()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

b) Java does not allow array resizing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many additional slots are added to the stack’s item array during a resize operation?
a) 3
b) 5
c) 7
d) 10

A

b) 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

c) It is incremented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In the Stack class, which method returns the current number of items in the stack?
a) getMaxCount()
b) getStackCount()
c) stackCount()
d) countItems()

A

b) getStackCount()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

b) To modify how the stack is displayed when printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

b) stackCount should only be changed internally by the Stack class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

c) resizeItemsArray() is called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

b) All items in reverse order with indices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will happen if we call the pop() method on an empty stack?
a) It will return null
b) It will throw an exception
c) It will return a default item
d) It will add a new item

A

b) It will throw an exception

17
Q

What is the purpose of the stack used by the Java Virtual Machine (JVM) during program execution?
a) To manage variable types
b) To control execution order of methods
c) To store user inputs
d) To optimize memory usage

A

b) To control execution order of methods

18
Q

In the DataStructuresExamples class, what type of object is stack?
a) ArrayList
b) Queue
c) Stack
d) HashMap

A

c) Stack

19
Q

Which item would be at the top of the stack after the following operations? stack.push(firstMan); stack.push(apollo13); stack.push(rogueOne);
a) firstMan
b) rogueOne
c) apollo13
d) The stack is empty

A

b) rogueOne

20
Q

If you wanted to access the bottom item in the stack, what operation would you perform?
a) Pop all items until the last one remains
b) Use peek()
c) Set stackCount to zero
d) Access the array directly

A

a) Pop all items until the last one remains