Question Authors' Forum

Testing random (Java) questions

Re: Testing random (Java) questions

by Richard Lobb -
Number of replies: 0

To answer your second question first: the version of Twig in CodeRunner is currently 1.42.3 (2019-08-24). I've held back on updating to a newer version because Twig 2.x needs at least PHP 7.1.3 to run. Many Moodle sites are running on older versions of PHP. The second parameter (max) to the random function was added in Twig 2.7.

Indeed, testing in Twig is a bit of a pain. But with practice you can usually find workarounds. In this particular example you could precompute a table of (min, max, divisor, result) tuples and select one of those at random. See https://github.com/trampgeek/moodle-qtype_coderunner#miscellaneous-tips.

If precomputed parameter sets aren't possible, you might be able to write a solution to the problem in either the template language or the student's answer language (if different). A template grader can then run your answer to get the 'Expected' value of the test and the student's code to get the 'Got' value. But first you have to learn how to write a template grader.

For the maximum flexibility you can use a combinator template grader and run any code you like to construct a suitable result table. Our in-house Python3 question type has a "useanswerfortests" template parameter that, if true, runs the sample answer to fill in the expected field of any test case that doesn't already have a value filled in.

A problem with using template graders in conjunction with randomisation is that the template code isn't run until the student submits their answer. So the For example table can't contain values computed by the template. Even then, there can be workarounds, like using a Twig-computed result for an easy case in the For Example table, leaving all the harder cases for the template grader to compute. Or, you can dispense with the For Example table altogether and describe the problem in more general terms. For example, in your case:

If you have a range of numbers from some minimum min to some maximum max inclusive, and some divisor d, you could compute the sum of all the numbers in that range that are divisible by d using a loop. For example, the sum of the numbers from 3 to 11 divisible by 2 would be 4 + 6+ 8 + 10 = 28. You are to write a program to compute the sum of all the numbers from {{min}} to {{max}} that are exactly divisible by {{divisor}}.

Such an approach is probably better in your example question, anyway, to prevent students submitting an answer of the form (in Python):

print('28')
But in order to grade the student's answer, you would still need to either write a solution in Twig (as you did) or use a template grader that computed the expected result at run time.

Lastly, to answer the question "Should I use a Python template?" my answer would always be yes. But I'm not exactly impartial when it comes to Java versus Python :-)