No, you may not be threatened with contempt or punishment for telling the truth, and judge has NO right to tell you what to say in court: this is a violation of judicial ethics, court rules, and the law. Do not lie and do not say what the judge wants you to say, if it would be bad for you. Tell the truth and if the judge does try to impose punishment, you may well have a civil rights case against the judge and court--if the judge doesn't let you tell the truth or punishes you for doing so, retain a lawyer (if you can) to help bring a case (and take any other appropriate action) or contact an organization like the ACLU to help you.