More Question Writing Examples

Purpose of this document

This document contains example IMathAS questions with explanation of the code used. For detailed question language reference, please refer to the help file.

Example of Function type

Common Control

$a,$b = nonzerodiffrands(-8,8,2)
$variables = "x"
$domain = "-5,5"

The first line defines two variables, $a and $b, as different, nonzero random integers. The first two arguments specify that the integers should be chosen between -8 and 8. The third argument specifies that 2 random integers should be chosen

$variables is used to define the variables in the expression. If more than one variable is used, enter a list of variables, like $variables = "x,y,x". This defaults to "x", so this line is not really necessary in this problem.

$domain specifies the domain on which the student's answer should be compared to the given answer. Enter as a list "min,max". The same domain will apply to all variables in the expression. This defaults to -10 to 10

Question Control

$ansprompt = "Ans="

Rather than place the $answerbox in the question text, I'm going to have the system place the default answer box at the end of the question. The $ansprompt variable specifies that the box should have "Ans=" displayed in front of the answer box

Question Text

Simplify `x^$a/x^$b`

Write your answer with positive exponents only.


$p = abs($a - $b)
$answer = "x^($p)" if ($a>$b)
$answer = "1/x^($p)" if ($a<$b)

$showanswer = "x^($a-$b) = $answer"

$requiretimes = "^,<2,-,=0"

The first three lines define the answer. Note that it would have worked just fine to define $answer = makepretty("x^($a-$b)"), but because I want to use the answer in the $showanswer to show students later, I instead defined the answer using "if" statements. The "if" allows you define different values to a variable depending on values of other variables

The $showanswer line defines the answer to show to students. There is no default value for Function type questions, so you must specify something if you want an answer to be available to students. In this case, I showed the first step as well as the answer

$requiretimes places format requirements on the student's answer. The list in quotes is in pairs; the first value is the symbol to look for, and the second value indicates the number of times that symbol should appear. In this example, the ^ symbol should show up less than two times, and the - symbol should show up zero times. The first rule requires that students cannot simply reenter the original expression and get credit. The second rule requires that students cannot enter negative exponents

Example of Matching type

Common Control

$qarr = array("`sin x`","`cos x`","`x^2`","`x^3`","`e^x`","`log x`","`2^x`")
$aarr = array("`cos x`","`-sin x`","`2x`","`3x^2`","`e^x`","`1/x`","`2^x ln2`")

$questions,$answers = jointshuffle($qarr,$aarr,4,5)

$questiontitle = "`f(x)`";
$answertitle = "`f'(x)`";

The first two lines define arrays of functions ($qarr) and their derivatives ($aarr)

The third line creates two new arrays, $questions and $answers, by jointly shuffling the arrays (retaining respective pairing), and picking 4 elements of the $qarr, and 5 elements of the $aarr.

The last two lines define the titles (column headers) for the $questions and $answers lists.

Question Text

Match each function with it's derivative.


There is no need to specify anything here

The Matching type requires a $questions array and $answers array. The $questions will display on the left with entry boxes next to each. The $answers will display on the right, lettered. If each answer is used at most once, then you do not have to do anything else - the first entry of the $answers array will be assumed to be the answer to the first entry of the $questions array. If there are more entries in $answers than $questions, the left over answers are presumed to never be used. If you want an answer to be used more than once, you will need to define a $matchlist

Load Library Example (Number type)

Example of using loadlibrary to access functions in a macro file (mean from stats library in this case)

Common Control

$a = nonzerodiffrands(1,10,5)

This line defines an array variable $a to be 5 different nonzero integers between 1 and 10. Note that since a single variable was defined, it was created as an array variable

Question Control

$table = showarrays("x",$a)

This defines $table using a standard display macro that creates a tabular display of the array $a with title (header) "x". If you want to display two lists side-by-side, you can do so, for example: showarrays("x",$a,"y",$b)

Question Text

Find `bar x`


