Q:

Java program to find the correct output of student quiz

belongs to collection: Java Basic Programs

0

Given quiz answers with the correct answer and we have to find score along with the correct answers using java program.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

import java.util.*;
class QuizCheckResult
{
	// creating array object.
    char A[][],K[];
    int S[],n;
     
    void input()
    {
    	// create object of scanner class.
        Scanner sc = new Scanner(System.in);
        
        // enter number of participants.
        System.out.print("Enter number of participants : ");
        n = sc.nextInt();
        
        // condition of least and heighest input
        if(n<4 || n>10)
        {
        	// input the range here.
            System.out.print("Input size out of range : ");
            System.exit(0);
        }
        
        A = new char[n][5]; 
        K = new char[5]; 
        S = new int[n]; 
        
        // enter the correct answer to check the answers of students.
        System.out.println("\n* Enter answer of each participant row-wise in a single line *\n");
        for(int i = 0; i<n; i++)
        {
            System.out.print("Participant "+(i+1)+" : ");
            for(int j=0; j<5; j++)
            {
                A[i][j] = sc.next().charAt(0);
            }
        }
        System.out.print("\nEnter Answer Key : ");
        for(int i = 0; i<5; i++)
        {
            K[i] = sc.next().charAt(0);
        }
    }
 
    // Function to calculate score of participant
    void Score() 
    {
 
        for(int i = 0; i<n; i++)
        {
            S[i] = 0;
            for(int j=0; j<5; j++)
            {
            	// Checking if Answer of the participants match with the key or not
                if(A[i][j] == K[j]) 
                {
                    S[i]++;
                }
            }
        }
    }
 
    // function to print score.
    void printScore()
    {
        int max = 0;
        System.out.println("\nSCORES : ");
        for(int i = 0; i<n; i++)
        {
            System.out.println("\tParticipant "+(i+1)+" = "+S[i]);
            if(S[i]>max)
            {
            	// Storing the Highest Score
                max = S[i]; 
            }
        }
        System.out.println();
         
        System.out.println("\tHighest Score : "+max);
         
        // Printing all those participant number who got highest score
        System.out.println("\tHighest Scorers : ");
        for(int i = 0; i<n; i++) 
        {
            if(S[i] == max)
            {
                System.out.println("\t\t\tParticipant "+(i+1));
            }
        }
    }
 
    public static void main(String args[])
    {
    	QuizCheckResult ob = new QuizCheckResult();
        ob.input();
        ob.Score();
        ob.printScore();
    }
}

Output

First run:
Enter number of participants : 4

* Enter answer of each participant row-wise in a single line *

Participant 1 : A B D C A
Participant 2 : B B C D A
Participant 3 : A A D C B
Participant 4 : D D C A B

Enter Answer Key : A C B D A

SCORES : 
	Participant 1 = 2
	Participant 2 = 2
	Participant 3 = 1
	Participant 4 = 0

	Highest Score : 2
	Highest Scorers : 
			Participant 1
			Participant 2


Second run:
Enter number of participants : 3
Input size out of range : 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check Emrip number... >>
<< Java program to find sum of factorials from 1 to N...