How to implement multiple choice quiz

walterf25

Expert
Licensed User
Longtime User
Hello everyone, I was wondering if i could get some tips from all the experts in here, i need to develop a quiz type app, which will ask a few questions, and each question will have three option buttons, only one will be the correct answer, how can i implement this? i'm randomly generating the questions, from 1 to 100, but i need to find a way to extract the correct answer ans throw in two more answers but only one will be the correct one, can anyone here maybe help me figure this out please?

thanks,
Walter
 

PharCyDeD

Active Member
Licensed User
Longtime User
I agree with NJ on assigning tags. Just put "Wrong" in all the tags of the radio buttons with the wrong answers and "Right" in the tag for the right answer radio buttons. Then something like:

B4X:
If YourNamedItemHere.tag = "Right" Then

You got it right code goes here!

Else

Wrong code goes here
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

Thanks NJDude and PharcyDed you guys are very helpful, i'm not sure i understand your instructions, do you guys think you guys can post a more elaborate example?
I'm retrieving the questions from a database and the answers as well, how would i do this if i'm retrieving the answer from the database, and there's only one right answer?

thanks,
Walter
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I'd suggest you to have in the database for each entry the question, three answers and the index of the right answer.
Col1 index (primary key), col2 question, col3 answer1, col4 answer2, col5 answer3 col6 index of right answer.
For the answer display you should have 3 radibuttons and with the index you know which one is the right one.

Best regards.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

thanks Klaus, that is exactly what i'm doing right now, i just added Answer1 answer2 and asnswer3 columns to the database, however i'm not sure i understand completely what the col6 for index of right answer is, how can i use that?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It could look like this:
entry 1:
answer 1 right answer : index = 0
entry 2:
answer 2 right answer : index = 1
entry 3:
answer 3 right answer : index = 2
entry 4:
answer 1 right answer : index = 0
entry 5:
answer 2 right answer : index = 1

I use the index values from 0 to 2.

