Hi Patrick
It's certainly best to avoid splitting questions into two when possible, and in this case it's fairly straightforward.
For one-off questions, you can just customise the template so that before running/calling the student's code it checks it for the disallowed constructs. For example, here's a simple modification to the Java Write a method template to check that the student's code does not contain a for statement.
public class Main {
{{ STUDENT_ANSWER }}
public static void main(String[] args) {
Main main = new Main();
String answer = "{{ STUDENT_ANSWER | e('java')}}";
if (answer.contains("for")) {
System.err.println("You answer contains 'for'!");
} else {
main.runTests();
}
}
public void runTests() {
{{ TEST.testcode }};
}
}
A typical such question and its output might be as follows.
If however they just copied the input method, they'd get:
A more sophisticated version would parse, or at least tokenize, the student's answer rather than just check for the substring "for".
If you're asking many questions of this sort, you'd be better of defining a new question type that uses template parameters to specify what constructs are required and what are forbidden. Most authors seem to wind up using Python for scripting such question types; see for example Peter Sander's question types elsewhere on this forum.