Computer Science Grade 8 20 min

Surveys and Questions

Surveys and Questions

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Design a `Question` class using Object-Oriented Programming (OOP) principles to model different question types. Implement a `Survey` class that contains an array of `Question` objects. Write code that uses conditional logic (if/else) to handle various question formats like multiple-choice and text input. Apply basic data validation to ensure user responses match the expected format for a given question. Iterate through an array of `Question` objects to display a full survey and collect responses. Explain how object composition is used to build a complex `Survey` object from simpler `Question` objects. Ever wonder how online quizzes or feedback forms work? 🤔 Let's pull back the curtain and learn how to build the 'brain' behind them using co...
2

Key Concepts & Vocabulary

TermDefinitionExample Object CompositionBuilding complex objects by combining simpler objects. Instead of inheriting properties, a class 'has-a' relationship with other classes.A `Survey` object is composed of an array of `Question` objects. The Survey 'has-a' list of Questions. Question TypeA property that defines the format of a question and how it should be answered, such as 'multiple-choice', 'text', or 'true/false'.A question with type 'multiple-choice' would display a list of options, while a 'text' type would display an input box. Conditional LogicUsing statements like `if`, `else if`, and `else` to make a program perform different actions based on a condition, like the question type.`if (question.type == 'm...
3

Core Syntax & Patterns

The Question Class Pattern class Question { constructor(text, type, options) { this.text = text; this.type = type; this.options = options; // an array } } Use this class as a blueprint to create individual question objects. The `constructor` initializes each new question with its text, type (e.g., 'multiple-choice'), and an array of possible answers. The Survey Class Pattern (Composition) class Survey { constructor(title) { this.title = title; this.questions = []; // an empty array } addQuestion(question) { this.questions.push(question); } } Use this class to manage a collection of questions. It 'has-a' list of questions. The `addQuestion` method allows you to add `Question` objects to the survey's internal array....

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
The tutorial's `Survey` class holds `Question` objects. You now need to build a `Quiz` that not only asks questions but also grades them. What is the most logical change to the `Question` class and what new method would the `Quiz` class need?
A.Add a `isCorrect` property to `Question`; add a `checkAnswers()` method to `Quiz`.
B.Add a `correctAnswer` property to `Question`; add a `calculateScore()` method to `Quiz`.
C.Add a `score` property to `Survey`; add a `grade()` method to `Question`.
D.No change to `Question`; add a `grade(question, answer)` method to `Quiz`.
Challenging
A developer suggests making `Survey` a child class of `Question` (using inheritance) instead of using composition. Why is this a fundamentally poor design choice?
A.Inheritance is an outdated concept and should not be used.
B.`Survey` is not a type of `Question`; this violates the 'is-a' principle of inheritance. A survey 'has-a' set of questions.
C.The code would be too difficult to read.
D.`Survey` class cannot have an array as a property if it inherits from another class.
Challenging
You want to implement 'skip logic': if a user answers 'Yes' to Q1, they see Q2; if 'No', they skip to Q3. The current `for...of` loop processes questions sequentially. What fundamental change is needed to support this?
A.Use a `while` loop with a counter variable (e.g., `i`) that can be manually changed inside the loop based on the user's answer.
B.Add a `skipped` property to the `Question` class.
C.Nest multiple `for` loops inside each other.
D.Run the survey in reverse order, from the last question to the first.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.