Running the quiz
- Java course
- Intermediate
- Lesson 44/116
- 6 min
Ask questions in turn with run.
Playing the quiz
The third lesson adds a method that plays the quiz. A display method prints all added questions in order. A for loop iterates through the questions in the array.
Example: void display() { for (int i = 0; i < count; i++) { System.out.println(questions[i]); } }. This method prints count questions. questions[i] represents each question; println automatically calls toString. This lets you see the quiz's contents. Later, answer checking will be added to this structure; for now, displaying the questions is enough.
Iterating with a loop
A for loop is used to iterate through an array. The loop variable i starts at zero and increments up to count. Each step accesses one question via questions[i].
Example: for (int i = 0; i < count; i++) { System.out.println(questions[i]); }. Here i starts at zero and increments by one each loop. The loop continues as long as i < count. Questions are accessed in order as questions[0], questions[1]. This is the basic method of iterating arrays. You use this structure when you want to do the same operation for all elements of an array.
Practice Add a display method to Quiz
Add a void display() method to the Quiz class. In its body, loop with i from zero to count; at each step run System.out.println(questions[i]);. In main create a Quiz instance, add two questions and call display().
Starter code
class Question {
String text;
String answer;
Question(String t, String a) {
text = t;
answer = a;
}
public String toString() {
return text + " = " + answer;
}
}
class Quiz {
Question[] questions;
int count;
Quiz() {
questions = new Question[10];
count = 0;
}
void add(Question q) {
questions[count] = q;
count++;
}
}
// Add a display method to Quiz
// Print questions with a for loop
// Instantiate and call display in mainSample solution
class Question {
String text;
String answer;
Question(String t, String a) {
text = t;
answer = a;
}
public String toString() {
return text + " = " + answer;
}
}
class Quiz {
Question[] questions;
int count;
Quiz() {
questions = new Question[10];
count = 0;
}
void add(Question q) {
questions[count] = q;
count++;
}
void display() {
for (int i = 0; i < count; i++) {
System.out.println(questions[i]);
}
}
public static void main(String[] args) {
Quiz quiz = new Quiz();
quiz.add(new Question("Capital", "Paris"));
quiz.add(new Question("City", "London"));
quiz.display();
}
}Expected output
Capital = Paris City = London
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How many questions does the display method print?
- The entire array
- Up to count Correct answer
- 1 question
- 10 questions
Explanation: The display method prints up to count questions.
2 Complete the for loop condition
for (int i = 0; i < ____; i++) {
System.out.println(questions[i]);
}
for (int i = 0; i < count; i++) {
System.out.println(questions[i]);
}
Explanation: The loop runs up to count.
3 Match the loop parts
| Term | Meaning |
|---|---|
int i = 0 |
Start |
i < count |
Condition |
i++ |
Increment |
questions[i] |
Access |