Junior — Middle
How does a StackOverflow exception occur in a program?
sobes.tech AI
Answer from AI
A StackOverflow exception occurs when the program's call stack overflows. This happens if recursive function calls or deep nested calls lack a proper exit condition or are too deep.
The call stack is a memory area where return addresses and local variables for functions are stored. If a function calls itself infinitely or too many times without an exit, the stack fills up and overflows.
Example of infinite recursion in Python:
def recursive():
return recursive()
recursive() # will cause a RecursionError (StackOverflow)
In QA, it's important to understand that StackOverflow is a runtime error that can lead to program or test crash.