Consider it this way: The total number of vegetarians is, of course, the sum of male vegetarians and female vegetarians. So you can split the task into two tasks:
- find the number of male vegetarians
- find the number of female vegetarians
Of course for the first task, you'd interview only boys, and for the second task you'd interview only girls.
Now clearly, your estimate for the male vegetarians gets better if you interview more boys, and your estimate for female vegetarians gets better if you interview more girls.
However you've got the additional restriction that the total number of people you interview is 40. That is, the more boys you interview, the less girls you can interview.
For the extreme cases, imagine that you interview only boys. Then you have the best estimate for the number of male vegetarians, but exactly zero information about the number of female vegetarians. Similarly, if you interview only girls, you get the best estimate for the number of female vegetarians, but no information about the male vegetarians.
Now this tradeoff means that there's some specific number of boys and girls where the combined estimate will be best. The weight factors are exactly to determine that specific number.
Note that the more people there are in each group, the more people you have to ask to get a good picture; as an extreme example, if there are only ten girls, by asking nine of them you already get a pretty good estimate of the number of vegetarians (you're at worst off by one), while asking nine of a million won't tell you much. So since there are more boys than girls in the school, it's no surprise that you also should ask more boys than girls.
Now if you just randomly choose 40 people, it might be that you choose the optimal number of boys and girls, but it is not at all guaranteed. Since by definition, you'll get worse result if the number is not optimal, leaving it to chance is obviously a worse option than interviewing the optimal number of each.
There's one exception: If there is no difference between vegetarianism rates for boys and girls, then it doesn't matter whether you select boys or girls. Then just randomly selecting 40 people will be just as good. But it's an assumption you don't really want to make if you can avoid it.