This is for C++. Thanks! 10. Driver’s License Exam The local Driver’s License Of
ID: 664775 • Letter: T
Question
This is for C++. Thanks!
10. Driver’s License Exam
The local Driver’s License Office has asked you to write a program that grades the written
portion of the driver’s license exam. The exam has 20 multiple choice questions.
Here are the correct answers:
1. A
2. D
3. B
4. B
5. C
6. B
7. A
8. B
9. C
10. D
11. A
12. C
13. D
14. B
15. D
16. C
17. C
18. A
19. D
20. B
Your program should store the correct answers shown above in an array. It should ask
the user to enter the student’s answers for each of the 20 questions, and the answers
should be stored in another array. After the student’s answers have been entered, the
program should display a message indicating whether the student passed or failed the
exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It
should then display the total number of correctly answered questions, the total number
of incorrectly answered questions, and a list showing the question numbers of the
incorrectly answered questions.
Input Validation: Only accept the letters A, B, C, or D as answers.
Explanation / Answer
import java.io.*;
import java.util.*;
public class DriverExam
{
public static int n=20;
public static void main(String[] args)
{
char answr[]={'A','D','B','B','C','B','A','B','C','D','A','C','D','B','D','C','C','A','D','B'};
char response[] = new char[n];
char ch;
int ncorrect=0,nwrong=0,k=0;
int poswrong[] = new int[n];
for(int i=0;i<n;i++)
poswrong[i] = 0;
Scanner sc = new Scanner(System.in);
System.out.println(" Enter the response(among A,B,C or D) for the following question numbers : ");
for(int i=0;i<n;i++) //Taking only valid input from user
{
System.out.print(" "+(i+1)+". ");
ch = sc.next().charAt(0);
if(ch=='A'||ch=='B'||ch=='C'||ch=='D')
response[i]=ch;
else
{
System.out.println("Please enter a valid response for question number : ");
i--;
}
}
for(int j=0;j<n;j++) //Calculation no of correct,wrong answers
{ if(response[j]==answr[j])
ncorrect++;
else
{
nwrong++;
poswrong[k]=j+1; //Storing question no of incorect answers
k++;
}
}
if(ncorrect>=15)
System.out.println("You have passed the exam!"); //Displaying result
else
System.out.println("You have failed!");
System.out.println("Number of correct answers are : "+ncorrect); //Displaying no of correct answers
System.out.println("Number of incorrect answers are : "+nwrong); //Displaying no of incorrect answers
if(nwrong!=0) //Displaying question numbers of incorrect answers
{ System.out.println("Questions which were incorrectly answered are : ");
int i=0;
while(poswrong[i]!=0)
{
if(i!=0)
System.out.print(",");
System.out.print(poswrong[i]);
i++;
}
}
else
System.out.println("You have answered all questions correctly");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.