Java Quiz program

 import java.util.Scanner;

class Questions {

    public String[][] qpa; 

    public String[][] qca; 

    Questions() {

        qpa = new String[10][5];

        // questions and objectives

        qpa[0][0] = "Which keyword is used to define a class in Java?";

        qpa[0][1] = "1. define";

        qpa[0][2] = "2. class";

        qpa[0][3] = "3. Class";

        qpa[0][4] = "4. def";

        qpa[1][0] = "Which method is the entry point of a Java program?";

        qpa[1][1] = "1. start()";

        qpa[1][2] = "2. main()";

        qpa[1][3] = "3. init()";

        qpa[1][4] = "4. run()";

        qpa[2][0] = "Which package contains the Random class?";

        qpa[2][1] = "1. java.util";

        qpa[2][2] = "2. java.io";

        qpa[2][3] = "3. java.net";

        qpa[2][4] = "4. java.lang";

        qpa[3][0] = "Which keyword is used to create an object in Java?";

        qpa[3][1] = "1. new";

        qpa[3][2] = "2. create";

        qpa[3][3] = "3. object";

        qpa[3][4] = "4. instantiate";

        qpa[4][0] = "What is the size of int in Java?";

        qpa[4][1] = "1. 8 bits";

        qpa[4][2] = "2. 16 bits";

        qpa[4][3] = "3. 32 bits";

        qpa[4][4] = "4. 64 bits";

        qca = new String[10][2];

        // questions and correct answers

        qca[0][0] = "Which keyword is used to define a class in Java?";

        qca[0][1] = "2. class";

        qca[1][0] = "Which method is the entry point of a Java program?";

        qca[1][1] = "2. main()";

        qca[2][0] = "Which package contains the Random class?";

        qca[2][1] = "1. java.util";

        qca[3][0] = "Which keyword is used to create an object in Java?";

        qca[3][1] = "1. new";

        qca[4][0] = "What is the size of int in Java?";

        qca[4][1] = "3. 32 bits";

    }

}

public class qu {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int x, correct = 0, wrong = 0, i, j; 

        String[] ans = new String[10];

        Questions q = new Questions();

        System.out.println("JAVA QUIZ");

        System.out.println();

        // for loop to display questions and read the answers from the user

        for (i = 0; i < 5; i++) {

            for (j = 0; j < 5; j++) {

                System.out.println(q.qpa[i][j]);

            }

            System.out.println("Your answer:");

            x = scanner.nextInt();

            ans[i] = q.qpa[i][x];

        }

        // calculate correct answers

        for (i = 0; i < 5; i++) {

            if (q.qca[i][1].equals(ans[i])) {

                correct++;

            } else {

                wrong++;

            }

        }

        // printing the correct answers and user-selected answers

        System.out.println("CORRECT ANSWERS");

        for (i = 0; i < 5; i++) {

            System.out.println();

            System.out.println(q.qpa[i][0]);

            System.out.println("Correct answer: " + q.qca[i][1]);

            System.out.println("Your answer: " + ans[i]);

        }

        System.out.println("Correct = " + correct + "\tWrong = " + wrong);

        scanner.close();

    }

}




OUTPUT:-


Which keyword is used to define a class in Java?

1. define

2. class

3. Class

4. def

Your answer:

2


Which method is the entry point of a Java program?

1. start()

2. main()

3. init()

4. run()

Your answer:

2


Which package contains the Random class?

1. java.util

2. java.io

3. java.net

4. java.lang

Your answer:

1


Which keyword is used to create an object in Java?

1. new

2. create

3. object

4. instantiate

Your answer:

1


What is the size of int in Java?

1. 8 bits

2. 16 bits

3. 32 bits

4. 64 bits

Your answer:

3


CORRECT ANSWERS


Which keyword is used to define a class in Java?

Correct answer: 2. class

Your answer: 2. class


Which method is the entry point of a Java program?

Correct answer: 2. main()

Your answer: 2. main()


Which package contains the Random class?

Correct answer: 1. java.util

Your answer: 1. java.util


Which keyword is used to create an object in Java?

Correct answer: 1. new

Your answer: 1. new


What is the size of int in Java?

Correct answer: 3. 32 bits

Your answer: 3. 32 bits


Correct = 5 Wrong = 0









Comments