Python Assessment


Q1. What is an abstract class?

Q2. What happens when you use the build-in function any() on a list?

Q3. What data structure does a binary tree degenerate to if it isn't balanced properly?

Q4. What is a static method?

Q5. What are attributes?

Q6. What is the term to describe this code?

count, fruit, price = (2, 'apple', 3.5)

Q7. What built-in list method would you use to remove items from a list?

Q8. What is one of the most common use of Python's sys library?

Q9. What is the runtime of accessing a value in a dictionary by using its key?

Q10. What is the correct syntax for defining a class called Game?

Q11. What is the correct way to write a doctest?

def sum(a, b):
    """
    sum(4, 3)
    7

    sum(-4, 5)
    1
    """
    return a + b
def sum(a, b):
    """
    >>> sum(4, 3)
    7

    >>> sum(-4, 5)
    1
    """
    return a + b
def sum(a, b):
    """
    # >>> sum(4, 3)
    # 7

    # >>> sum(-4, 5)
    # 1
    """
    return a + b
def sum(a, b):
    ###
    >>> sum(4, 3)
    7

    >>> sum(-4, 5)
    1
    ###
    return a + b

Q12. What buit-in Python data type is commonly used to represent a stack?

Q13. What would this expression return?

college_years = ['Freshman', 'Sophomore', 'Junior', 'Senior']
return list(enumerate(college_years, 2019)

Q14. How does defaultdict work?

Q15. What is the correct syntax for defining a class called "Game", if it inherits from a parent class called "LogicGame"?

Q16. What is the purpose of the "self" keyword when defining or calling instance methods?

Q17. Which of these is NOT a characteristic of namedtuples?

Q18. What is an instance method?

Q19. Which choice is the most syntactically correct example of the conditional branching?

num_people = 5

if num_people > 10;
    print("There is a lot of people in the pool.")
elif num_people > 4;
    print("There are some people in the pool.")
elif num_people > 0;
    print("There are a few people in the pool.")
else:
    print("There is no one in the pool.")
num_people = 5

if num_people > 10;
    print("There is a lot of people in the pool.")
if num_people > 4;
    print("There are some people in the pool.")
if num_people > 0;
    print("There are a few people in the pool.")
else:
    print("There is no one in the pool.")
num_people = 5

if num_people > 10:
    print("There is a lot of people in the pool.")
elif num_people > 4:
    print("There are some people in the pool.")
elif num_people > 0:
    print("There are a few people in the pool.")
else:
    print("There is no one in the pool.")
if num_people > 10;
    print("There is a lot of people in the pool.")
if num_people > 4;
    print("There are some people in the pool.")
if num_people > 0;
    print("There are a few people in the pool.")
else:
    print("There is no one in the pool.")

Q20. Which statement does NOT describe the object-oriented programming concenpt of encapsulation?

Q21. What is the purpose of an if/else statement?

Q22. What buit-in Python data type is commonly used to represent a queue?

Q23. What is the correct syntax for instantiating a new object of the type Game?

Q24. What does the built-in map() function do?

Q25. If you don't explicitly return a value from a function, what happens?

Q26. What is the purpose of the pass statement in Python?

Q27. What is the term used to describe items that may be passed into a function?

Q28. Which collection type is used to associate values with unique keys?

Q29. When does a for loop stop iterating?

Q30. Assuming the node is in a singly linked list, what is the runtime complexity of searching for a specific node within a singly linked list?

Q31. Given the following three list, how would you create a new list that matches the desired output printed below?

fruits = ['Apples', 'Oranges', 'Bananas']
quantities = [5, 3, 4]
prices = [1.50, 2.25, 0.89]

# Desired output
[('Apples', 5, 1.50),
('Oranges', 3, 2.25),
('Bananas', 4, 0.89)]
output = []

fruit_tuple_0 = (first[0], quantities[0], price[0])
output.append(fruit_tuple)

fruit_tuple_1 = (first[1], quantities[1], price[1])
output.append(fruit_tuple)

fruit_tuple_2 = (first[2], quantities[2], price[2])
output.append(fruit_tuple)

return output
i = 0
output = []
for fruit in fruits:
    temp_qty = quantities[i]
    temp_price = prices[i]
    output.append((fruit, temp_qty, temp_price))
    i += 1
return output
groceries = zip(fruits, quantities, prices)
return groceries

>>> [
('Apples', 5, 1.50),
('Oranges', 3, 2.25),
('Bananas', 4, 0.89)
]
i = 0
output = []
for fruit in fruits:
    for qty in quantities:
        for price in prices:
            output.append((fruit, qty, price))
    i += 1
return output

Q32. What happens when you use the built-in function all() on a list?

Q33. What is the correct syntax for calling an instance method on a class named Game?

>>> dice = Game()
>>> dice.roll()
>>> dice = Game(self)
>>> dice.roll(self)
>>> dice = Game()
>>> dice.roll(self)
>>> dice = Game(self)
>>> dice.roll()

Q34. What is the algorithmic paradigm of quick sort?

Q35. What is runtime complexity of the list's built-in .append() method?

Q36. What is key difference between a set and a list?

Q37. What is the definition of abstraction as applied to object-oriented Python?

Q38. 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])
a 1
a 2
a 3
b 1
b 2
b 3
c 1
c 2
c 3
['a', 'b', 'c'], [1, 2, 3]
aaa
bbb
ccc
111
222
333
a 1 2 3
b 1 2 3
c 1 2 3

