Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon

Building Class Hierarchies with Inheritance in Python

Inheritance commonly gets a bad reputation and is fairly easy to misuse, but when building a system that requires multiple variations of the same general concept, then it is the right tool for the job. Knowing when and how to use inheritance is important for being the most productive object-oriented programmer that you can be. In this hands-on lab, you'll be building the class hierarchy for question types used in a quiz system. By completing this lab, you'll have demonstrated that you know how to factor class logic across a tree of classes to minimize repeated code and leverage how inheritance allows you to share logic.

Google Cloud Platform icon

Path Info

Clock icon Advanced
Clock icon 1h 15m
Clock icon Mar 26, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Implement the Base Question Class

    The base Question class is the class for multiple-choice, single answer questions. For this class to meet the business requirements, it will need to have the proper initialization, a select method, a grade method, and a customized __str__ method. The string output should work by printing the following:

    1. [OPTION]
    2. [OPTION]
    3. [OPTION]
    4. [OPTION]
  2. Challenge

    Create the TrueFalseQuestion Class by Subclassing Question

    When looking at the TrueFalseQuestion class, we want to simplify the initialization since the options are obvious, and we'd also like to customize the printed representation so that it starts with True/False:, unless the question text provided already begins with the words "True" or "False" (ignoring case). Here's what the output would look like:

    True/False: [QUESTION_TEXT]
    1. [True or False (randomized order)]
    2. [Opposite of option 1.]
  3. Challenge

    Create the MultipleSelectQuestion Class by Subclassing Question

    The biggest difference between a multiple-choice, multiple correct answer question type and the standard multiple choice question type is that the correct_choice and selected_answer attributes need to work with a list of strings instead of a single string. Changing how those two things operate will potentially require adjusting how select and grade work.

    Additionally, the number of correct answers should be included in the printed output for a MultipleSelectQuestion. Here's an example with three correct answers:

    [QUESTION_TEXT] (select 3)
    1. [OPTION]
    2. [OPTION]
    3. [OPTION]
    4. [OPTION]
    5. [OPTION]

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans