-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_quiz.html
200 lines (188 loc) · 8.14 KB
/
python_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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python 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>Python Language Quiz</h1>
<div id="quiz"></div>
<button id="nextButton">Next Question</button>
<div id="result" class="result"></div>
</div>
<script>
const questions = [
{
question: "1. Which of the following is not a valid data type in Python?",
options: ["int", "char", "float", "string"],
answer: "char"
},
{
question: "2. What is the output of the following code?\n\nx = 5\ny = 2\nprint(x ** y)",
options: ["10", "25", "7", "Error"],
answer: "25"
},
{
question: "3. How do you create a comment in Python?",
options: ["//This is a comment", "# This is a comment", "// This is a comment", "/* This is a comment */"],
answer: "# This is a comment"
},
{
question: "4. What is the correct way to create a function in Python?",
options: ["def myFunction():", "function myFunction():", "function myFunction{}", "def myFunction{}:"],
answer: "def myFunction():"
},
{
question: "5. What is the output of the following code?\n\nx = 'Hello'\nprint(len(x))",
options: ["5", "6", "Error", "Undefined"],
answer: "5"
},
{
question: "6. What is the output of the following code?\n\nx = 'Hello'\nprint(x[0])",
options: ["H", "e", "Error", "Undefined"],
answer: "H"
},
{
question: "7. How do you start a loop in Python?",
options: ["for loop:", "for loop()", "for loop in:", "for loop() in:"],
answer: "for loop in:"
},
{
question: "8. What is the output of the following code?\n\nfor x in range(5):\n print(x)",
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: "9. Which of the following is not a valid comparison operator in Python?",
options: ["==", "<>", ">=", "<="],
answer: "<>"
},
{
question: "10. What is the output of the following code?\n\nx = True\ny = False\nprint(x and y)",
options: ["True", "False", "Error", "Undefined"],
answer: "False"
},
{
question: "11. What is the output of the following code?\n\nx = True\ny = False\nprint(x or y)",
options: ["True", "False", "Error", "Undefined"],
answer: "True"
},
{
question: "12. How do you define a list in Python?",
options: ["(1, 2, 3)", "[1, 2, 3]", "{1, 2, 3}", "1 2 3"],
answer: "[1, 2, 3]"
},
{
question: "13. What is the output of the following code?\n\nx = [1, 2, 3]\nprint(x[1])",
options: ["1", "2", "3", "Error"],
answer: "2"
},
{
question: "14. What is the output of the following code?\n\nx = [1, 2, 3]\nprint(len(x))",
options: ["1", "2", "3", "Error"],
answer: "3"
},
{
question: "15. How do you add an element to a list in Python?",
options: ["append()", "add()", "insert()", "push()"],
answer: "append()"
},
{
question: "16. What is the output of the following code?\n\nx = [1, 2, 3]\nx.append(4)\nprint(x)",
options: ["[1, 2, 3]", "[1, 2, 3, 4]", "[1, 2, 4, 3]", "[4, 1, 2, 3]"],
answer: "[1, 2, 3, 4]"
},
{
question: "17. What is the output of the following code?\n\nx = [1, 2, 3]\nx.remove(2)\nprint(x)",
options: ["[1, 2, 3]", "[1, 3]", "[2, 3]", "[1, 2]"],
answer: "[1, 3]"
},
{
question: "18. 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]", "[3, 2, 1]"],
answer: "[1, 2]"
},
{
question: "19. How do you define a tuple in Python?",
options: ["(1, 2, 3)", "[1, 2, 3]", "{1, 2, 3}", "1 2 3"],
answer: "(1, 2, 3)"
},
{
question: "20. What is the output of the following code?\n\nx = (1, 2, 3)\nprint(x[1])",
options: ["1", "2", "3", "Error"],
answer: "2"
},
{
question: "21. What is the output of the following code?\n\nx = (1, 2, 3)\nprint(len(x))",
options: ["1", "2", "3", "Error"],
answer: "3"
},
{
question: "22. How do you add an element to a tuple in Python?",
options: ["append()", "add()", "insert()", "Tuples are immutable"],
answer: "Tuples are immutable"
},
{
question: "23. What is the output of the following code?\n\nx = (1, 2, 3)\nx.remove(2)\nprint(x)",
options: ["(1, 2, 3)", "(1, 3)", "(2, 3)", "Error"],
answer: "Error"
},
{
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>