What should I add to print all the questions I have passed in the driver class?

Stack Overflow Asked by Suvab on December 12, 2020

These are my quiz and question class.

class Question{
    String question, answer;
        question = null;
        answer = null;
    public Question(String question, String answer)
        this.question = question;
        this.answer = answer;
    public String toString(){
        return question;

class Quiz extends Question{
    String name;
    Quiz(String name)
        super(); = name;

    public void addQuestion(Question q)
        answer = q.answer;
        question = q.question;
    public String toString(){
        return name+"nn" + question;

And this is my driver class.

public class QuizSample{
    public static void main(String[] args){
        Quiz sample = new Quiz("Sample 1903 Quiz");
        sample.addQuestion(new Question("Who is known as the father of Java?", "James Gosling"));
        sample.addQuestion(new Question("Write a statement that assigns the length of a string s to int i", "i = s.length();"));
        sample.addQuestion(new Question("True or false: assigning an int to double is an example of a widening conversion", "true"));

This is the output:

Sample 1903 Quiz
True or false: assigning an int to double is an example of a widening conversion

But I want to print all the questions that I have passed.
Please help guys.

2 Answers

Your Quiz class can have a List to store all the questions,


List<Questions> lstQuestions = new ArrayList<Questions>();

and can be used in addQuestion method to add all the questions,

 public void addQuestion(Question q)

and the main method can look like below,

public static void main(String[] args) {
        Scanner console = new Scanner(;
        System.out.println("Welcome to my Quiz");
        for(Question quest : lstQuestion){
            String ans =;
                System.out.println("right answer");
                System.out.println("wrong answer");


Answered by Prabhu on December 12, 2020

Class Quiz does not need to extend Question but rather to have a collection of questions (list or set).

Also, toString method needs to be updated to pretty print questions.

import java.util.*;

class Quiz {
    String name;
    List<Question> questions = new ArrayList<>();
    Quiz(String name) { = name;

    public void addQuestion(Question q) {
    public String toString() {
        return name+"nn" +

Answered by Alex Rudenko on December 12, 2020

Add your own answers!

Related Questions

C++ Selection Sort not Sorting Some Values of an Array

1  Asked on January 3, 2022 by steffen-brown


SIM serial information API level 29

1  Asked on January 3, 2022 by fturriaf


AJAX to fetch, get same funcionality

1  Asked on January 3, 2022 by reinaldo-ep


Using Sequelize CLI with the ESM package

1  Asked on January 3, 2022 by wdgelwix


Why is my psycopg2 not working with python3?

0  Asked on January 3, 2022 by nicole-ganung


Problem with even indexes in Java program

6  Asked on January 1, 2022 by iothio


calling String method from different class to main class

1  Asked on January 1, 2022 by chan-myae-tun


Re-arranging a two-way table

2  Asked on January 1, 2022


How to iterate through json file in Java

2  Asked on January 1, 2022 by francemadrid


COUNT Multiple Columns using GROUP BY – SQL

3  Asked on January 1, 2022 by barnee


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP