I'm currently reviewing for my OCPJP 6 using the Sierra & Bates reviewer. I stumbled upon a question regarding an endless loop not throwing a StackOverflowError. As far as I've learned, it should throw it eventually.
Please refer to this PDF for the question: https://java.net/downloads/jfjug/SCJP%20Sun%20Certified%20Programmer%20for%20Java%206-0071591060.pdf
Question I'm referring to is from Self Test Chapter 5 Question 9 (page 455 of the PDF).
I answered, CDF. The correct answer, according to the book was DF. It was also explained in there that case 0 initiates an endless loop, not a StackOverflowError.
True, it does initiate an endless loop, but eventually turn out to be a StackOverflowError. The answer C stated "might throw a StackOverflowError" so I knew C was correct.
If I'm wrong, can anybody explain why?
Since, in that loop, you're not actually calling methods that need to call other methods (a la recursion), you're not adding more calls to the stack. You're merely repeating the steps you did most every time through.
Since a StackOverflowError
is only invoked in certain conditions - namely, the calling of another method (which would call more methods), or the allocation of more elements onto the stack, then there's really no way that this particular loop could cause such an error.