Skip to content

Latest commit

 

History

History
680 lines (592 loc) · 23 KB

1. POO.md

File metadata and controls

680 lines (592 loc) · 23 KB

1. Questions PCPP1 - POO

  1. 👉Which sentence describing an abstract class is true?

    1. it provides a means for API
    2. it is a contract between two classes
    3. it is a class decorated with the @abc.abstractclass decorator
    4. it is a class inheriting from the Abstractclass class
        🗝️ Ans. i
       
  2. 👉Take a look at the snippet and choose one of the following statements which is true:

    import abc
    
    
    @abc.abstractclass
    class BluePrint(abc.ABC):
    
        @abc.abstractmethod
        def hello(self):
            pass
    
    
    class WhitePool(BluePrint):
    
        def hello(self):
            print('Welcome to the White Pool!')
    
    
    wp = WhitePool()
    1. it is erroneous as there is a typo in the abstract method decorator.
    2. it defines one abstract class with one abstract method.
    3. it prints “Welcome to the White Pool”
    4. it is erroneous as there is no abc abstractclass decorator
        🗝️ Ans. iii
       
  3. 👉 Select the correct answer. The chaining concept introduces the following attribute on exception instances:

    1. __traceback__ , which is inherent only for implicitly chained exceptions
    2. __context__ , which is inherent for implicitly chained exceptions
    3. __traceback__ , which is inherent only for explicitly chained exceptions
    4. __cause__ , which is inherent for implicitly chained exceptions
        🗝️ Ans. ii
       
  4. 👉The following snippet is an example of what kind of exception chaining?

    Select the best answer:

    class OwnMath(Exception):
       pass
    
    
    def calculatevalue(numerator, denominator):
       try:
          value = numerator / denominator
    
       except ZeroDivisionError as e:
    
          raise OwnMath from e
    
       return value
    
    
    calculatevalue(4, 0)
    1. explicitly chained exceptions
    2. implicitly chained exceptions
    3. the code is erroneous.
    4. the code is fine, and the script execution is not interrupted by any exception.
        🗝️ Ans. i
       
  5. 👉 @property is used to decorate:

    1. a 'getter' type method
    2. any proxying method.
    3. a 'delete type method.
    4. a 'setter' type method
    5. an 'access controller' type method
        🗝️ Ans. ii
       
  6. 👉What should he the order of decorators, placed in your code, controlling access to one specific attribute?

    1. first is @property then @attribute.getter or @attribute.setter
    2. first is @property then @attribute.setter or @attribute.deleter
    3. first is @attribute.getter, then @property or @attribute.setter
    4. first is @attribute.setter, then @property or @attribute.deleter
        🗝️ Ans. ii
       
  7. 👉Look at the following code and name its elements.

    class A:
    
        def init(self):
            self.name = None
    
        def function(self, value):
            self.b = value
    
    
    a = A()
    1. A is a class, function is a method, a is a method.
    2. A is a class, function is an object, a is an object.
    3. A is an attribute, function is a method, a is a method.
    4. A is a method, b is a method, a is a method.
        🗝️ Ans. ii
       
  8. 👉What is the difference between inheritance and composition? Choose the best answer.

    1. both terms describe the same concept
    2. inheritance models an 'is a' relation whereas composition models a 'has a' relation.
    3. you cannot implement a multiple inheritance while multiple composition is possible.
    4. composition models an 'is a' relation whereas inheritance models a 'has a' relation.
        🗝️ Ans. i
       
  9. 👉The function id() returns:

    1. the identity of the object, a unique value amongst other objects
    2. the absolute memory address occupied by the object.
    3. a sequential number of the object created from the moment a Python script was started.
    4. the identity of the object representing the object type
        🗝️ Ans. ii
       
  10. 👉Select the true statement about class methods:

    1. class methods are methods that work on the class itself and require class object instances.
    2. class methods are methods that work on the class itself.
    3. class methods are methods that work on the class objects that are instantiated.
    4. class methods cannot change class variables' values as there is no reference for the class.
        🗝️ Ans. ii
       
  11. 👉What are class methods?

    1. class methods are classes that expect two parameters: one indicating the class object and other indicating the class itself.
    2. class methods are tool methods available to all class instances.
    3. class methods are classes that are bound to class instances.
    4. class methods are decorated with the @method trait.
        🗝️ Ans. ii
       
  12. 👉Select the true sentence.

    1. *args refers to a tuple of all not explicitly expected arguments; **kwargs refers to a dictionary of all not explicitly expected keyword arguments
    2. *args refers to a list of all not explicitly expected arguments; **kwargs refers to a dictionary of all not explicitly expected keyword arguments
    3. **args refers to a tuple of all not explicitly expected arguments; *kwargs refers to a dictionary of all not explicitly expected keyword arguments
    4. *args collects all matched positional arguments; **kwargs collects all matched keyword arguments
        🗝️ Ans. i
       
  13. 👉What is a decorator? Select the best answer.

    1. it can be only a function that wraps another function to perform additional steps.
    2. it is an only way to make your function safe by enforcing security checks for arguments values.
    3. it can be a function or class that only accelerates another function execution.
    4. it can be a function that returns a function that can be called later.
        🗝️ Ans. iii
       
  14. 👉Which function can be used to get a list of methods inherent to an object?

    1. __dict__()
    2. __help__()
    3. help()
    4. dict()
        🗝️ Ans. iii
       
  15. 👉What is the following special method responsible for?

    Instancecheck(self, object)

    1. it is responsible for handling the issubclass() function calls.
    2. it is responsible for handling the instancecheck() function calls
    3. it is responsible for handling the is instance() function calls.
    4. there is no such special method.
        🗝️ Ans. iii
       
  16. 👉What is inheritance?

    1. it is a concept of placing attributes inside objects and protecting then with dedicated methods.
    2. it is a concept of building new classes, based on superclasses. New subclasses extend or modify inherited traits
    3. it is another name for the 'has a' relation between two classes.
    4. it is a synonym for polymorphism.
        🗝️ Ans. ii
       
  17. 👉What is polymorphism?

    1. it is the possibility to abstract methods from specific types to treat those types in a dedicated way.
    2. it is a synonym for multiple inheritance.
    3. it is the provision of a single interface to objects of different types.
    4. it is a synonym for single inheritance.
        🗝️ Ans. iii
       
  18. 👉What is metaprogramming?

    1. it is a programming technique in which computer programs create metadata describing complex data structures like dictionaries.
    2. it is a polymorphic code that has the ability to decrypt itself in order to protect itself against antiviruses.
    3. it is a programming technique in which computer programs have the ability to modify their own code.
    4. it is a synonym for polymorphism.
        🗝️ Ans. iii
       
  19. 👉Which of the following cannot be "pickled"?

    1. function definitions
    2. large objects (LOB) whose size exceeds 64KB.
    3. objects having references to other objects.
    4. objects already pickled.
        🗝️ Ans. i
       
  20. 👉What is the dict property?

    1. it is a property that shows the content of every variable.
    2. it is a property that acts like a dictionary and combines all attributes available in your code.
    3. it is a built-in property owned by every Python keyword, object and instruction.
    4. it is a built-in special method that is responsible for supporting any operations expressed with the square brackets.
        🗝️ Ans. ii
       
  21. 👉In the following snippet, the flag parameter has been assigned a value equal to 'n' What can you say about this specific value? Select the best answer.

    import shelve
    
    my_shelve = shelve.open('first_shelve.shlv', flag='n')
    1. 'n' stands for 'neutral' - shelve will accept all data types for keys, but it will be slower in use.
    2. 'n' stands for 'new' - a new, empty database will be created.
    3. it is incorrect.
    4. 'n' stands for 'next' - new records will be appended to the existing ones.
        🗝️ Ans. iii
       
  22. 👉Look at the following snippet. What is the expected output?

    class OwnList(list):
    
        def __setitem__(self, index, value):
            list.append(self, value)
    
        def append(self, value):
            list.__setitem__(self, value)
    
    
    own_list = OwnList()
    own_list.append(3)
    own_list.append(2)
    
    print(own_list)
    1. [3, 3]
    2. [2, 3]
    3. [2, 2]
    4. the code is erroneous.
    5. [3, 2]
        🗝️ Ans. iii
       
  23. 👉What is duck typing?

    1. it is one of the pillars of OOP, which assumes that all classes provide a consistent interface.
    2. it is a built-in protection against calling unknown methods. All exceptions raised by duck-typing are handled in a standard way.
    3. it is a fancy term for the assumption that class objects own methods that are called.
    4. it is a concept of attribute encapsulation. It is similar to the concept of eggs protected by their shells.
        🗝️ Ans. iii
       
  24. 👉The reason for implementing abstract classes is that:

    1. abstract classes set requirements regarding methods that must be implemented by their subclasses.
    2. abstract classes decrease the code execution time.
    3. abstract classes help you keep your code clean, short, and self-documenting.
    4. abstract classes are deeper magic than 99% of users should ever worry about
        🗝️ Ans. iii
       
  25. 👉Select the best statement related to a decorator.

    1. it is an example of 'syntactic overload' that is reserved only to closures.
    2. it can be a function that decorates another function with characters carrying additional meaning such as '@' or '#' or '%'
    3. it can be a function or a class that gains access to arguments of the function being decorated.
    4. it can be a function or a class that optimizes another callable object execution by limiting its functionalities.
        🗝️ Ans. iii
       
  26. 👉What is serialization? Select the best answer.

    1. a process of assigning unique identifiers to every newly created Python object
    2. a process of creating Python objects based on byte sequences.
    3. a process of converting an object structure into a stream of bytes
    4. another name for the process of data transmission
        🗝️ Ans. iii
       
  27. 👉Which of the following statements related to Python objects is false?

    1. an object is an instance of a class.
    2. an object is a synonym for a class.
    3. every object has its type.
    4. an object is a synonym for an instance.
        🗝️ Ans. ii
       
  28. 👉Encapsulation allows for:

    1. controlling the access to selected attributes
    2. exposing the init method to consumers
    3. changing the type of encapsulated data; it works like a converting method.
    4. controlling the access to selected methods and attributes
        🗝️ Ans. i
       
  29. 👉Which statement about the 'shelve' module is false?

    1. the 'shelve' module is built on top of the 'pickle' module, but you do not have to abide by the order of all the elements placed into the shelve object.
    2. the 'shelve' module is built on top of the 'pickle' module, so you must abide by the order of all the elements placed into the shelve object.
    3. the 'shelve' module is used for object serialization and deserialization.
    4. the 'shelve' module does not require importing the 'pickle' module by the programmer.
        🗝️ Ans. ii
       
  30. 👉What is an instance variable?

    1. any kind of variable as everything in Python is an object.
    2. a variable that can be created only during object initialization.
    3. a kind of variable that exists inside an object.
    4. a variable that by default holds the None value as a result of variable initialization.
        🗝️ Ans. iii
       
  31. 👉Exception chaining is:

    1. a way to compact exception information.
    2. a decorative statement for better exception handling.
    3. a way to persist details of an exception when translating it to another type of exception.
    4. an exception which occurs during recursion that exhausts all system resources.
        🗝️ Ans. iii
       
  32. 👉Exception chain is:

    1. a term that describes how exception details are traveling the exception tree, upwards to the BaseException.
    2. a chain of trust between the subsequent exception handling methods
    3. a structure containing exception attributes: context and cause.
    4. a concept of handling exceptions raised by other exception handling code.
        🗝️ Ans. iii
       
  33. 👉What is the meaning of asterisks that are present in a function definition?

    1. they denote complex math operators.
    2. they denote parameters that should be unpacked before use.
    3. they denote pointers as in the C or C++ languages because CPython is written in C++.
    4. they denote parameters of constant type and length.
        🗝️ Ans. ii
       
  34. 👉When you access data stored in a shelve object, you must use the keys of specific type. What type is it?

    1. tuple
    2. integer
    3. string
    4. any mutable type
        🗝️ Ans. iii
       
  35. 👉Which statement is false?

    1. every list could be copied using slicing [:], which results in getting an independent list object.
    2. deep copy might cause problems when there are cyclic references.
    3. compound objects should be copied using the deep copy method.
    4. it takes more time to make a deep copy than a shallow copy of an object.
        🗝️ Ans. i
       
  36. 👉**“We're all consenting adults here” was said by:**

    1. Guido van Rossum, the author of Python
    2. John Marwood Cleese, a co-founder of 'Monty Python'
    3. Tim Peters, the author of 'Zen of Python'
    4. Uncle Bob, the developer of several software design principles
        🗝️ Ans. i
       
  37. 👉Answer the following question: What is the main reason for subclassing a built-in class?

    1. making the code execution faster
    2. avoiding any intelectual rights issues
    3. overriding all the parent's methods
    4. getting a new class that can enrich the parent's methods.
        🗝️ Ans. iii
       
  38. 👉What is a metaclass?

    1. It a description of a complex data structure such as dictionary
    2. a synonym for any abstract class which sets the guidelines regarding the methods required in subclasses.
    3. a term describing a file which contains many classes that are in the 'is-a' relation between each other.
    4. a class whose instances are classes.
        🗝️ Ans. iii
       
  39. 👉Is a 'pickle' file format constant amongst different Python releases?

    1. yes, that's how the power of Python manifests itself.
    2. it depends on the operating system used.
    3. no, nobody's expected wide compatibility.
    4. yes, as long as the CPU architecture (32 or 64 bits) is retained.
        🗝️ Ans. iii
       
  40. 👉Select the best answer to complete the following sentence:

    When constructing a subclass that overrides methods from its superclass:

    1. you are not allowed to override all methods.
    2. you still have access to the superclass methods.
    3. you no longer have access to any of the superclass methods.
    4. you should leave at least one method untouched for backwards compatibility.
        🗝️ Ans. ii
       
  41. 👉In the following snippet, the flag parameter has been assigned a value equal to 'c' What can you say about this specific value? Select the best answer.

    import shelve
    
    my_shelve = shelve.open('first_shelve.shlv', flag='c')
    1. 'c' stands for 'create' - a database will be created if it does not exist.
    2. 'c' stands for 'critical' - a limited number of data types will be used to increase the database performance.
    3. it is incorrect.
    4. 'c' stands for 'character' - only characters (strings) can be used for shelve keys.
        🗝️ Ans. i
       
  42. 👉Is it possible to instantiate an abstract class? Choose the correct answer.

    1. yes, every class can be instantiated.
    2. no, because this is not the role of abstract classes; those are blueprints that must be implemented by subclasses.
    3. no, because it has no method implementations at all.
    4. no, because it is part of a contract between the class designer and the developer.
        🗝️ Ans. ii
       
  43. 👉Select the best answer to complete the following sentence:

    **The *args and kwargs parameters:

    1. are responsible for supporting an arbitrary number of positional arguments and keyword.
    2. arguments, but their names cannot be changed.
    3. can be placed in any order in a function definition.
    4. are responsible for handling any number of additional arguments, placed right after the expected arguments, passed to a function called.
    5. are deprecated names of any function parameters, originating from Python 2.x.
        🗝️ Ans. iii
       
  44. 👉What is an abstract class? Select the best answer.

    1. it is another name for the 'has a' relation between two abstract methods
    2. it is a blueprint for other classes; it cannot contain fully defined methods
    3. it is a blueprint for other classes; it must contain at least one abstract method
    4. it is a class that overloads methods derived from its superclass
    5. it is a class decorated with the @abstract decorator
        🗝️ Ans. iii
       
  45. 👉Look at the following snippet. What is the expected output?

    class OwnDict(dict):
    
        def __setitem__(self, _key, _val):
            super().__setitem__(_key, _val)
    
        def update(self, *args, **kwargs):
            for _key, _val in dict(*args, **kwargs).items():
                self.__setitem__(_key, _val)
    
    
    own_dict = OwnDict()
    own_dict[4] = 1
    own_dict[2] = 0.5
    
    print(own_dict)
    1. the code is erroneous.
    2. {4: 4, 2: 2}
    3. {4: 1, 2: 0.5}
    4. {1: 4, 0.5: 2}
        🗝️ Ans. iii
       
  46. 👉What is an implicit exception chaining?

    1. it is a way to fade an exception by replacing it with another exception to limit the memory consumed by exception details.
    2. it is the only way to promote exception details upwards the call stack.
    3. it is a situation when an exception is raised during other exception handling, so the context attribute is filled with exception details.
    4. it is a situation when an exception is raised intentionally during other exception handling, so the cause attribute is filled with exception details.
        🗝️ Ans. iii
       
  47. 👉What is a class? Select the best answer.

    1. it is another name for the Python module file.
    2. it expresses an idea representing a real-life entity or problem.
    3. it is an independent instance of any variable such as string, float, or dictionary.
    4. it is a synonym for an attribute.
        🗝️ Ans. ii
       
  48. 👉Select the true statement about the 'self' named parameter.

    1. it is used as a reference to the class instance.
    2. it is an inherited object variable.
    3. it can be used before an instance is created when to access a class variable.
    4. this keyword is reserved for instance references and other names cannot be used for that.
        🗝️ Ans. i
       
  49. 👉What is inheritance? Choose the right answer.

    1. a concept that allows for modeling a tight relation between two subclasses.
    2. a concept of encapsulating inherited data to protect them against modifications.
    3. a concept that allows for modeling a tight relation between a superclass and subclasses.
    4. a concept that allows for building classes from uncoupled elements.
        🗝️ Ans. iii
       
  50. 👉What is a Python magic method?

    1. it is a special-purpose method that can accept any number of parameters when called.
    2. it is a special-purpose method that must be delivered by a developer, based on abstract blueprints.
    3. it is a method whose name starts and ends with a dunder.
    4. it is a method whose name starts and ends with a single underscore.
        🗝️ Ans. iii