Recall that items in backticks are rendered as math. The math command "bar" will place a bar over the item that follows it


$answer = mean($a)

The first line loads the stats macro library. Admins can install new Macro libraries to extend the functionality of IMathAS. The Macro Library Help link will show what libraries are currently installed and the functions they provide.

Here we are using the mean function from the stats library to determine the answer.

Another Example of Matching Type

Common Control

$a,$b,$c = rands(-3,3,3)

This selects three random numbers between -3 and 3

$cols = singleshuffle("red,green,blue")

shuffles the list of colors, placing it in the array $cols

$graphs = array("$a*x^2+$b*x+$c,$cols[0]","2*$a*x+$b,$cols[1]","$a*x^3/3+$b*x^2/2+$c*x,$cols[2]")

We're going to be using the showplot macro. The first argument is a single function or an array of functions. In this case, we're giving an array of functions, though we're only specifying the function and the color. There are other options available.

$plot = showplot($graphs,-3,3,-5,5,off,off)

this actually calls the showplot macro. After the function, the window is specified, then we're setting the labels to off, and grid is set to off

$questions = array("`f(x)`","`f'(x)`","`int f(x)dx`")
$answers = $cols

this defines the questions and answers. Note that they are matched - the first entry in $answers is the answer the first entry in $questions. Notice that the primary randomization in this question is the shuffling of the color array.

Question Control

$questiontitle = "Function"
$answertitle = "Graph Color"

these set titles for the list of questions and answers

Question Text

Match each function with its graph



Nothing is needed here. The answers are automatically associated with the questions based on array order

Example of Multipart Type

Common Control

$anstypes = array("calculated","calculated")
$a,$b = nonzerodiffrands(-8,8,2)
$c = nonzerorand(-30,30)

The first line defines that there will be two parts, both of type calculated. Refer the help for valid anstypes.

The next two lines define our random variables

Question Control

$question = makeprettydisp("{$a}x+{$b}y=$c")

Set up the equation

$hidepreview[1] = true

in some multipart questions, it might be useful to hide the preview button usually provided with calculated and function answer types. You can set $hidepreview to hide the preview button. Note that it is suffixed with a [1]. This specifies to apply the option to the second calculated type. All options should be suffixed like this in a multipart problem unless the option applies to all parts of the problem.

Note that this is a silly example; there is no good reason to hide the preview on one part of this question but not the other

Question Text

Find the x and y intercepts of $question

x-int: `x=`$answerbox[0]<br/>
y-int: `y=`$answerbox[1]


Note the use of the $answerbox above. This places the answerboxes in the problem text. Make sure you put the boxes in numerical order; entry tips are given assuming this.

$answer[0] = $c/$a
$answer[1] = $c/$b

like with other options, the $answer also needs to be suffixed with the question part.

Example of Number Type

Common Control

$a = nonzerorand(-5,5)

Set $a to be a nonzero random number between -5 and 5

$b = rrand(.1,5,.1) if ($a < 0)
$b = rrand(-5,-.1,.1) if ($a > 0)

a decimal number between -5 and 5, with one decimal place. We're going to ensure that $a and $b are different signs using the "if" conditional

$c,$d = nonzerodiffrands(-5,5,2)

two different, nonzero integers

Question Control

$prob = "`$a + $b + $c + $d`"

this could show up as: -4 + -2.3 + 3 + -1 the backquotes tell it to display as math

$prob2 = makeprettydisp("$a + $b + $c + $d")

if we want to simplify it like: -4 - 2.3 + 3 - 1

Question Text

Find: $prob

or equivalently: $prob2


$answer = $a + $b + $c + $d

for number, we just need to specify the answer. No quotes here because we're calculating, not creating a display string

by default, numbers are allowed a .001 relative error.
$reltolerance = .0001 would require a higher accuracy
$abstolerance = .01 would require an absolute error under .01
$answer = "[-10,8)" would accept any answer where `-10 <= givenanswer < 8`

Example of Calculated Type

