1. What is an abstract class?
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods. It can only be inherited.
抽象类是一个特殊的类,它的特殊之处在于只能被继承,不能被实例化。如果说类是从一堆对象中抽取相同的内容而来的,那么抽象类就是从一堆类中抽取相同的内容而来的,内容包括数据属性和函数属性。
2. What happens when you use built-in function “any()” on a list?
any() 函数用于判断给定的可迭代参数 iterable 是否全部为 False。
3. What data structure does a binary tree degenerate to if it isn’t balanced properly?
A degenerate tree is a tree where for each parent node, there is only one associated child node.
4. What is a static method?
• A static method is also a method which is bound to the class and not the object of the class.
• A static method can’t access or modify class state.
• It is present in a class because it makes sense for the method to be present in class.
• Static methods in Python are extremely similar to python class level methods, the difference being that a static method is bound to a class rather than the objects for that class. This means that a static method can be called without an object for that class.
静态方法,通过类直接调用,不需要创建对象,不会隐式传递self,和类本身没有交互,即在静态方法中,不会涉及到类中的方法和属性的操作。
5. What are attributes?
(attributes are like properties, instances are like items)
Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility.
Unlike class attributes, instance attributes are not shared by objects. Every object has its own copy of the instance attribute (In case of class attributes all object refer to single copy).
6. What is the difference between class attributes and instance attributes?
Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility. Unlike class attributes, instance attributes are not shared by objects. Every object has its own copy of the instance attribute (In case of class attributes all object refer to single copy).
7. What is the term used to describe this code? Count, fruit, price = (2, ‘apple’, 3.5)
In packing, we place value into a new tuple while in unpacking we extract those values back into variables.
Python tuples are immutable means that they can not be modified in whole program. In Python there is a very powerful tuple assignment feature that assigns right hand side of values into left hand side. In other way it is called unpacking of a tuple of values into a variable. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable.
# Program to understand about
# packing and unpacking in Python
# this lines PACKS values (assignment)
# into variable a
a = ("MNNIT Allahabad", 5000, "Engineering")
# this lines UNPACKS values
# of variable a
(college, student, type_of college) = a
# print college name
print(college)
# print no of student
print(student)
# print type of college
print(type_ofcollege)
8. What built-in method would you use to remove items from a list?
listname.pop(index)
9. What is a runtime of accessing a value in a dictionary by using its key?
1. if key in dict:
It’s O(n)
if you use:
1. if dict.get(key):
It’s O(1)
10. What is the correct syntax of defining a class called Game?
11. What is a correct way to write a doctest?
12. What built-in python data type is commonly used to represent a stack?
14. How does “defaultdict” work?
A defaultdict works exactly like a normal dict, but it is initialized with a function (“default factory”) that takes no arguments and provides the default value for a nonexistent key. A defaultdict will never raise a KeyError. Any key that does not exist gets the value returned by the default factory.
15. What is the correct syntax for defining a class name “Game”, if it inherit from a parent class called “LogicGame”?
16. When would you use a “try/except” block?
The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program's response to any exceptions in the preceding try clause.
17. Which of there is NOT a characteristic of “namedtuples”?
Python supports a type of container like dictionaries called “namedtuples()” present in module, “collections“. Like dictionaries they contain keys that are hashed to a particular value. But on contrary, it supports both access from key value and iteration, the functionality that dictionaries lack.
Operations on namedtuple():
Access Operations
1. Access by index: The attribute values of namedtuple() are ordered and can be accessed using the index number unlike dictionaries which are not accessible by index.
2. Access by keyname: Access by keyname is also allowed as in dictionaries.
3. using getattr() :- This is yet another way to access the value by giving namedtuple and key value as its argument.
18. What is an instance method?
A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
19. What would happen if you did not alter the state of the element that an algorithm is operating on recursively?
Like the robots of Asimov, all recursive algorithms must obey three important laws:
1. A recursive algorithm must have a base case.
2. A recursive algorithm must change its state and move toward the base case.
3. A recursive algorithm must call itself, recursively.
20. Which statement does NOT describe the object-orented programming concept of encapsulation?
21. What is the runtime complexity of searching for an item in a binary search tree?
22. What is the algorithmic paradigm of merge sort?
23. Why would you use a decorator?
Decorators allow you to define reusable building blocks that can change or extend the behavior of other functions. And they let you do that without permanently modifying the wrapped function itself. The function's behavior changes only when it's decorated.
24. Why would you use a mixin?
Mixins are a sort of class that is used to "mix in" extra properties and methods into a class. This allows you to create classes in a compositional style.
A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:
1. You want to provide a lot of optional features for a class.
2. You want to use one particular feature in a lot of different classes.
Mixins encourage code reuse and can be used to avoid the inheritance ambiguity that multiple inheritance can cause (the "diamond problem"), or to work around lack of support for multiple inheritance in a language. A mixin can also be viewed as an interface with implemented methods.
25. What is the runtime complexity of adding an item to a stack and removing an item from a stack?
26. What is the purpose of self keyword when defining or calling instance methods?
self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes.
27. which choice is the most syntactically correct example of conditional branching?
28. What is the purpose of an if/else statement?
Python IF...ELIF...ELSE Statements. ... An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if.
29. Which of the following is true about how numeric data would be organized in a binary search tree?
30. Running time of the code?
def my_print(a_list, b_list):
for a in a_list:
for b in b_list:
print(a, b)
31. What is the definition of abstraction as applied to object-oriented Python?
32. What is a key difference between a set and a list?
33. What is the correct syntax for calling an instance method on a class named Game?
34. What is the runtime complexity of the list’s built-in .append() method?
35. What is the algorithmic paradigm of quick sort?
36. What is a class method?
37. What does it mean for a function to have a linear runtime?
38. What does the built-in map( ) function do?
39. What is the correct syntax for defining an _init_( ) method that takes no parameters?
A?
40. Which syntax correctly creates a variable that is bound to a tuple?
41. Suppose a Game class inherits from two parent classes: BoardGame and LogicGame. Which statement is true about the methods of an object instantiated from the Game class?
42. What does this function print?
def print _alpha_nums(abc_list, num_list):
for char in abc_list:
for num in num_list:
print(char, num)
return
print _alpha_nums([‘a’, ‘b’, ‘c’],[1, 2, 3])