[go: up one dir, main page]

0% found this document useful (0 votes)
9 views18 pages

Quiz App

The document contains XML layout code for a quiz application interface, including a question display, multiple-choice options, and a 'Next' button. The accompanying Java code defines the main activity of the app, handling question loading, user input, and score tracking. It features an array of questions, options, and correct answers, with functionality to navigate through the quiz and display the final score.

Uploaded by

riyatorane1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

Quiz App

The document contains XML layout code for a quiz application interface, including a question display, multiple-choice options, and a 'Next' button. The accompanying Java code defines the main activity of the app, handling question loading, user input, and score tracking. It features an array of questions, options, and correct answers, with functionality to navigate through the quiz and display the final score.

Uploaded by

riyatorane1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

XML Code:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question"
android:textSize="20sp"
android:layout_marginBottom="20dp" />

<RadioGroup
android:id="@+id/options"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />

<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />

<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />

<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 4" />
</RadioGroup>

<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_marginTop="20dp" />
</LinearLayout>

JAVA Code:
package com.example.quizapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

TextView questionText;
RadioGroup optionsGroup;
RadioButton option1, option2, option3, option4;
Button nextBtn;

String[] questions = {
"1. What is JVM?",
"2. Which keyword is used to inherit a class in Java?",
"3. What is the size of int in Java?",
"4. Which method is the entry point of a Java program?",
"5. What is the default value of boolean in Java?",
"6. What is Encapsulation in Java?",
"7. Which exception is thrown when a divided by zero?",
"8. Can we overload the main method?",
"9. What is the parent class of all Java classes?",
"10. What is the output of: System.out.println(10 + 20 + \"Java\")?"
};

String[][] options = {
{"Java Virtual Machine", "Java Very Machine", "Java Verified Machine", "None"},
{"this", "import", "extends", "implement"},
{"4 bytes", "2 bytes", "8 bytes", "Depends on system"},
{"main()", "start()", "run()", "init()"},
{"true", "false", "0", "null"},
{"Wrapping data & code", "Inheritance", "Polymorphism", "Abstraction"},
{"ArithmeticException", "NullPointerException", "IOException",
"ArrayIndexOutOfBoundsException"},
{"No", "Yes", "Only in Java 8", "Only in Java 11"},
{"Object", "Class", "Main", "Parent"},
{"30Java", "Java1020", "1020Java", "Java30"}
};

String[] answers = {
"Java Virtual Machine",
"extends",
"4 bytes",
"main()",
"false",
"Wrapping data & code",
"ArithmeticException",
"Yes",
"Object",
"30Java"
};

int index = 0;
int score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

questionText = findViewById(R.id.question);
optionsGroup = findViewById(R.id.options);
option1 = findViewById(R.id.option1);
option2 = findViewById(R.id.option2);
option3 = findViewById(R.id.option3);
option4 = findViewById(R.id.option4);
nextBtn = findViewById(R.id.nextBtn);

loadQuestion();

nextBtn.setOnClickListener(v -> {
int selectedId = optionsGroup.getCheckedRadioButtonId();

if (selectedId == -1) {
Toast.makeText(MainActivity.this, "Please select an option",
Toast.LENGTH_SHORT).show();
return;
}

RadioButton selectedOption = findViewById(selectedId);


String answer = selectedOption.getText().toString();

if (answer.equals(answers[index])) {
score++;
}

index++;

if (index < questions.length) {


loadQuestion();
} else {
showResult();
}
});
}

private void loadQuestion() {


questionText.setText(questions[index]);
option1.setText(options[index][0]);
option2.setText(options[index][1]);
option3.setText(options[index][2]);
option4.setText(options[index][3]);
optionsGroup.clearCheck();
}

private void showResult() {


questionText.setText("Quiz Over! Your Score: " + score + "/" + questions.length);
optionsGroup.setVisibility(View.GONE);
nextBtn.setVisibility(View.GONE);
}
}

Output:

You might also like