-
Notifications
You must be signed in to change notification settings - Fork 0
/
JAVAQuiz.html
566 lines (526 loc) · 21.2 KB
/
JAVAQuiz.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ad sense tag-->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1733919132650092"
crossorigin="anonymous"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FBJL1SB5QM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-FBJL1SB5QM');
</script>
<title>Java QUIZ - Test Your Coding Knowledge | CodingNation</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="title" content="Coding-nation">
<meta name="description"
content="CodingNation is a comprehensive code tutorial website, providing beginner to advanced coding tutorials, coding exercises, and coding challenges to help you learn to code and build your skills.">
<meta name="keywords"
content="Coding-nation, CodeNation, HTML tutorials, python tutorials, css tutorials, c tutorials,React js tutorials, javascript tutorials,java tutorials, php tutorials, code editor, online code editor, Codebot,chatbot,quizzes">
<meta name="robots" content="index, follow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="English">
<meta name="author" content="coding-nation.com">
<link rel="stylesheet" href="../Styles/Quiz.css" />
<link rel="icon" type="image/png" sizes="32x32" href="../Images/favicon.png">
<!-- Latest compiled and minified CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body onload="NextQuestion(0)">
<div class="chatbot-icon">
<a href="../codebot.html"><i class='bx bx-bot'></i></i>
</a>
</div>
<!--navigation bar-->
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="../index.html">CodingNation</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto" id="navbarNavLinks">
<li class="nav-item">
<a class="nav-link" href="../index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">Courses</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../Python.html">Python</a>
</li>
<li>
<a class="dropdown-item" href="../React.html">React JS</a>
</li>
<li>
<a class="dropdown-item" href="../html.html">HTML</a>
</li>
<li><a class="dropdown-item" href="../css.html">CSS</a></li>
<li>
<a class="dropdown-item" href="../jascri.html">JavaScript</a>
</li>
<li><a class="dropdown-item" href="../cpro.html">C</a></li>
<li>
<a class="dropdown-item" href="../java.html">Java</a>
</li>
<li><a class="dropdown-item" href="../php.html">PHP</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">Practice</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLQuiz.html" target="_blank">HTML Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CSSQuiz.html" target="_blank">CSS Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JSQuiz.html" target="_blank">JavaScript Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CQuiz.html" target="_blank">C Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAQuiz.html" target="_blank">Java Quiz</a>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button"
data-bs-toggle="dropdown">Exercises</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLExer.html" target="_blank">HTML Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CSSExer.html" target="_blank">CSS Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JsExer.html" target="_blank">JavaScript
Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CExer.html" target="_blank">C Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAExer.html" target="_blank">Java Exercise</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="../codebot.html" target="_blank">CodeBot</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../Editor.html" target="_blank">Editor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../About.html">About</a>
</li>
</ul>
</div>
</div>
</nav>
<main>
<!-- creating a modal for when quiz ends -->
<div class="modal-container" id="score-modal">
<div class="modal-content-container">
<h1>Congratulations, Quiz Completed.</h1>
<div class="grade-details">
<p style="font-size: 16px">Result :</p>
<p>Wrong Answers : <span id="wrong-answers"></span></p>
<p>Right Answers : <span id="right-answers"></span></p>
<p>Grade : <span id="grade-percentage"></span>%</p>
<p><span id="remarks"></span></p>
</div>
<div class="modal-button-container">
<button onclick="closeScoreModal()">Continue</button>
</div>
</div>
</div>
<!-- end of modal of quiz details-->
<div class="game-quiz-container">
<div class="game-details-container">
<h1>Score : <span id="player-score"></span> / 20</h1>
<h1>Question : <span id="question-number"></span> / 20</h1>
</div>
<div class="game-question-container">
<h1 id="display-question"></h1>
</div>
<div class="game-options-container">
<div class="modal-container" id="option-modal">
<div class="modal-content-container">
<h1>Please Pick An Option</h1>
<div class="modal-button-container">
<button onclick="closeOptionModal()">Continue</button>
</div>
</div>
</div>
<span>
<input type="radio" id="option-one" name="option" class="radio" value="optionA" />
<label for="option-one" class="option" id="option-one-label"></label>
</span>
<span>
<input type="radio" id="option-two" name="option" class="radio" value="optionB" />
<label for="option-two" class="option" id="option-two-label"></label>
</span>
<span>
<input type="radio" id="option-three" name="option" class="radio" value="optionC" />
<label for="option-three" class="option" id="option-three-label"></label>
</span>
<span>
<input type="radio" id="option-four" name="option" class="radio" value="optionD" />
<label for="option-four" class="option" id="option-four-label"></label>
</span>
</div>
<div class="next-button-container">
<button onclick="handleNextQuestion()">Next Question</button>
</div>
</div>
</main>
<script>
//this would be the object shape for storing the questions
//you can change the questions to your own taste or even add more questions..
const questions = [
{
question: 'What is a correct syntax to output "Hello World" in Java?',
optionA: 'console.writeline("Hello world")',
optionB: 'echo("Hello world")',
optionC: 'print("Hello world")',
optionD: 'System.out.println("Hello world")',
correctOption: "optionD",
},
{
question: "What is the keyword used to declare a class in Java?",
optionA: "class",
optionB: "public",
optionC: "static",
optionD: "void",
correctOption: "optionA",
},
{
question: "What is the keyword used to create an object in Java?",
optionA: "new",
optionB: "this",
optionC: "final",
optionD: "private",
correctOption: "optionA",
},
{
question:
"What is the access modifier used to declare a variable or method that can be accessed within the same package only?",
optionA: "public",
optionB: "private",
optionC: "protected",
optionD: "default",
correctOption: "optionD",
},
{
question: "What is the keyword used to define a constant in Java?",
optionA: "var",
optionB: "final",
optionC: "static",
optionD: "const",
correctOption: "optionB",
},
{
question:
"What is the name of the method used to compare two strings in Java?",
optionA: "compare",
optionB: "equals",
optionC: "compareTo",
optionD: "concat",
correctOption: "optionB",
},
{
question:
"What is the keyword used to declare an abstract class in Java?",
optionA: "abstract",
optionB: "class",
optionC: "interface",
optionD: "extends",
correctOption: "optionA",
},
{
question:
"What is the keyword used to implement an interface in Java?",
optionA: "interface",
optionB: "implements",
optionC: "extends",
optionD: "abstract",
correctOption: "optionB",
},
{
question:
"What is the keyword used to define a method that can be accessed without creating an object of the class?",
optionA: "static",
optionB: "public",
optionC: "private",
optionD: "final",
correctOption: "optionA",
},
{
question: "What is the keyword used to break out of a loop in Java?",
optionA: "continue",
optionB: "break",
optionC: "return",
optionD: "exit",
correctOption: "optionB",
},
{
question: "What is the keyword used to handle exceptions in Java?",
optionA: "try",
optionB: "catch",
optionC: "finally",
optionD: "throw",
correctOption: "optionA",
},
{
question: "What is the keyword used to create a subclass in Java?",
optionA: "extends",
optionB: "implements",
optionC: "subclass",
optionD: "superclass",
correctOption: "optionA",
},
{
question:
"What is the keyword used to call a superclass constructor in a subclass in Java?",
optionA: "super",
optionB: "this",
optionC: "base",
optionD: "parent",
correctOption: "optionA",
},
{
question:
"Which data type is used to create a variable that should store text?",
optionA: "string",
optionB: "Txt",
optionC: "mystring",
optionD: "String",
correctOption: "optionD",
},
{
question:
"What is the process of combining two or more strings called?",
optionA: "concatenation",
optionB: "compilation",
optionC: "iteration",
optionD: "assignment",
correctOption: "optionA",
},
{
question: "What is the keyword used to declare a method in Java?",
optionA: "void",
optionB: "method",
optionC: "function",
optionD: "public",
correctOption: "optionD",
},
{
question:
"What is the default value of an uninitialized variable in Java?",
optionA: "null",
optionB: "0",
optionC: "false",
optionD: "undefined",
correctOption: "optionA",
},
{
question:
"What is the process of creating a new object from an existing object called?",
optionA: "cloning",
optionB: "casting",
optionC: "overriding",
optionD: "abstracting",
correctOption: "optionA",
},
{
question:
"What is the process of converting a primitive data type to an object called?",
optionA: "boxing",
optionB: "casting",
optionC: "overriding",
optionD: "abstracting",
correctOption: "optionA",
},
{
question:
"What is the process of extracting a value from an object called?",
optionA: "accessing",
optionB: "casting",
optionC: "overriding",
optionD: "abstracting",
correctOption: "optionA",
},
];
let shuffledQuestions = []; //empty array to hold shuffled selected questions out of all available questions
function handleQuestions() {
//function to shuffle and push 10 questions to shuffledQuestions array
//app would be dealing with 10questions per session
while (shuffledQuestions.length <= 19) {
const random =
questions[Math.floor(Math.random() * questions.length)];
if (!shuffledQuestions.includes(random)) {
shuffledQuestions.push(random);
}
}
}
let questionNumber = 1; //holds the current question number
let playerScore = 0; //holds the player score
let wrongAttempt = 0; //amount of wrong answers picked by player
let indexNumber = 0; //will be used in displaying next question
// function for displaying next question in the array to dom
//also handles displaying players and quiz information to dom
function NextQuestion(index) {
handleQuestions();
const currentQuestion = shuffledQuestions[index];
document.getElementById("question-number").innerHTML = questionNumber;
document.getElementById("player-score").innerHTML = playerScore;
document.getElementById("display-question").innerHTML =
currentQuestion.question;
document.getElementById("option-one-label").innerHTML =
currentQuestion.optionA;
document.getElementById("option-two-label").innerHTML =
currentQuestion.optionB;
document.getElementById("option-three-label").innerHTML =
currentQuestion.optionC;
document.getElementById("option-four-label").innerHTML =
currentQuestion.optionD;
}
function checkForAnswer() {
const currentQuestion = shuffledQuestions[indexNumber]; //gets current Question
const currentQuestionAnswer = currentQuestion.correctOption; //gets current Question's answer
const options = document.getElementsByName("option"); //gets all elements in dom with name of 'option' (in this the radio inputs)
let correctOption = null;
options.forEach((option) => {
if (option.value === currentQuestionAnswer) {
//get's correct's radio input with correct answer
correctOption = option.labels[0].id;
}
});
//checking to make sure a radio input has been checked or an option being chosen
if (
options[0].checked === false &&
options[1].checked === false &&
options[2].checked === false &&
options[3].checked == false
) {
document.getElementById("option-modal").style.display = "flex";
}
//checking if checked radio button is same as answer
options.forEach((option) => {
if (
option.checked === true &&
option.value === currentQuestionAnswer
) {
document.getElementById(correctOption).style.backgroundColor =
"green";
playerScore++; //adding to player's score
indexNumber++; //adding 1 to index so has to display next question..
//set to delay question number till when next question loads
setTimeout(() => {
questionNumber++;
}, 1000);
} else if (option.checked && option.value !== currentQuestionAnswer) {
const wrongLabelId = option.labels[0].id;
document.getElementById(wrongLabelId).style.backgroundColor = "red";
document.getElementById(correctOption).style.backgroundColor =
"green";
wrongAttempt++; //adds 1 to wrong attempts
indexNumber++;
//set to delay question number till when next question loads
setTimeout(() => {
questionNumber++;
}, 1000);
}
});
}
//called when the next button is called
function handleNextQuestion() {
checkForAnswer(); //check if player picked right or wrong option
unCheckRadioButtons();
//delays next question displaying for a second just for some effects so questions don't rush in on player
setTimeout(() => {
if (indexNumber <= 19) {
//displays next question as long as index number isn't greater than 9, remember index number starts from 0, so index 9 is question 10
NextQuestion(indexNumber);
} else {
handleEndGame(); //ends game if index number greater than 9 meaning we're already at the 10th question
}
resetOptionBackground();
}, 1000);
}
//sets options background back to null after display the right/wrong colors
function resetOptionBackground() {
const options = document.getElementsByName("option");
options.forEach((option) => {
document.getElementById(option.labels[0].id).style.backgroundColor =
"";
});
}
// unchecking all radio buttons for next question(can be done with map or foreach loop also)
function unCheckRadioButtons() {
const options = document.getElementsByName("option");
for (let i = 0; i < options.length; i++) {
options[i].checked = false;
}
}
// function for when all questions being answered
function handleEndGame() {
let remark = null;
let remarkColor = null;
// condition check for player remark and remark color
if (playerScore <= 3) {
remark =
"Bad Grades, Keep Practicing the HTML <a href='https://www.coding-nation.com/java.html'>Click Here</a>.";
remarkColor = "red";
} else if (playerScore >= 8 && playerScore < 14) {
remark =
"Below Average Grades, Keep Practicing the HTML <a href='https://www.coding-nation.com/java.html'>Click Here</a>..";
remarkColor = "orange";
} else if (playerScore >= 14 && playerScore < 17) {
remark =
"Average Grades, Keep Practicing the HTML<a href='https://www.coding-nation.com/java.html'>Click Here</a>..";
remarkColor = "orange";
} else if (playerScore >= 17) {
remark = "Excellent, Keep on Going!.";
remarkColor = "green";
}
const playerGrade = (playerScore / 20) * 100;
//data to display to score board
document.getElementById("remarks").innerHTML = remark;
document.getElementById("remarks").style.color = remarkColor;
document.getElementById("grade-percentage").innerHTML = playerGrade;
document.getElementById("wrong-answers").innerHTML = wrongAttempt;
document.getElementById("right-answers").innerHTML = playerScore;
document.getElementById("score-modal").style.display = "flex";
}
//closes score modal, resets game and reshuffles questions
function closeScoreModal() {
questionNumber = 1;
playerScore = 0;
wrongAttempt = 0;
indexNumber = 0;
shuffledQuestions = [];
NextQuestion(indexNumber);
document.getElementById("score-modal").style.display = "none";
}
//function to close warning modal
function closeOptionModal() {
document.getElementById("option-modal").style.display = "none";
}
</script>
</body>
</html>