In: Mechanical Engineering
write a “quiz” program on a topic, about which you are knowledgeable. this quiz can be work related, general knowledge or any set of questions where there is a specific answer. ignore questions where long descriptions or general answers needed. the program should display the questions, one at a time, and possible answers, and accept an answer from the user. if the answer is correct, the program should go on to the next question. if it is incorrect, store the question for the second attempt and go on to the next question. when the list of questions has ended, the questions that were missed previously, or answered incorrectly, should be displayed again, in their original order. keep a count of the correct answers for each category of questions and display the final count after two attempts. also, display the correct answer, when necessary, in the second round of questioning. limit the quiz to ten (10) questions. include questions from both true/false and multiple-choice categories. four (4) possible answers should be listed in multiple-choice questions.
public class Quiz {
//declare question and option
String[] question = {"Prime Minister of
India?","9*29=","Which city is famous for oranges?","
How may squares are there in a Chess Board?",
"The oceans cover % of the surface of the
earth","Largest river in the world is","Which of the
following cities of India is called the \"Silicon Valley of
India\"?",
"The International Court of Justice has _ judges","
fresh water reserves constitute %"," The percentage of
glucose present in the normal urine is"};
String[] option1 = {"Narendra
Modi","197","Kanpur","36","71","Ganga","Pune","10","4.
5","0.1"};
String[] option2 = {"Yogi
Adityanath","261","Pune","48","60","Bharamputra","Ban
glore","15","1.2","0"};
String[] option3 = {"Rahul
Gandhi","271","Mumbai","64","81","Nile","Mumbai","20"
,"7.2","0.2"};
String[] option4 = {"Sachin
Tendukar","191","Nagpur","72","90","Amazon","Chennai
","25","2.7","0.9"};
int[] answer = {1,2,4,3,1,3,2,2,4,1};
ArrayList<Integer> correctAnswer,wrongAnswer;
public Quiz() {
correctAnswer = new ArrayList<>();
wrongAnswer = new ArrayList<>();
}
// select random question
public int SelectQuestion(){
boolean selected = false;
int num = 0;
while(!selected){
num = ThreadLocalRandom.current().nextInt(0,
10);
if (!correctAnswer.contains(new Integer(num))){
if(!wrongAnswer.contains(new Integer(num)))
selected = true;
}
}
return num;
} //Ask Question with option
public int askQuestion(int num){
System.out.println(question[num]);
System.out.println("1: "+option1[num]);
System.out.println("2: "+option2[num]);
System.out.println("3: "+option3[num]);
System.out.println("4: "+option4[num]);
return num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Quiz quiz = new Quiz();
Integer num;
System.out.println("Select answer or enter your
choice");
for( int i =0;i<quiz.question.length;i++){
num = quiz.SelectQuestion();
quiz.askQuestion(num);
try{
int n = sc.nextInt();
//check answer
if (n==quiz.answer[num]){
System.out.println("Your Answer is correct");
quiz.correctAnswer.add(num);
}
else{
System.out.println("Your Answer is incorrect");
quiz.wrongAnswer.add(num);
}
}
catch(Exception e){
System.out.println("Your Answer is incorrect");
quiz.wrongAnswer.add(num);
}
}
//check if any wrong answer
if (!quiz.wrongAnswer.isEmpty()){
for (Integer i:
(Integer[])quiz.wrongAnswer.toArray()){
quiz.askQuestion(i);
try{
int n = sc.nextInt();
if (n==quiz.answer[i]){
System.out.println("Your Answer is
correct");
quiz.correctAnswer.add(i);
}
else{
System.out.println("Your Answer is
incorrect and Correct answer is" + quiz.answer[i]);
quiz.correctAnswer.add(i);
}
}
catch(Exception e){
System.out.println("Your Answer is
incorrect");
quiz.wrongAnswer.add(i);
}
}
}
System.out.println("Quiz Completed");
sc.close();
}
}