CodeBlanks Logo Icon CodeBlanks

Adding questions

Learn to add questions with the addQuestion function.

Adding questions

In the second lesson we add the addQuestion function. It takes a question text and an answer, and adds a new Question to the list.

Example: fun addQuestion(text: String, answer: String) { questions.add(Question(text, answer)) }. This function is called as addQuestion("2+2?", "4"). The questions list is declared at file scope, so the function reaches it directly. To add multiple questions you call the function again. The data model and list come from the first lesson; this lesson only adds the function.

Adding multiple questions

Each call to addQuestion grows the list. Every call adds a new Question instance.

Example: addQuestion("Capital city?", "Ankara"); addQuestion("2+2?", "4"). After these two calls questions has two items. Calling println(questions) prints [Question(text=Capital city?, answer=Ankara), Question(text=2+2?, answer=4)]. Thanks to the data class's automatic toString, the list content is readable.

Practice Add two questions with addQuestion

Build on the first lesson's solution: data class Question(val text: String, val answer: String) and val questions: MutableList<Question> = mutableListOf() are defined. Add a fun addQuestion(text: String, answer: String) function whose body does questions.add(Question(text, answer)). In main call addQuestion("Capital city?", "Ankara") and addQuestion("2+2?", "4"), then print the list with println(questions).

Starter code

Main.kt
data class Question(val text: String, val answer: String)

val questions: MutableList<Question> = mutableListOf()

// add the addQuestion function: takes text and answer, adds a Question to the list

fun main() {
    // add two questions and print the list
}

Sample solution

Main.kt
data class Question(val text: String, val answer: String)

val questions: MutableList<Question> = mutableListOf()

fun addQuestion(text: String, answer: String) {
    questions.add(Question(text, answer))
}

fun main() {
    addQuestion("Capital city?", "Ankara")
    addQuestion("2+2?", "4")
    println(questions)
}

Expected output

[Question(text=Capital city?, answer=Ankara), Question(text=2+2?, answer=4)]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the addQuestion function

Question
fun addQuestion(text: String, answer: String) {
    questions.____(Question(text, answer))
}
Answer
fun addQuestion(text: String, answer: String) {
    questions.add(Question(text, answer))
}

Explanation: questions.add adds a new question.

2 Match the addQuestion parts

Term Meaning
fun addQuestion(text, answer) Function signature
questions.add List append
Question(text, answer) New question instance
addQuestion("2+2?", "4") Call

3 How does the <code>addQuestion</code> function access the <code>questions</code> list?

  • It takes it as a parameter
  • Through a singleton
  • Directly, because it is declared at file scope Correct answer
  • From a companion object

Explanation: questions is declared at file scope; the function reaches it directly.

All lessons in Intermediate