Test your knowledge with 10 questions!
// Basic Quiz App in C
#include
#include
int quiz(struct Question a[], int size) {
int score = 0;
for (int i = 0; i < size; i++) {
printf("%s\n", a[i].question);
for (int j = 0; j < 4; j++) {
printf("%d. %s\n", j + 1, a[i].options[j]);
}
int usera;
while (1) {
printf("Enter the correct option number: ");
if (scanf("%d", &usera) != 1) {
while (getchar() != '\n'); // Clear input buffer
printf("Invalid input. Please enter a number.\n");
continue;
}
if (usera >= 1 && usera <= 4) {
break;
} else {
printf("Invalid input. Please enter a number within the range of options.\n");
}
}
if (strcmp(a[i].options[usera - 1], a[i].answer) == 0) {//Arrays are 0-based, so we use usera - 1.
printf("Correct!\n");
score++;
} else {
printf("Incorrect. The correct answer was: %s\n", a[i].answer);
}
//strcmp(...) == 0:
//strcmp is used to compare two strings. It returns 0 if they are exactly equal.
//So this condition checks: "Did the user pick the correct option?"
printf("\n");
}
printf("Quiz complete! Your score is: %d/%d\n", score, size);
return score;
}
struct Question {
char question[256];
char options[4][100];
char answer[100];
};
int main() {
struct Question quizData[] = {
{
"What is the name of Eren Yeager Titan name?",
{ "Colossal Titan", "Armored Titan", "Female Titan", "Attack Titan" },
"Attack Titan"
},
{
"What type of creature does Kaneki become in Tokyo Ghoul?",
{ "Vampire", "Ghoul", "Demon", "Werewolf" },
"Ghoul"
}
};
int size = sizeof(quizData) / sizeof(quizData[0]);
quiz(quizData, size);
return 0;
}
// Basic Quiz App in C++
#include
#include // For strcmp
struct Question {
char question[256];
char options[4][100];
char answer[100];
};
int quiz(Question a[], int size) {
int score = 0;
for (int i = 0; i < size; i++) {
std::cout << "Q" << i + 1 << ": " << a[i].question << std::endl;
for (int j = 0; j < 4; j++) {
std::cout << j + 1 << ". " << a[i].options[j] << "\n";
}
int choice;
bool valid = false;
// Input validation loop
while (!valid) {
std::cout << "Enter the correct option number (1-4): ";
std::cin >> choice;
if (std::cin.fail() || choice < 1 || choice > 4) {
std::cin.clear(); // Clear error state
std::cin.ignore(10000, '\n'); // Flush invalid input
std::cout << "Invalid input. Please enter a number between 1 and 4.\n";
} else {
valid = true;
}
}
if (std::strcmp(a[i].options[choice - 1], a[i].answer) == 0) {//Arrays are 0-based, so we use usera - 1.
std::cout << "Correct!\n\n";
score++;
} else {
std::cout << "Wrong. The correct answer was: " << a[i].answer << "\n\n";
}
}
return score;
}
int main() {
struct Question quizData[] = {
{
"What is the name of Eren Yeager Titan name?",
{ "Colossal Titan", "Armored Titan", "Female Titan", "Attack Titan" },
"Attack Titan"
},
{
"What type of creature does Kaneki become in Tokyo Ghoul?",
{ "Vampire", "Ghoul", "Demon", "Werewolf" },
"Ghoul"
}
};
int size = sizeof(quizData) / sizeof(quizData[0]);
quiz(quizData, size);
return 0;
}
// Basic Quiz App in Java
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
class Question {
String question;
List options;
String answer;
Question(String question, List options, String answer) {
this.question = question;
this.options = options;
this.answer = answer;
}
}
class Quiz {
private List questions;
private Scanner scanner;
Quiz(List questions) {
this.questions = questions;
this.scanner = new Scanner(System.in);
}
public void run() {
int score = 0;
for (Question q : questions) {
System.out.println(q.question);
for (int j = 0; j < q.options.size(); j++) {
System.out.println((j + 1) + ". " + q.options.get(j));
}
int user = 0;
while (true) {
System.out.print("Enter the number of your answer: ");
try {
user = Integer.parseInt(scanner.nextLine());
if (user >= 1 && user <= q.options.size()) {
break;
} else {
System.out.println("Invalid input. Please enter a number within range.");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
}
}
if (q.options.get(user - 1).equals(q.answer)) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Incorrect. The correct answer was: " + q.answer + "\n");
}
}
System.out.println("Quiz complete! Your score is: " + score + "/" + questions.size());
scanner.close();
}
}
public class Main {
public static void main(String[] args) {
List questions = new ArrayList<>();
List options1 = List.of("Colossal Titan", "Armored Titan", "Female Titan", "Attack Titan");
questions.add(new Question("What is the name of Eren Yeager's Titan?", options1, "Attack Titan"));
List options2 = List.of("Vampire", "Ghoul", "Demon", "Werewolf");
questions.add(new Question("What type of creature does Kaneki become?", options2, "Ghoul"));
Quiz quiz = new Quiz(questions);
quiz.run();
}
}
# Python Anime Quiz Code
def quiz(a):
s=0
for i in a:
print(i['question'])
for j,option in enumerate(i['options']):
print(f"{j+1}.{option}")
while True:
try:
uans=int(input("enter the correct options : "))
if 1 <= uans <= len(i['options']):
break
else:
print("Invalid input. Please enter a number within the range of options.")
except ValueError:
print("Invalid input. Please enter a number.")
if i['options'][uans-1]==i['answer']:
print("correct ")
s+=1
else:
print("incorrect")
print(f"Quiz complete! Your score is: {s}/{len(a)}")
a=[
{
'question': 'What is the name of Eren Yeager Titan name?',
"options": ["Colossal Titan", "Armored Titan", "Female Titan", "Attack Titan"],
"answer": "Attack Titan"
},
{
"question": "What type of creature does Kaneki become in Tokyo Ghoul?",
"options": ["Vampire", "Ghoul", "Demon", "Werewolf"],
"answer": "Ghoul"
}
]
quiz(a)
// Basic Quiz App in JavaScript
const quiz = [
{
question: "1. What is the capital of India?",
options: ["Delhi", "Mumbai", "Chennai", "Kolkata"],
answer: 0
},
{
question: "2. What is 2 + 2?",
options: ["3", "4", "2", "5"],
answer: 1
},
{
question: "3. Which planet is known as the Red Planet?",
options: ["Earth", "Venus", "Mars", "Jupiter"],
answer: 2
},
{
question: "4. Who wrote 'Harry Potter'?",
options: ["J.R.R. Tolkien", "J.K. Rowling", "George R.R. Martin", "Rick Riordan"],
answer: 1
},
{
question: "5. What is the largest mammal?",
options: ["Elephant", "Giraffe", "Blue Whale", "Hippo"],
answer: 2
},
{
question: "6. What is the boiling point of water?",
options: ["90°C", "100°C", "110°C", "120°C"],
answer: 1
},
{
question: "7. HTML stands for?",
options: ["HighText Markup Language", "HyperText Markup Language", "HyperTabular Markup Language", "None"],
answer: 1
},
{
question: "8. Which one is a JavaScript framework?",
options: ["Flask", "Django", "React", "Laravel"],
answer: 2
},
{
question: "9. What does CSS stand for?",
options: ["Creative Style System", "Cascading Style Sheets", "Computer Style Structure", "Colorful Style Syntax"],
answer: 1
},
{
question: "10. Which company created Java?",
options: ["Oracle", "Sun Microsystems", "Microsoft", "IBM"],
answer: 1
}
];
let current = 0;
let score = 0;
function loadQuestion() {
if (current >= quiz.length) {
document.getElementById("questionText").textContent = "Quiz Finished!";
document.getElementById("options").innerHTML = "";
document.getElementById("scoreBox").textContent = `🎉 Your Score: ${score} / ${quiz.length}`;
return;
}
const q = quiz[current];
document.getElementById("questionText").textContent = q.question;
const optionsDiv = document.getElementById("options");
optionsDiv.innerHTML = "";
q.options.forEach((opt, i) => {
const btn = document.createElement("button");
btn.className = "btn btn-outline-primary option-btn";
btn.textContent = opt;
btn.onclick = () => {
if (i === q.answer) score++;
nextQuestion();
};
optionsDiv.appendChild(btn);
});
}
function nextQuestion() {
current++;
loadQuestion();
}
function toggleCode() {
const section = document.getElementById("codeSection");
section.style.display = section.style.display === "none" ? "block" : "none";
}
function showCode(lang) {
document.querySelectorAll("pre").forEach(pre => pre.style.display = "none");
document.getElementById(lang).style.display = "block";
}
window.onload = loadQuestion;