Planning a Quiz App

On this page, you will begin to develop a quiz app by creating an abstract data type to pair the questions with their answers.

  1. Talk with Your PartnerDecide what type of quiz you would like to build, and write three to five questions and their correct answers.
  2. Take turns speakingRead the following section. (Don't build these in Snap!. You'll build your own quiz later on the page.)

: Sublist

A sublist is a list used as an item of another list.

(The word sublist is also used to refer to some subset of a list.)

You'll use a list to store your quiz items in a global variable and use a sublist for each question/answer pair. Then you can choose just one pair at a time to work with. set (computer science quiz) to (list (list (What is the computer science term for looping or repetition?) (iteration)) (list (What is the name for the abstraction in a program that can hold a value?) (variable)) (list (What kind of variable is available only in the part of the program where it is defined?) (local variable))) ask (item(1) of (item (3) of (computer science quiz))) and wait
if (item (2) of (item (3) of (computer science quiz)) = answer) {
    say (That's correct!) for (2) secs
}

AAP.1.D.2, AAP.1.D.3, AAP.1.D.4

But code like item (1) of (item (3) of (computer science quiz)) is hard to read and understand. A better way is to use abstraction to organize the quiz items. The abstraction just hides the list and item of blocks, so it isn't complicated to build, but it can make your code much easier to write, read, and debug.
set (computer science quiz) to (list (question: (What is the computer science term for looping or repetition?) answer: (iteration)) (question: (What is the name for the abstraction in a program that can hold a value?) answer: (variable)) (question: (What kind of variable is available only in the part of the program where it is defined?) answer: (local variable))) ask (question of quiz item (item (3) of (computer science quiz))) and wait
if (answer = answer from quiz item (item (3) of (computer science quiz))) {
    say (That's correct!) for (2) secs
}

: Data Types
: Abstract Data Types The constructor and selector together implement the quiz item abstract data type.
photo of Reshma Saujani

Reshma Saujani (born 1975) founded the nonprofit organization Girls Who Code in response to the historical decline in the number of women employed in computer science fields. According to their website, in 1995, 37% of computer scientists were women. As of 2023, it’s only 24%. Reshma and Girls Who Code aim to fix this imbalance by running clubs that teach girls CS. In addition to being an activist, Reshma was the first Indian American woman to run for Congress and has served as New York City’s Deputy Public Advocate, where she created innovative partnerships to support DREAMers and promote campaign finance reform, among other initiatives.

Video: Teach girls bravery, not perfection

  1. "U2L2-Quiz"Start a New Project called U2L2-Quiz 
  2. AAP-1.D
  3. Build the custom quiz item abstract data type (both the constructor and the two selectors).
  4. Specifying an Input Type

    Your selectors expect a quiz item, i.e., a list, as input. You can make your blocks show what type of data they expect. It's not necessary in Snap! but, like assigning a color to a block, it can be a helpful reminder of what the block does and what type of input it expects. You've already seen input slots of several shapes, indicating different expected data types.

    In the Block Editor while creating a selector, click on a plus sign to enter an input name. Then...

    1. Click on the arrow to the right of the input name:
      create input name right arrow
    2. Choose the data type you want for that input. (For this project, you'll use the "text" and "list" input types.)
    3. Click "OK."

  5. Create a global variable to store your quiz items and initialize it as a list of items, using your constructor where appropriate.
  6. Snap! has two different views for lists within lists. You can switch which view you see by right-clicking (or control-clicking) on the quiz watcher (or whatever you called the variable) on the stage.
    If you don't see the watcher on the stage, make sure the checkbox beside the quiz variable in the Variables palette is checked. quiz watcher checked
    • Table View for computer science quiz Watcher (your quiz will be different):
      cs quiz watcher with first column expanded
      My watcher is too narrow.
      At first the watcher will look like this:
      cs quiz watcher at initial size
      Drag out the resize handle at the lower right to get this:
      cs quiz watcher resized
      Then hover your mouse over the letter A near the top. It will turn into a digit 1. Grab it and drag it to the right:
      cs quiz watcher with first column expanded
    • List View for computer science quiz Watcher:
      cs quiz watcher, list view
    : Table

    A table is a two-dimensional data structure with rows and columns. If you've used a spreadsheet program, what it displays is a table.

    In Snap!, a table is implemented as a list of lists, in which each sublist is one row of the table.

    Save your work
  7. Test both the selectors for different items in your list of quiz items, and debug any problems.
  8. Imagine you make a variable capitals and use set to give this list of lists a name:
    set (capitals) to {{Augusta, Maine}, {Boise, Idaho}, {Columbia, South Carolina}, {Des Moines, Iowa}}
    Which of the following statements are true?

    Choose all that apply.

    item (1) of (capitals) reports a list with two items: "Augusta" and "Maine".
    item (1) of (item (1) of (capitals)) reports the word "Augusta".
    item (last) of (item (2) of (capitals)) reports the word "Iowa".
    (length) of (capitals) reports the number 8.
    all but first of (item (last) of (capitals)) reports a list with one item: "Iowa".
    item (3) of (capitals) reports the string "Boise".
    item (last) of (all but first of (capitals)) reports the string "Iowa".
    (length) of (item (1) of (capitals)) reports the number 2.