Q39. What is the correct syntax for calling an instance method on a class named Game?

my_game = Game()
my_game.roll_dice()
my_game = Game()
self.my_game.roll_dice()
my_game = Game(self)
self.my_game.roll_dice()
my_game = Game(self)
my_game.roll_dice(self)

Q40. Correct representation of doctest for function in Python

def sum(a, b):
    # a = 1
    # b = 2
    # sum(a, b) = 3

    return a + b
def sum(a, b):
    """
    a = 1
    b = 2
    sum(a, b) = 3
    """

    return a + b
def sum(a, b):
    """
    >>> a = 1
    >>> b = 2
    >>> sum(a, b)
    3
    """

    return a + b
def sum(a, b):
    '''
    a = 1
    b = 2
    sum(a, b) = 3
    '''
    return a + b

Q41. 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?

Q42. What does calling namedtuple on a collection type return?

Q43. What symbol(s) do you use to assess equality between two elements?

Q44. Review the code below. What is the correct syntax for changing the price to 1.5?

fruit_info = {
'fruit': 'apple',
'count': 2,
'price': 3.5
}

Q45. What value would be returned by this check for equality?

5!=6

Q46. What does a class's init() method do?

Q47. What is meant by the phrase "space complexity"?

Q48. What is the correct syntax for creating a variable that is bound to a dictionary?

Q49. What is the proper way to write a list comprehension that represents all the keys in this dictionary?

fruits = {'Apples': 5, 'Oranges': 3, 'Bananas': 4}

Q50. What is the algorithmic paradigm of quick sort?

Q51. What is the purpose of the self keyword when defining or calling methods on an instance of an object?

Q52. What is a class method?

Q53. What does it mean for a function to have linear runtime?

Q54. What is the proper way to define a function?

Q55. According to the PEP 8 coding style guidelines, how should constant values be named in Python?

Q56. Describe the functionality of a deque.

Q57. What is the correct syntax for creating a variable that is bound to a set?

Q58. What is the correct syntax for defining an __init__() method that takes no parameters?

class __init__(self):
    pass
def __init__():
    pass
class __init__():
    pass
def __init__(self):
    pass

Q59. Which statement about the class methods is true?

Q60. Which of the following is TRUE About how numeric data would be organised in a banary Search tree?

Q61. Why would you use a decorator?

Q62. When would you use a for loop ?

Q63. What is the most self-descriptive way to define a function that calculates sales tax on a purches?

def tax(my_float): '''Calculates the sales tax of a purchase. Takes in a float representing the subtotal ...???