Code for PROGRAM TO EVALUATE A MULTIPLE-CHOICE TEST in Java Programming.
Write a program to check responses of a student to the multiple-choice exam in Java.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.Scanner; public class JavaExamples2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Program to Check Responses of a Student to the Multiple-Choice"); String correct_answers = "abbdd"; int question_count=correct_answers.length(); int correct_count=0; char[] answer_sheet = correct_answers.toCharArray(); char[] test_answers = new char[question_count]; //Reading student responses and counting correct ones System.out.println("Answers can only be (a, b, c, d, null(n))"); int i =0; while(i < question_count) { System.out.printf("Input responses of student answer %s :",i+1); char ch=console.next().charAt(0); if (ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'n') { test_answers[i] =ch; i++; } } for (int j = 0; j < question_count; j++) { if (answer_sheet[j] == test_answers[j]) { correct_count+=1; }else{ System.out.printf("answer %s is wrong \n",j+1); } } System.out.printf("Score is %s out of %s\n",correct_count,question_count); } } |
Output: