Which option would be the best implementation of the __repr__ method?
__repr__
a. ObjName(arg1, arg2)
b. f"The Obj {self.name} contains {arg1} {arg2}"
c. 0x310788FF
a. ObjName(arg1, arg2)
You want to provide the object type when representing using repr, readability for sake of printing is not necessary.
What is Object Oriented Programming?
How would you print the current date and time in Python?
from datetime import datetime
print(datetime.now())
Calling the previously built methods with this function saves you from needing to rewrite methods in your subclass, and allows you to swap out superclasses with minimal code changes.
What is super()?
A testing framework that provides a rich set of tools for constructing and running tests. Originally inspired by JUnit, this module supports test automation, supports test fixture, test cases, test suites, and test runners.
What is unittest?
What will the following print?
i = '5'
int(i)
ord(i)
i = int(ord(i))
>>> print(i)
53,
the ord() function returns an integer representing the Unicode character.
Special methods that start and end with the double underscores. They are not meant to be invoked directly by you, but the invocation happens internally from the class on a certain action.
What are magic (dunder) methods?
How would you create an empty "dummy" class in Python?
class Class:
pass
Showing only essential attributes and hiding unnecessary information is?
Abstraction
Python code in one module gains access to the code in another module by the process of this important statement, usually at the top of your file. It combines two operations; searching for the named module, and then binding the results of that search to a name in the local scope.
What is an import statement?
Returns a list of valid attributes of an object.
What is the dir() method?
5 + 5 = 10
but why (explain) ?
The __add__ method behaves differently for different objects. For a string type, calling + concatenates the two. For an int type, calling + performs an addition operation. This is an example of Dynamic Method Dispatch, as python handles the different operations based on the object type.
This formula is used to calculate the middle of an array during a binary search
mid = (high + low) // 2
What is a static method?
The interpreter for Python was written in this language, which allows for dynamic memory allocation.
What is C?
Define anonymous functions, meant for one time use. Typically done in a single line for simple tasks for which creating an entire method is not necessary.
What are lambda functions?
This characteristic of OOP allows for the correct version of an overridden method to be called when an instance of a subclass is used to call it
What is polymorphism?
Write a function to return the max of two ints
max length: 1 line
max = lambda a, b : a if (a > b) else b
In this type of inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and grandfather.
What is multilevel inheritance?
List all bitwise operations
&& (AND)
| | (OR)
^ (XOR)
~ (NOT)
<< (Left Bit Shift)
>> (Right Bit Shift)
x = ["a", "b", "c"]
y = ["a", "b", "c"]
print(x is y and x ==y)
What will this output?
False
Although they are equivalent, they do not point to the same object in memory. So x == y will return True, but the "is" operator compares the memory addresses of the two objects, which will return False, so this returns False as a whole because of the "and" operation.
Determines the order in which Python looks for a method in a hierarchy of classes.
What is Method Resolution Order?
What are the properties of a BST?
Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a BST.
What is the base Object class here?
class F(O):pass
class E(O):pass
class D(O):pass
class C(D,F):pass
class B(D,E):pass
class A(B,C):pass
In this case, O is the base object class. The MRO will look something like this:
O
/ | \
D E F
|/ \ /
B C
\ /
A
What is the runtime complexity of a standard Binary Search?
a. O(NlogN)
b. Θ(logN)
c. O(N)
d. O(N^2)
A binary search, in the worst case, runs in O(logN) time, which is implied by big Theta Θ.