Classes - Foundations of OOP

13 important questions on Classes - Foundations of OOP


If we assume that pythons, vipers, and cobras are subclasses of the same superclass, how would you call it?

Snakes

Write the very first line of the 'Python' class declaration, expressing the fact that the new class is actually a subclass of Snake.
Assume that there is a class named Snakes


class Python(Snakes):


Something is missing from the following declaration – what?

class Snakes:
    def __init__():
        self.sound = 'Sssssss'

The __init__() constructor lacks the obligatory parameter (we should name it self to stay compliant with the standards).



class Snakes:
    def __init__(self):
        self.sound = 'Sssssss'
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

Modify the code to guarantee that the venomous property is private.

class Snakes:
    def __init__(self):
        self.venomous = True


class Snakes:
    def __init__(self):
        self.__venomous = True

Modify the code to guarantee that the venomous property is private.

class Snakes:
    def __init__(self):
        self.venomous = True


class Snakes:
    def __init__(self):
        self.__venomous = True


class ExampleClass:
def __init__(self, val = 1):
self.first = val
def set_second(self, val):
self.second = val



example_object_3 = ExampleClass(4)
example_object_3.third = 5


Is it possible to assign 5 to example_object_3.third when it is not made inside the class?

Yes, it's possible to add attributes to an object dynamically.

Explain a class variable and mention a use

Class variables are shared among all instances of a class.
  • Useful for storing values common to all objects, like a counter how many classes have been created

Which of the Python class properties are instance variables and which are class variables? Which of them are private?

class Python:
  population = 1
  victims = 0
  def __init__(self):
    self.length_ft = 3
    self.__venomous = False

population and victims are class variables, while length and __venomous are instance variables (the latter is also private).

What do you call a function embedded in a class?

Method

Name two things a constructor (__init__(self): ) cannot do:

  • Return a value.
  • Be called explicitly like a regular method.

The declaration of the Snake class is given below. Enrich the class with a method named increment(), adding 1 to the victims property.

class Snake:
def __init__(self):
self.victims = 0

def increment(self):
  self.victims += 1


Redefine the Snake class constructor so that is has a parameter to initialize the victims field with a value passed to the object during construction.
class Snake: def __init__(self): self.victims = 0

Class snake:
    def __init__(self,vic)
        self.victims = vic

Class Snake:
    pass
class Python(Snake):
    pass
print(Python.__name__, 'is a', Snake.__name__)
print(Python.__bases__[0].__name__, 'can be a', Python.__name__)

Python is a Snake; Snake can be a Python.

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo