Driver’s License Exam The local Driver’s License Office has asked you to write a
ID: 3688306 • Letter: D
Question
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.A 9.C 10.D 11.A 12.C 13.D 14.A 15.A 16.C 17.B 18.A 19.D 20.C 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.
Explanation / Answer
import java.util.Scanner;
public class DriverLic {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
final int SIZE = 20;
char [] correctAnswers = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A',};
char singleAnswer = ' ';
Scanner scan = new Scanner(System.in);
char [] incorrectAnswers = new char [SIZE];
int wrongAnswersCounter = 0;
boolean wrongAnswer;
for (int i = 0; i < SIZE; i++)
{
do
{
wrongAnswer = false;
System.out.print("Please enter your answer to question #" + (i+1) + ": ");
singleAnswer = scan.nextLine().toUpperCase().charAt(0);
if (singleAnswer != 'A' && singleAnswer != 'B' && singleAnswer !='C' && singleAnswer !='D')
{
System.out.print("Invalid Input Please re-enter your answer. ");
wrongAnswer = true;
}
}while (wrongAnswer); // loop while wrongAnswer is true...
if (correctAnswers[i] != singleAnswer)
{
incorrectAnswers[i] = singleAnswer;
wrongAnswersCounter++;
}
} // end of for loop...
scan.close();
// if (wrongAnswersCounter >= 5)
// System.out.println(); Tinary if statement below:
System.out.println(wrongAnswersCounter >= 5 ? "Sorry, you failed." : "Correct! Congrats! you passed.");
System.out.println("You have answered " + (SIZE - wrongAnswersCounter) + " Correctly and " +
wrongAnswersCounter + " incorrectly. " );
for (int i = 0; i < SIZE; i++)
{
if (incorrectAnswers[i] != 'u0000') // null character
{
System.out.println("You answered question #" + (i+1) + " incorrectly. Your anwer was " +
incorrectAnswers[i] + ". Correct answer is " + correctAnswers[i] + ".");
}
}
}
}
sample output
Please enter your answer to question #1: a
Please enter your answer to question #2: a
Please enter your answer to question #3: b
Please enter your answer to question #4: d
Please enter your answer to question #5: b
Please enter your answer to question #6: c
Please enter your answer to question #7: a
Please enter your answer to question #8: f
Invalid Input Please re-enter your answer.
Please enter your answer to question #8: s
Invalid Input Please re-enter your answer.
Please enter your answer to question #8: a
Please enter your answer to question #9: b
Please enter your answer to question #10: w
Invalid Input Please re-enter your answer.
Please enter your answer to question #10: a
Please enter your answer to question #11: c
Please enter your answer to question #12: a
Please enter your answer to question #13: c
Please enter your answer to question #14: a
Please enter your answer to question #15: b
Please enter your answer to question #16: b
Please enter your answer to question #17: a
Please enter your answer to question #18: v
Invalid Input Please re-enter your answer.
Please enter your answer to question #18: av
Please enter your answer to question #19: av
Please enter your answer to question #20: a
Sorry, you failed.
You have answered 3 Correctly and 17 incorrectly.
You answered question #1 incorrectly. Your anwer was A. Correct answer is B.
You answered question #2 incorrectly. Your anwer was A. Correct answer is D.
You answered question #3 incorrectly. Your anwer was B. Correct answer is A.
You answered question #4 incorrectly. Your anwer was D. Correct answer is A.
You answered question #5 incorrectly. Your anwer was B. Correct answer is C.
You answered question #6 incorrectly. Your anwer was C. Correct answer is A.
You answered question #7 incorrectly. Your anwer was A. Correct answer is B.
You answered question #9 incorrectly. Your anwer was B. Correct answer is C.
You answered question #10 incorrectly. Your anwer was A. Correct answer is D.
You answered question #11 incorrectly. Your anwer was C. Correct answer is B.
You answered question #12 incorrectly. Your anwer was A. Correct answer is C.
You answered question #13 incorrectly. Your anwer was C. Correct answer is D.
You answered question #15 incorrectly. Your anwer was B. Correct answer is D.
You answered question #16 incorrectly. Your anwer was B. Correct answer is C.
You answered question #17 incorrectly. Your anwer was A. Correct answer is C.
You answered question #18 incorrectly. Your anwer was A. Correct answer is B
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.