Skip to content

Instantly share code, notes, and snippets.

@yaswanthrajyadiki
Created August 19, 2015 04:15
Show Gist options
  • Save yaswanthrajyadiki/fe2de8aa1c323d7194c0 to your computer and use it in GitHub Desktop.
Save yaswanthrajyadiki/fe2de8aa1c323d7194c0 to your computer and use it in GitHub Desktop.
This can create a quiz with questions and can add questions in add method which is class
import java.util.Scanner;
class Question {
private int serialNumber;
private String questionText;
private String choices;
private int correctAnswer;
private static int points;
Question(int serialNumber, String questionText, String choices, int correctAnswer, int points) {
this.serialNumber = serialNumber;
this.questionText = questionText;
this.choices = choices;
this.correctAnswer = correctAnswer;
this.points = points;
}
public int getSerialNumber() {
return this.serialNumber;
}
public String getQuestionText() {
return this.questionText;
}
public int getCorrectAnswer() {
return this.correctAnswer;
}
public String getChoices() {
return this.choices;
}
public int getPoints() {
return this.points;
}
}
public class Quiz {
private int point;
Quiz() {
point = 0;
}
final void addQuestions() {
Question[] q1 = new Question[10];
q1[0] = new Question(1,"What is Java?"+"\n","1.POP\n2.OOP\n3.Interface\n4.Compiler",2,1);
q1[1] = new Question(2,"What is object?"+"\n","1.Object is an veriable.\n"+"2.Object is an instance of class.\n"+"3.Object is an instance of method.\n"+"4.Object is an reference.",2,1);
q1[2] = new Question(3,"What is c?\n","1.POP\n2.OOP\n3.Interface\n4.Compiler",1,1);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of questions");
int number = sc.nextInt();
for (int i = 0; i < number; i++) {
System.out.print(q1[i].getSerialNumber()+". ");
System.out.print(q1[i].getQuestionText());
System.out.println(q1[i].getChoices());
int answer = sc.nextInt();
if(answer == q1[i].getCorrectAnswer()) {
point = point + q1[i].getPoints();
}
}
}
final void startQuiz() {
Quiz q = new Quiz();
q.addQuestions();
q.showReport();
}
final void showReport(){
System.out.println("Points Obtained: "+point);
}
public static void main(String[] args) {
Quiz q2 = new Quiz();
q2.startQuiz();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment