In the US any employer who asks you about personal relationships during an interview is opening themselves up to an illegal discrimination lawsuit.
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.
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”.
IQ testing is uncommon in US employment because it doesn't do a good job of selecting candidates, not because it's unlawful.