Asking personal questions could be seen as a way to elicit information about a protected status and thus give a rejected candidate ammunition for a claim, whether warranted or not.
It’s best to just keep questions focused on the workplace.
Even in the case of complete innocence, it often becomes a he-said-she-said situation, and the outcome boils down to which side presents the best set of “facts”.
I use quotes there because my broader experience with the court system routinely shows that it does not need be burdened by the “truth” or “facts”. That is probably because the regular cast in those venues are literally trained and practiced liars.
But if you're at a large enough company, you're absolutely getting sued for this from time to time, so you'll have the "how to not get sued" training before you're allowed to interview.
(Edit: this isn't limited to interviews. There's many, many examples of things that large companies will not touch due to legal risk, that smaller companies will... either due to lack of knowledge on the legal risk (maybe no legal department even exists yet?) or intentionally as a gamble)
It is illegal, and in my book also immoral to deny such a candidate, but the other side of the coin is there.
[1] https://en.wikipedia.org/wiki/Griggs_v._Duke_Power_Co.
[2] https://www.wsj.com/us-news/education/black-latino-teachers-...
[3] https://teachercertification.com/nystce/multi-subject-arts-a...
The specific cases you mention and the finer point is how do you demonstrate the necessity of a measure? Is high general IQ absolutely necessary for SWEs? Or is it enough to have a high logical reasoning, but don’t need spatial? Do you really need high IQ or is it enough to have a lot of practical experience with hands on skills? Do you need higher IQ to do zero to one development vs code maintenance? The devil’s always in the details with these kinds of questions, and it’s definitely not a blanket “you can’t use anything”.