Select the true statements about sockets. (Select two answers)
Answer : A, D
1. A socket is a connection point that enables a two-way communication between programs running in a network.
2. A socket is always the secure means by which computers on a network can safely communicate, without the risk of exposure to an attack.
3. A socket is a connection point that enables a one-way communication only between remote processes.
4. A socket can be used to establish a communication endpoint for processes running on the same or different machines.
What is a static method?
Answer : C
A static method is a method that belongs to a class rather than an instance of the class. It is defined using the@staticmethoddecorator and does not take aselforclsparameter. Static methods are often used to define utility functions that do not depend on the state of an instance or the class itself.
Select the true statement about the___name___attribute.
Answer : D
The true statement about the__name__attribute isD.nameis a special attribute, which is inherent for classes, and it contains the name of a class. The__name__attribute is a special attribute of classes that contains the name of the class as a string.
The__name__attribute is a special attribute in Python that is available for all classes, and it contains the name of the class as a string. The__name__attribute can be accessed from both the class and its instances using the dot notation.
Which sentence about the property decorator is false?
Answer : A
The@propertydecorator should be defined after the method that is responsible for setting an encapsulated attribute is a false sentence. In fact, the@propertydecorator should be defined before the method that is used to set the attribute value. The@propertydecorator and the setter and deleter methods work together to create an encapsulated attribute, which is used to provide control over the attribute's value.
Official Python documentation on Property:https://docs.python.org/3/library/functions.html#property
The@propertydecorator is used to designate a method as a getter for an instance attribute. The method decorated with@propertyshould be defined before any setter or deleter methods for the same attribute.
Which function or operator should you use to obtain the answer True or False to the question: "Do two variables refer to the same object?"
Answer : D
To test whether two variables refer to the same object in memory, you should use theisoperator. Theisoperator returnsTrueif the two variables point to the same object in memory, andFalseotherwise.
For example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False
In this example,aandbrefer to the same list object in memory, soa is breturnsTrue. On the other hand,aandcrefer to two separate list objects with the same values, soa is creturnsFalse.
Theisoperator is used to test whether two variables refer to the same object in memory. If two variablesxandyrefer to the same object, the expressionx is ywill evaluate toTrue. Otherwise, it will evaluate toFalse.
Analyze the code and choose the best statement that describes it.
Answer : D
The correct answer isD. The code is responsible for the support of the inequality operator i.e. i != j. In the given code snippet, the__ne__method is a special method that overrides the behavior of the inequality operator!=for instances of theMyClassclass. When the inequality operator is used to compare two instances ofMyClass, the__ne__method is called to determine whether the two instances are unequal.
Analyze the following snippet and decide whether the code is correct and/or which method should be distinguished as a class method.
Answer : B
The correct answer isB. The getNumberofCrosswords() method should be decorated with @classmethod. In the given code snippet, thegetNumberofCrosswordsmethod is intended to be a class method that returns the value of thenumberofcrosswordsclass variable. However, the method is not decorated with the@classmethoddecorator and does not take aclsparameter representing the class itself. To makegetNumberofCrosswordsa proper class method, it should be decorated with@classmethodand take aclsparameter as its first argument.
1. ThegetNumberofCrosswords()method should be decorated with@classmethod.
This is because thegetNumberofCrosswords()method is intended to access the class-level variablenumberofcrosswords, but it is defined as an instance method, which requires an instance of the class to be created before it can be called. To make it work as a class-level method, you can define it as a class method by adding the@classmethoddecorator to the function.
Here's an example of how to definegetNumberofCrosswords()as a class method:
class Crossword:
numberofcrosswords = 0
def __init__(self, author, title):
self.author = author
self.title = title
Crossword.numberofcrosswords += 1
@classmethod
def getNumberofCrosswords(cls):
return cls.numberofcrosswords
In this example,getNumberofCrosswords()is defined as a class method using the@classmethoddecorator, and theclsparameter is used to access the class-level variablenumberofcrosswords.
Official Python documentation on Classes:https://docs.python.org/3/tutorial/classes.html