This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod3quiz.html
260 lines (234 loc) · 8.42 KB
/
mod3quiz.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
<html>
<head>
<h1 align="center">LEVEL 3 MODULE 3 QUIZ</h1>
</head>
<body onload="startup()" style="background-color:#bbccdd;font-weight:bold;font-size:22px;">
<pre style="white-space:pre-wrap;"><code>
<div id="docBody"style="overflow:visible;"></div>
</code></pre>
</body>
<script>
function Question(){
this.question = "";
this.choices = [];
this.correctAnswer = "";
this.userAnswer = "";
this.visited = false;
}
var body;
var questions = [];
var currentQuestion;
function startup(){
currentQuestion = 0;
body = document.getElementById("docBody");
questions[0] = new Question();
questions[0].question = "Fill in the blank.<br><br>"+
"abstract class Shape{<br>"+
"}<br><br>"+
"public class Square _______ Shape{<br>"+
"}<br>";
questions[0].choices[0] = "implements";
questions[0].choices[1] = "extends";
questions[0].choices[2] = "run";
questions[0].choices[3] = "inherits";
questions[0].choices[4] = "elaborates";
questions[0].correctAnswer = 1;
questions[1] = new Question();
questions[1].question = "Fill in the blank.<br><br>"+
"interface Clickable{<br>"+
"}<br><br>"+
"public class Button _______ Clickable{<br>"+
"}<br>";
questions[1].choices[0] = "implements";
questions[1].choices[1] = "extends";
questions[1].choices[2] = "run";
questions[1].choices[3] = "inherits";
questions[1].choices[4] = "elaborates";
questions[1].correctAnswer = 0;
questions[2] = new Question();
questions[2].question = "Why will the following code fail to compile?<br><br>"+
"interface ActionItem{<br>"+
"\tpublic void performAction();<br>"+
"}<br><br>"+
"public class Junction implements ActionItem{<br>"+
"}<br>";
questions[2].choices[0] = "\"implements\" should be changed to \"extends\".";
questions[2].choices[1] = "ActionItem must define the performAction method.";
questions[2].choices[2] = "Junction must define the performAction method.";
questions[2].choices[3] = "A third data structure is needed to link Junction with ActionItem.";
questions[2].choices[4] = "The code should compile just fine.";
questions[2].correctAnswer = 2;
questions[3] = new Question();
questions[3].question = "Why will the following code fail to compile?<br><br>"+
"abstract class Block{<br>"+
"}<br><br>"+
"public class House{<br>"+
"\tpublic static void main(String[] args){<br>"+
"\t\tBlock b = new Block();<br>"+
"\t}<br>"+
"}<br>"
questions[3].choices[0] = "Block can not be instantiated.";
questions[3].choices[1] = "The Block constructor needs a parameter.";
questions[3].choices[2] = "The Block class needs at least one method.";
questions[3].choices[3] = "The House class must extend Block.";
questions[3].choices[4] = "The code should compile fine.";
questions[3].correctAnswer = 0;
questions[4] = new Question();
questions[4].question = "Why will the following code fail to compile?<br><br>"+
"abstract class Building{<br>"+
"\t//code<br>"+
"}<br><br>"+
"class Tower extends Building{<br>"+
"\t//code<br>"+
"}<br><br>"+
"public class BuildingBuilder{<br>"+
"\tpublic static void main(String[] args){<br>"+
"\t\tBuilding eiffelTower = new Tower();<br>"+
"\t\tnew BuildingBuilder().constructBuilding(eiffelTower);<br>"+
"\t}<br><br>"+
"\tpublic constructBuilding(Building b){<br>"+
"\t\t//some code<br>"+
"\t}<br>"+
"}";
questions[4].choices[0] = "Tower can not be instantiated.";
questions[4].choices[1] = "The Tower constructor needs a parameter.";
questions[4].choices[2] = "constructBuilding cannot take a Building object as a parameter.";
questions[4].choices[3] = "The BuildingBuilder class must extend Building.";
questions[4].choices[4] = "The code should compile fine.";
questions[4].correctAnswer = 4;
questions[5] = new Question();
questions[5].question = "Which of the following is NOT a true statement?";
questions[5].choices[0] = "Interfaces can only have abstract methods and statically defined methods.";
questions[5].choices[1] = "Abstract classes can have non-abstract methods.";
questions[5].choices[2] = "A class can only extend one abstract class.";
questions[5].choices[3] = "A class can only implement one interface.";
questions[5].choices[4] = "An interface cannot have any private methods.";
questions[5].correctAnswer = 3;
setDivText(questions[0]);
setButtons(questions[0]);
}
function setDivText(q){
body.innerHTML += currentQuestion + 1 + ".<br>";
body.innerHTML += q.question + "<br><br><hr size=\"1\">";
if(q.choices.length == 0){
body.innerHTML += "<br><textarea id=\"answer\" rows=\"10\" cols=\"80\">" +
q.userAnswer + "</textarea><br>";
}else{
for(var i = 0; i < q.choices.length; i++){
if(i == q.userAnswer && q.visited){
body.innerHTML += convertChar(i) + "<input type=\"radio\" checked=\"true\"name=\"answer\" id=\"" + i + "\">" +q.choices[i] + "</input><br><br>";
}else{
body.innerHTML += "<input type=\"radio\" name=\"answer\" id=\"" + i + "\"><br>" +q.choices[i] + "</input><br><hr size=\"1\">";
}
}
q.visited = true;
}
}
function convertChar(a){
switch(a){
case 0:
case "0": return "A";
case 1:
case "1": return "B";
case 2:
case "2": return "C";
case 3:
case "3": return "D";
case 4:
case "4": return "E";
}
}
function setButtons(q){
if(q == questions[0]){
if(questions.length > 1){
body.innerHTML += "<button onclick=\"nextQuestion()\">NEXT</button>";
}else{
body.innerHTML += "<button onclick=\"submit()\">SUBMIT</button>";
}
}
else if(q == questions[questions.length - 1]){
body.innerHTML += "<button onclick=\"previousQuestion()\">PREVIOUS</button>";
body.innerHTML += "<button onclick=\"submit()\">SUBMIT</button>";
}else{
body.innerHTML += "<button onclick=\"previousQuestion()\">PREVIOUS</button>";
body.innerHTML += "<button onclick=\"nextQuestion()\">NEXT</button>";
}
}
function nextQuestion(){
if(setUserAnswer()){
body.innerHTML = "";
currentQuestion++;
setDivText(questions[currentQuestion]);
setButtons(questions[currentQuestion]);
}
}
function previousQuestion(){
if(setUserAnswer()){
body.innerHTML = "";
currentQuestion--;
setDivText(questions[currentQuestion]);
setButtons(questions[currentQuestion]);
}
}
function setUserAnswer(){
var q = questions[currentQuestion];
if(q.choices.length > 0){
try{
q.userAnswer = document.querySelector('input[name="answer"]:checked').id;
return true;
}catch(err){
alert("Make a choice. You can come back and change it later.");
return false;
}
}else{
q.userAnswer = document.getElementById("answer").value.trim();
return true;
}
}
function checkQuiz(){
var total = 0;
for(var i = 0; i < questions.length; i++){
var q = questions[i];
if(q.choices.length == 0){
if(q.userAnswer.trim() == q.correctAnswer){
total++;
}
} else {
if(q.correctAnswer == q.userAnswer){
total++;
}
}
}
return total;
}
function reloadPage(){
location = location;
}
function submit(){
setUserAnswer();
body.innerHTML = "You got " + checkQuiz() + " out of " + questions.length + " correct.<br><hr size=\"3\">";
for(var i = 0; i < questions.length; i++){
if(questions[i].choices.length > 0){
body.innerHTML += i + 1 + ". ";
body.innerHTML += questions[i].question + "<br><br>";
body.innerHTML += "Your Answer: <br>";
body.innerHTML += questions[i].choices[questions[i].userAnswer] + "<br>";
body.innerHTML += "Correct Answer: <br>";
body.innerHTML += questions[i].choices[questions[i].correctAnswer] + "<br>";
body.innerHTML += "<hr size=\"3\">";
}else{
body.innerHTML += i + 1 + ". ";
body.innerHTML += questions[i].question + "<br><br>";
body.innerHTML += "Your Answer: <br>";
body.innerHTML += questions[i].userAnswer + "<br>";
body.innerHTML += "Correct Answer: <br>";
body.innerHTML += questions[i].correctAnswer + "<br>";
body.innerHTML += "<hr size=\"3\">";
}
}
body.innerHTML += "<button onclick=\"reloadPage()\">START OVER</button>";
}
</script>
<script src="http://league-level0.github.io/copyrightFooter.js"></script>
<div id="copyright"><script>copyright();</script></div>
</html>