Is there an active community policy to discourage users from replying to questions that later might be closed as off-topic?
No. There is an active community policy to try to close all off-topic questions as and when they arise and before they get answers. Unfortunately we can't get to all of them in time, and we're sometimes late by a few hours / days / weeks / months / years, but when we see an off-topic question which hasn't been closed yet, the question's age (together with the presence or not of answers) does not factor into the decision of whether it should be open or not.
If you want to avoid posting on threads that later get closed, the surefire way is to not post answers to off-topic questions. This can sometimes be tricky, but the example you posted is a textbook case (ho, ho) of blatant homework, and it is very clearly off-topic.
Is there a way to forward comments/ask for clarification from anonymous users who voted on a question or answer? I know that I cannot see who voted on a post, but it would be nice to be able to ask them to give a hint of what I am doing wrong.
No, by design. This is something that we as a community have no control over, and SE has made it repeatedly clear that they are not going to consider changing these mechanisms. The best you can do is to add a polite comment asking for feedback and hope that people answer it. Or, if it's really bad, you can come here to meta and ask.
In this particular case, there's a high chance that the downvotes represent people who think that the question is particularly bad and that by answering such bad questions you're encouraging the OP, and others who see the question, to post similarly bad questions. If you want to avoid this type of downvote, I would recommend not answering bad questions.