package main import "fmt" type Stack struct { items []int maxSize int } func (s *Stack) Push(item int) bool { if len(s.items) == s.maxSize { return false } s.items = append(s.items, item) return true } func (s *Stack) Pop() (int, bool) { if len(s.items) == 0 { return 0, false } item := s.items[len(s.items)-1] s.items = s.items[:len(s.items)-1] return item, true } func (s *Stack) IsEmpty() bool { return len(s.items) == 0 } func (s *Stack) IsFull() bool { return len(s.items) == s.maxSize } func main() { stack := Stack{maxSize: 3} fmt.Println(stack.IsEmpty()) stack.Push(10) stack.Push(20) fmt.Println(stack.IsFull()) stack.Push(30) fmt.Println(stack.IsFull()) stack.Pop() fmt.Println(stack.IsFull()) }
The stack starts empty, so IsEmpty() returns true.
After pushing 10 and 20, the stack is not full (size 2 of max 3), so IsFull() returns false.
After pushing 30, the stack reaches max size, so IsFull() returns true.
After popping one item, the stack size decreases, so IsFull() returns false.
The stack is full when the number of items equals the maximum size.
Option A correctly checks this condition.
Other options either check wrong conditions or unrelated values.
func (s *Stack) Pop() int { item := s.items[len(s.items)-1] s.items = s.items[:len(s.items)-1] return item }
If the stack is empty, len(s.items) is 0.
Accessing s.items[len(s.items)-1] means s.items[-1], which is invalid.
This causes a runtime panic with 'index out of range' error.
package main import "fmt" type Stack struct { items []int maxSize int } func (s *Stack) IsEmpty() bool { return len(s.items) == 0 } func (s *Stack) IsFull() bool { return len(s.items) == s.maxSize } func (s *Stack) Push(item int) bool { if s.IsFull() { return false } s.items = append(s.items, item) return true } func main() { stack := Stack{maxSize: 2} fmt.Println(stack.IsEmpty()) stack.Push(5) fmt.Println(stack.IsFull()) stack.Push(10) fmt.Println(stack.IsFull()) stack.Push(15) fmt.Println(stack.IsFull()) }
Initially, stack is empty, so IsEmpty() returns true.
After pushing 5, stack size is 1, not full, so IsFull() returns false.
After pushing 10, stack size is 2, equals maxSize, so IsFull() returns true.
Trying to push 15 fails because stack is full, so size remains 2, IsFull() returns true.
In fixed-size stacks, pushing beyond max size causes errors or corrupts data.
Checking if full prevents overflow and runtime errors.
Other options describe behaviors not typical for fixed-size stacks.
