From WeBWorK_wiki
Revision as of 13:50, 26 March 2010 by Glarose (talk | contribs)
Jump to navigation Jump to search

Multiple Choice Problems: PG Code Snippet

This code snippet shows the essential PG code to include a multiple-choice question in a problem.

For an example of a multiple choice problem in which the choices are graphs, see Example 1 of GraphsInTables

Note that in this example we use old-style multiple choice answer objects. The new-style MathObjects have a multiple choice object as well, but its behavior is sufficiently different than that suggested here that is not documented here.

Problem Techniques Index

PG problem file Explanation



Initialization: Include in the list of loaded macro files.

$mc = new_multiple_choice();
"What is your favorite color?", 
$mc->makeLast("none of the above");

Setup: Create a new multiple choice object with new_multiple_choice, and then store the question and correct answer with the qa method. Other answers are specified as a list of arguments to the extra method. To force an answer (either a new extra answer, or the correct answer) to appear last in the list of options, use the makeLast method. All other answers will be scrambled when the multiple choice problem is shown to students.

To make answers appear in a certain order (e.g., Yes followed by No and Maybe), use $mc->qa("question","Yes"); $mc->makeLast("Yes","No","Maybe"); and do not use extra( ) at all. Note that if we have a list of questions (and correct answers) it is easy to randomly pick one to display. For example, if we want to pick between the questions "How many legs do cats have?" and "How many legs to ostriches have?", we could use the following:

@quest = ("How many legs do cats have?",
     "How many legs to ostriches have?"); 
@ans = ("4","2"); 
$pick = random(0,1,1);
$mc->makeLast("2","4","None of the above");

\{ $mc->print_q() \}
\{ $mc->print_a() \}

Main text: In the text section we print the question and answers.

$showPartialCorrectAnswers = 0;

ANS( radio_cmp( $mc->correct_ans() ) );


Answer Evaluation: In most cases we will want to set $showPartialCorrectAnswers to 0 (false) for multiple choice problems. Otherwise, students can use the feedback or the partial credit received to guess and check if their answers are correct. We grade the problem with radio_cmp.

Problem Techniques Index