Actually 4 in this case would be completely valid answer since the net profit at both quantity 4 and quantity 5 is equal $\Pi(q=4)=\Pi(q=5)=40$. I would even argue 4 is potentially better answer.
I think that the reasoning of whoever made that textbook was that given the profits are equal whichever quantity has marginal costs closer to price should be preferred.
In this case the marginal costs at $q=4$ are 17 and $q=5$ are 30 and 30 is just more closer to 28 than 17. Again, I would not say that necessarily the best answer to discrete problem like this, but my guess is they want to teach to always look at point where price is the closest to MC since in optimum it should be equal.
I would not read too much into this exercise. At least this chapter of that online resource is at high-school or at best very simple undergraduate level. At such low level often rigor is sacrificed for the sake of making the material not too difficult in order to not discourage young students. In your future courses/self-study you will get more precision and nuance.