Common Control

$a,$b = randsfrom("2,3,5,7,11",2)

choose two numbers from a list. Can also choose from an array

$c = rand(1,10) where ($c % $a != 0)
$d = rand(1,10) where ($d % $b != 0)

the "where" statement is used with randomizers. It allows you to avoid a specific case. In this case, we're requiring that $a not divide evenly into $c. The modulus operator, %, gives the remainder upon division

$answerformat = "reducedfraction"

note that the student could enter 2/5*6/7 and get the correct answer. We can prevent this by adding this line. $answerformat = "fraction" is also an option, if you don't care if the answer is reduced.

Question Control

Question Text

Multiply: `$c/$a * $d/$b`

Enter your answer as a single, reduced fraction


$answer = $c/$a * $d/$b

like with the Number type, we supply a number as the answer. The only difference is that the student can enter a calculation instead of a number

Example of Multiple-Choice Type

Common Control

$a,$b = nonzerodiffrands(-5,5,2)

pick two different nonzero numbers. The numbers are important here to ensure that all the choices will be different.

$questions[0] = $a+$b
$questions[1] = $a-$b
$questions[2] = $a*$b

we can either define the entire $questions array at once, or define each piece separately. The former would look like: $questions = array($a+$b,$a-$b,...

Question Control

$displayformat = "horiz"
$text = makeprettydisp("$a+$b")

The first line above will lay out the choices horizontally. To do a standard vertical layout, just omit this line

Question Text

Find $text


$answer = 0

Here the answer is the INDEX into the questions array that holds the correct answer. Arrays are zero-indexed, so the first entry is at index 0.

In multiple-choice questions, the question order is automatically randomized unless you specify otherwise, so it's fine for $answer to always be 0; the location of the correct answer will be shuffled

Example of Multiple Answer Type

Common Control

$questions = listtoarray("`sin(x)`,`sin^-1(x)`,`tan(x)`,`csc(x)`,`x^2`")

the $questions array is a list of the options. The listtoarray macro converts a list of numbers or strings to an array. Use calclisttoarray to convert a list of calculations to an array of numbers

Question Control

Question Text

Select all the functions that are periodic


$answers = "0,2,3"

the answer here is a list of indexes into the $questions array that contain correct answers. Remember that arrays are 0-indexed. Like with multiple-choice, the question order is randomized automatically.

Normally, each part is given equal weight (each checkbox is worth 1/5 point). If you wish to divide the point score only by the number of correct answers, use this line: $scoremethod = "answers"

A Graphing Example (Multipart)

Common Control

$anstypes = listtoarray("number,number,number,number")

Specify the answer types. In this case, four number answers

$graphs[0] = "-x-5,black,-5,-1,,closed"
$graphs[1] = "-2x+3,black,-1,2,open"
$graphs[2] = "-2x+3,black,2,5,open"

Define the graphs. For each graph, it's: function,color,xmin,xmax,startmark,endmark

$graphs[3] = "2,black,2,2,closed"

last one is really just a dot, but we define it as a function

$plot = showplot($graphs,-5,5,-5,5,1,1)

The inputs here are: graphs,xmin,xmax,ymin,ymax,label spacing,grid spacing

Question Control

this question is not randomized; it's just meant for illustration of graphing options.

Question Text

The graph below is the function `f(x)`


Find `lim_(x->-1^+) \ f(x)`  $answerbox[0]

Find `lim_(x->-1^-) \ f(x)`  $answerbox[1]

Find `lim_(x->-1) \ f(x)`  $answerbox[2]

Find `lim_(x->2) \ f(x)` $answerbox[3]

the backslashes above add extra spacing between the limit and the f(x)


$answer[0] = 5
$answer[1] = -4
$answer[2] = "DNE"
$answer[3] = -1

Define the part answers. "DNE" and "oo" (for infinity) are allowed string answers to number questions

© 2006 David Lippman
This guide was written with development grant support from the WA State Distance Learning Council