Answer to Question #277666 in C++ for Shaminie

Question #277666

Create a c++ quiz program,the program is about looping, in this program it has a 10 questions that the user will answer. The program has the following functions:



A. It will show the message (Correct) if the the answer is correct and (Wrong) if the answer is wrong



B. It can compute the total score



C. If the user Failed in the quiz it will ask the user if he wants to retake the quiz. ( 6 is the passing rate)



C1.1If he select Yes he will take the quiz again.



C1.2. If he select No the program will automatically closed.



1
Expert's answer
2021-12-09T07:30:10-0500


#include <iostream>
#include <string>
using namespace std;




struct Quiz{
	string question;
	string answers[4];
	int correctAnswer;
};




int main(){
	int totalScores=0;
	Quiz quiz[10];
	quiz[0].question="The default access specifer for the class members is";
	quiz[0].answers[0]="public";
	quiz[0].answers[1]="private";
	quiz[0].answers[2]="protected";
	quiz[0].answers[3]="None of the above.";
	quiz[0].correctAnswer=2;


	quiz[1].question="A trigraph character begins with";
	quiz[1].answers[0]="#";
	quiz[1].answers[1]="##";
	quiz[1].answers[2]="?";
	quiz[1].answers[3]="??";
	quiz[1].correctAnswer=3;


	quiz[2].question="C++ does not supports the following";
	quiz[2].answers[0]="Multilevel inheritance";
	quiz[2].answers[1]="Hierarchical inheritance";
	quiz[2].answers[2]="Hybrid inheritance";
	quiz[2].answers[3]="None of the above.";
	quiz[2].correctAnswer=4;


	quiz[3].question="One of the following is true for an inline function.";
	quiz[3].answers[0]="It executes faster as it is treated as a macro internally";
	quiz[3].answers[1]="It executes faster because it priority is more than normal function";
	quiz[3].answers[2]="It doesn’t executes faster compared to a normal function";
	quiz[3].answers[3]="None of the above holds true for an inline function";
	quiz[3].correctAnswer=1;


	quiz[4].question="Choose the pure virtual function definition from the following.";
	quiz[4].answers[0]="virtual void f()=0 { }";
	quiz[4].answers[1]="void virtual f()=0 { }";
	quiz[4].answers[2]="virtual void f() {} = 0;";
	quiz[4].answers[3]="None of the above.";
	quiz[4].correctAnswer=4;


	quiz[5].question="Abstract class is __";
	quiz[5].answers[0]="A class must contain all pure virtual functions";
	quiz[5].answers[1]="A class must contain at least one pure virtual function";
	quiz[5].answers[2]="A class may not contain pure virtual function.";
	quiz[5].answers[3]="A class must contain pure virtual function defined outside the class.";
	quiz[5].correctAnswer=2;


	quiz[6].question="Choose the operator which cannot be overloaded.";
	quiz[6].answers[0]="/";
	quiz[6].answers[1]="()";
	quiz[6].answers[2]="::";
	quiz[6].answers[3]="%";
	quiz[6].correctAnswer=3;


	quiz[7].question="Which operator is required to be overloaded as member function only?";
	quiz[7].answers[0]="_";
	quiz[7].answers[1]="_ _";
	quiz[7].answers[2]="++ (postfix version)";
	quiz[7].answers[3]="=";
	quiz[7].correctAnswer=4;


	quiz[8].question="Which of the following is not the keyword in C++?";
	quiz[8].answers[0]="volatile";
	quiz[8].answers[1]="friend";
	quiz[8].answers[2]="extends";
	quiz[8].answers[3]="this";
	quiz[8].correctAnswer=3;


	quiz[9].question="By default the members of the structure are";
	quiz[9].answers[0]="private";
	quiz[9].answers[1]="protected";
	quiz[9].answers[2]="public";
	quiz[9].answers[3]="Access specifiers not applicable for structures.";
	quiz[9].correctAnswer=3;






	string answer="";
	while(answer!="No" && answer!="no"){
		int userAnswer;
		//Each question should print one by one.
		for(int i=0;i<10;i++){
			cout<<"Q "<<(i+1)<<" - "<<quiz[i].question<<"\n";
			for(int j=0;j<4;j++){
				cout<<(j+1)<<" - "<<quiz[i].answers[j]<<"\n";
			}
			//select one option
			cout<<"Your choice: ";
			cin>>userAnswer;
			//A. It will show the message (Correct) if the the answer is correct and (Wrong) if the answer is wrong
			if(userAnswer==quiz[i].correctAnswer){
				cout<<"\nCorrect\n\n";
				totalScores++;
			}else{
				cout<<"\nWrong\n\n";
			}
		}


		//3) At the end of the quiz you should printout Total marks obtained and on the base of those marks tell the candidate if he/she has paased or fail the Test
		cout<<"\nTotal marks: "<<totalScores<<"\n";
		if(totalScores>=6){
			cout<<"The user pased the test\n\n";
			answer="Yes";
		}else{
			cout<<"The user fail the test\n\n";
			cin.ignore();
			cout<<"Do you want to retake the quiz? Yes/No: ";
			getline(cin,answer);
		}
	}




	cin>>totalScores;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog