-
Notifications
You must be signed in to change notification settings - Fork 1
/
java_quiz.html
207 lines (195 loc) · 9.43 KB
/
java_quiz.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Java Language Quiz</title>
<link rel="stylesheet" href="/css/quiz_style.css">
<link rel="icon" type="image/png" href="logo/fav.jpeg">
</head>
<body>
<div class="container">
<h1>Java Language Quiz</h1>
<div id="quiz"></div>
<button id="nextButton">Next Question</button>
<div id="result" class="result"></div>
</div>
<button><a href="/">Home</a></button>
<style>
a {
color: azure;
text-decoration: none;
}
</style>
<script>
const questions = [
{
question: "1. What is the correct syntax to declare a variable in Java?",
options: ["variable x;", "x = 5;", "int x;", "x = int;"],
answer: "int x;"
},
{
question: "2. What is the output of the following code?\n\npublic class Main {\n public static void main(String[] args) {\n int x = 5;\n System.out.println(x++);\n }\n}",
options: ["5", "6", "Error", "Undefined"],
answer: "5"
},
{
question: "3. How do you declare a method in Java?",
options: ["method myMethod()", "myMethod() { }", "void myMethod()", "public void myMethod()"],
answer: "public void myMethod()"
},
{
question: "4. What is the correct way to define a class in Java?",
options: ["MyClass class {}", "class MyClass()", "class MyClass {}", "class = MyClass {}"],
answer: "class MyClass {}"
},
{
question: "5. What is the output of the following code?\n\npublic class Main {\n public static void main(String[] args) {\n System.out.println(\"Hello, World!\");\n }\n}",
options: ["Hello, World!", "Error", "Undefined", "Null"],
answer: "Hello, World!"
},
{
question: "6. How do you create a comment in Java?",
options: ["//This is a comment", "# This is a comment", "/* This is a comment */", "' This is a comment"],
answer: "//This is a comment"
},
{
question: "7. What is the output of the following code?\n\nint x = 10;\nint y = 5;\nSystem.out.println(x + y);",
options: ["15", "105", "Error", "Undefined"],
answer: "15"
},
{
question: "8. Which data type is used to store a single character in Java?",
options: ["char", "string", "character", "chr"],
answer: "char"
},
{
question: "9. What is the output of the following code?\n\nString x = \"Hello\";\nSystem.out.println(x.length());",
options: ["5", "6", "Error", "Undefined"],
answer: "5"
},
{
question: "10. What is the output of the following code?\n\nint x = 10;\nint y = 5;\nSystem.out.println(x * y);",
options: ["15", "50", "Error", "Undefined"],
answer: "50"
},
{
question: "11. What is the output of the following code?\n\nint x = 10;\nint y = 5;\nSystem.out.println(x / y);",
options: ["2", "0.5", "Error", "Undefined"],
answer: "2"
},
{
question: "12. How do you declare an array in Java?",
options: ["int[] myArray;", "myArray[] = new int[];", "int myArray();", "array myArray;"],
answer: "int[] myArray;"
},
{
question: "13. What is the output of the following code?\n\nint[] myArray = {1, 2, 3};\nSystem.out.println(myArray[0]);",
options: ["1", "2", "3", "Error"],
answer: "1"
},
{
question: "14. What is the output of the following code?\n\nint[] myArray = {1, 2, 3};\nSystem.out.println(myArray.length);",
options: ["1", "2", "3", "Error"],
answer: "3"
},
{
question: "15. What is the output of the following code?\n\nint[] myArray = {1, 2, 3};\nSystem.out.println(myArray[3]);",
options: ["3", "Error", "Undefined", "Null"],
answer: "Error"
},
{
question: "16. What is the output of the following code?\n\nint[] myArray = new int[3];\nSystem.out.println(myArray[0]);",
options: ["0", "1", "Error", "Undefined"],
answer: "0"
},
{
question: "17. What is the output of the following code?\n\nint[] myArray = new int[3];\nSystem.out.println(myArray.length);",
options: ["1", "2", "3", "Error"],
answer: "3"
},
{
question: "18. What is the output of the following code?\n\nint[] myArray = new int[3];\nSystem.out.println(myArray[3]);",
options: ["3", "Error", "Undefined", "Null"],
answer: "Error"
},
{
question: "19. What is the output of the following code?\n\nint x = 10;\nif (x > 5) {\n System.out.println(\"x is greater than 5\");\n}",
options: ["x is greater than 5", "Error", "Undefined", "Null"],
answer: "x is greater than 5"
},
{
question: "20. What is the output of the following code?\n\nint x = 10;\nif (x < 5) {\n System.out.println(\"x is less than 5\");\n} else {\n System.out.println(\"x is greater than or equal to 5\");\n}",
options: ["x is less than 5", "x is greater than or equal to 5", "Error", "Undefined"],
answer: "x is greater than or equal to 5"
},
{
question: "21. What is the output of the following code?\n\nint x = 10;\nif (x == 10) {\n System.out.println(\"x is equal to 10\");\n} else {\n System.out.println(\"x is not equal to 10\");\n}",
options: ["x is equal to 10", "x is not equal to 10", "Error", "Undefined"],
answer: "x is equal to 10"
},
{
question: "22. What is the output of the following code?\n\nint x = 10;\nif (x == 5) {\n System.out.println(\"x is equal to 5\");\n} else if (x == 10) {\n System.out.println(\"x is equal to 10\");\n} else {\n System.out.println(\"x is not equal to 5 or 10\");\n}",
options: ["x is equal to 5", "x is equal to 10", "x is not equal to 5 or 10", "Error"],
answer: "x is equal to 10"
},
{
question: "23. What is the output of the following code?\n\nfor (int i = 0; i < 5; i++) {\n System.out.println(i);\n}",
options: ["0 1 2 3 4", "1 2 3 4 5", "0 1 2 3", "1 2 3 4"],
answer: "0 1 2 3 4"
},
{
question: "24. What is the output of the following code?\n\nx = (1, 2, 3)\nx.pop()\nprint(x)",
options: ["(1, 2, 3)", "(1, 2)", "(2, 3)", "Error"],
answer: "Error"
},
{
question: "25. What is the output of the following code?\n\nx = {'a': 1, 'b': 2, 'c': 3}\nprint(x['b'])",
options: ["1", "2", "3", "Error"],
answer: "2"
}
];
let currentQuestionIndex = 0;
let score = 0;
function showQuestion(questionIndex) {
const currentQuestion = questions[questionIndex];
const quizElement = document.getElementById("quiz");
quizElement.innerHTML = `
<div class="question">
<p>${currentQuestion.question}</p>
<div class="options">
${currentQuestion.options.map((option, index) => `
<label><input type="radio" name="answer" value="${option}"> ${option}</label>
`).join('')}
</div>
</div>
`;
}
function showResult() {
const resultElement = document.getElementById("result");
resultElement.innerHTML = `Your score is: ${score}`;
}
function checkAnswer() {
const selectedAnswer = document.querySelector('input[name="answer"]:checked');
if (selectedAnswer) {
const userAnswer = selectedAnswer.value;
const correctAnswer = questions[currentQuestionIndex].answer;
if (userAnswer === correctAnswer) {
score++;
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion(currentQuestionIndex);
} else {
showResult();
document.getElementById("nextButton").disabled = true;
}
} else {
alert("Please select an answer.");
}
}
document.getElementById("nextButton").addEventListener("click", checkAnswer);
showQuestion(currentQuestionIndex);
</script>
</body>
</html>