Question Program
import java.lang.*;
import java.io.*;
class Questions {
public String[][] qpa; // Questions and possible answers
public String[][] qca; // Correct answers
Questions() throws IOException {
qpa = new String[10][5]; // Initialize the question and answers array
/* Questions and Objectives */
qpa[0][0] = "What is the size of an int in Java?";
qpa[0][1] = "1. 2 bytes";
qpa[0][2] = "2. 4 bytes";
qpa[0][3] = "3. 8 bytes";
qpa[0][4] = "4. Depends on the system";
qpa[1][0] = "Which of these is a valid declaration of a char?";
qpa[1][1] = "1. char ch = 'ab';";
qpa[1][2] = "2. char ch = '\\u0022';";
qpa[1][3] = "3. char ch = 23;";
qpa[1][4] = "4. char ch = '\\n';";
qpa[2][0] = "What is the output of the following code? System.out.println(10 + 20 + \"30\");";
qpa[2][1] = "1. 3030";
qpa[2][2] = "2. 102030";
qpa[2][3] = "3. 1030";
qpa[2][4] = "4. 3030";
qpa[3][0] = "Which of the following is not a Java keyword?";
qpa[3][1] = "1. static";
qpa[3][2] = "2. Boolean";
qpa[3][3] = "3. void";
qpa[3][4] = "4. private";
qca = new String[10][2];
/* Questions and Correct Answers */
qca[0][0] = "What is the size of an int in Java?";
qca[0][1] = "2. 4 bytes";
qca[1][0] = "Which of these is a valid declaration of a char?";
qca[1][1] = "2. char ch = '\\u0022';";
qca[2][0] = "What is the output of the following code? System.out.println(10 + 20 + \"30\");";
qca[2][1] = "1. 3030";
qca[3][0] = "Which of the following is not a Java keyword?";
qca[3][1] = "2. Boolean";
}
}
public class qu {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(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 answer from the user */
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
System.out.println(q.qpa[i][j]);
}
System.out.println("Your answer:");
x = Integer.parseInt(in.readLine());
ans[i] = q.qpa[i][x];
}
/* Calculate correct answers */
for (i = 0; i < 4; 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 < 4; 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);
}
}
Comments
Post a Comment