For the RadioButtons you should dim an array of RadioButtons.
B4X:
Dim rbtAnswer(3) As RadioButton
Then
B4X:
rbtAnswer(0).Initialize("rbtAnswer")
rbtAnswer(0).Tag = 0
Activity.AddView(rbtAnswer(0)....

rbtAnswer(1).Initialize("rbtAnswer")
rbtAnswer(1).Tag = 1
Activity.AddView(rbtAnswer(1)....

rbtAnswer(2).Initialize("rbtAnswer")
rbtAnswer(2).Tag = 2
Activity.AddView(rbtAnswer(2)....
Then
B4X:
rbtAnswer(0).Text = answer1
rbtAnswer(1).Text = answer2
rbtAnswer(2).Text = answer3
Then
B4X:
Sub rbtAnswer_CheckedChange(Checked As Boolean)
  If Checked = True Then
    Dim rbt As RadioButton
    rbt = Sender
    If rbt.Tag = Index Then
      ' right answer
    Else
      ' wrong answer
    End If
  End If
End Sub
Best regards.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

Thanks again Klaus, i'm not sure i understand about the right answer index, where you have to set 0, 1 and 2 i have all the other columns in my database, ie. Primary Key, column 1 Questions column 2, Answer1 column 3 Answer2 column 4, answer3 column 5, if i set column 6 to answer index, i don't see how i can set it to 0, 1 or 2, i don't understand how this will help!
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

Ok, just let me make sure i understand so i would have a database like this

___________________________________________________________________
Primary Key Question Ans1 Ans2 Ans3 Index
___________________________________________________________________
1 ????? Opt1 Opt2 Opt3 0
2 ????? Opt1 Opt2 Opt3 1
3 ????? Opt1 Opt2 Opt3 2
___________________________________________________________________

Is that correct, or am i missing something, i'm sorry but i feel a little confused, please let me know if this looks correct to you.

Thanks Klaus
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Check the attachment below for a quiz example I made. It may not be the BEST method, but it works.
 

Attachments

  • quiz.zip
    7.4 KB · Views: 938
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

Thanks a lot PharcyDeD, it is a very good example, however i decided to do it like Klaus suggested, the only problem i see with that is that if i keep doing it that way, the correct answer will always be assigned to the first Radiobutton, and i want to be able to assign the correct answer randomly so that the correct answer won't always be assigned to the first radio button, is there a way to do this, i'm sure there is but my brain is running low on fuel at this moment, i'm attaching my source code so that you guys can take a look at it and maybe suggest a better way to do this!

thank again guys, you guys Rock
:sign0098:
View attachment 10635
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

Thanks PharCyDeD, that's exactly what i'm trying to do at this moment, i think i got it working though, again thanks a lot for all your help buddy. I will post the results when i get it to work completely.

:sign0188:
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
No problem. I have received tons of help (and I am sure I will need plenty more) from this forum so I am trying to give back. One thing you could do is once the buttons are assigned you could use a simple map/listview randomizing system like I used in my example. Then you could just do:

B4X:
If rbButton1Spot = 1 Then

rbButton.Top = 10
rbButton.Left = 20

End If

If rbButton2Spot = 1 Then

rbButton.Top = 10
rbButton.Left = 20

End If

If rbButton3Spot = 1 Then

rbButton.Top = 10
rbButton.Left = 20

End If

...
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find your project a little bit modified.
In the database you entered systematically 0,1,2,0,1,2 etc in the ansind column.
The index in this column should be the index of the correct answer !
If Answer1 is the correct one then ansind = 0
If Answer2 is the correct one then ansind = 1
If Answer3 is the correct one then ansind = 2

in the rbtAnswer_CheckedChange routine we can check if the RadioButton that raised the event contains the correct answer.

Best regards.
 

Attachments

  • Multiple Choice Quiz1.zip
    241.2 KB · Views: 792
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Multiple choice questons

Hi Klaus, thanks for your help, i wish i had checked back sooner, i actually came up with a different way of accomplishing this, but i can understand the change you made, however i would like to share how i accomplished randomizing the answers so that they no same question will be assigned to the same radio button, hope it helps others, thanks for your help Klaus.

B4X:
Sub btnNext_Click
Dim Result As Int 


Counter = Counter + 1
Qnumber = Rnd(1, 95)
lblQuestion.Text = QuestionList.Get(Qnumber)      'display a random question every time.
 If Qnumber >= 95 Then
 Qnumber = 0
 End If
 
' If Counter = 10 Then      'if ten questions have passed then display message box with correct and wrong answers.
' Result = Msgbox2("You have answered 8 correct out of 10", "Score", "Continue", "Finish", "", Null)
' Counter = 0
'End If
rbtAnswer(0).Checked = False    'uncheck all option buttons
rbtAnswer(1).Checked = False 
rbtAnswer(2).Checked = False 

opt1 = Rnd(0, 3)
opt2 = Rnd(0, 3)
opt3 = Rnd(0, 3)

If opt1 = opt2 OR opt1 = opt3 OR opt2 = opt3 Then   'make sure opt1 or opt2 or opt3 are not equals to each other
Do While opt1 = opt2 OR opt2 = opt3 OR opt3 = opt1   'if any of them are then randomize again, until none of them are equal
opt1 = Rnd(0, 3)
opt2 = Rnd(0, 3)
opt3 = Rnd(0, 3)
Loop
rbtAnswer(opt1).Text = AnswerList1.Get(Qnumber)   'assign that specific number 0 to 2 once they are unique
rbtAnswer(opt2).Text = AnswerList2.Get(Qnumber)   'that way each answer will be assigned to any option button every time
rbtAnswer(opt3).Text = AnswerList3.Get(Qnumber)   'no same answer will be on the same position every time.
Log(opt1 & " " & opt2 & " " & opt3)
Else
rbtAnswer(opt1).Text = AnswerList1.Get(Qnumber)
rbtAnswer(opt2).Text = AnswerList2.Get(Qnumber)
rbtAnswer(opt3).Text = AnswerList3.Get(Qnumber)
Log(opt1 & " " & opt2 & " " & opt3)
End If


correctAnswer = AnswerList1.Get(Qnumber)   'assign the correct answer to variable correctAnswer.

End Sub
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
These examples are great. I've taken some bits and pieces and have also trying to work on some further randomization.

I'm wondering - is there a way to modify the AnswerList names with a random value.

For example instead of

AnswerList1.Get(Qnumber)

it would be

AnswerList(randomvalue).Get(Qnumber)

?
 
Upvote 0
Top