Question program
import java.lang.*;
import java.io.*;
class Questions {
public String[][] qpa;
public String[][] qca;
Questions() throws IOException {
qpa = new String[10][5];
// Updated questions and objectives
DataInputStream in = new DataInputStream(System.in);
qpa[0][0] = "What is the default value of an int variable in Java?";
qpa[0][1] = "1. 0.0";
qpa[0][2] = "2. null";
qpa[0][3] = "3. 0";
qpa[0][4] = "4. Undefined";
qpa[1][0] = "Which of these is a loop construct that will always execute at least once?";
qpa[1][1] = "1. for";
qpa[1][2] = "2. while";
qpa[1][3] = "3. do-while";
qpa[1][4] = "4. foreach";
qpa[2][0] = "Which of the following is not a Java feature?";
qpa[2][1] = "1. Object-oriented";
qpa[2][2] = "2. Use of pointers";
qpa[2][3] = "3. Platform independent";
qpa[2][4] = "4. Dynamic";
qpa[3][0] = "Which keyword is used to define a subclass in Java?";
qpa[3][1] = "1. super";
qpa[3][2] = "2. this";
qpa[3][3] = "3. extends";
qpa[3][4] = "4. implements";
qca = new String[10][2];
// Updated correct answers
qca[0][0] = "What is the default value of an int variable in Java?";
qca[0][1] = "3. 0";
qca[1][0] = "Which of these is a loop construct that will always execute at least once?";
qca[1][1] = "3. do-while";
qca[2][0] = "Which of the following is not a Java feature?";
qca[2][1] = "2. Use of pointers";
qca[3][0] = "Which keyword is used to define a subclass in Java?";
qca[3][1] = "3. extends";
}
}
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();
// 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