Python
Assessment
Q1.What is an abstract class?
class exists only so that other “concrete” classes can inherit from the abstract class.// CORRECT
Q2.What happens when you use the build – in function “any[]” on a list?
The “any[]” function returns True if any item in the list evaluates to True.Otherwise, it returns False. // CORRECT
Q4.What is a static method? Static methods are called static because they always return “None”. Static methods can be bound to either a class or an instance of a class.
They serve mostly as utility methods or helper methods, since they can’t access or modify a class’s state. // CORRECT
Q5.What are attributes?
class Function arguments are called “attributes” in the context of class methods and instance methods.// CORRECT
Q6.What is the term to describe this code?
count, fruit, price = [2, ‘apple’, 3.5]
tuple assignment // CORRECT
Q7.What built – in list method would you use to remove items from a list?
“.pop[]” method // CORRECT
Q8.What is one of the most common use of Python’s sys library?
To capture command-line arguments given at a file’s runtime //CORRECT
Q9.What is the runtime of accessing a value in a dictionary by using its key?
“O[1]”, also called constant time // CORRECT
Q10.What is the correct syntax for defining a class called Game?
class Game: pass // CORRECT
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]: //CORRECT
Q12.What buit – in Python data type is commonly used to represent a stack?
set list // CORRECT
Q13.What would this expression return?
college_years = [‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’]
return list[enumerate[college_years, 2019]
[[‘Freshman’, 2019], [‘Sophomore’, 2020], [‘Junior’, 2021], [‘Senior’, 2022]]
[[2019, 2020, 2021, 2022],
[‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’]]
[[‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’], [2019, 2020, 2021, 2022]]
[[2019, ‘Freshman’], [2020, ‘Sophomore’], [2021, ‘Junior’], [2022, ‘Senior’]] // CORRECT
Q14.How does “defaultdict” work?
“defaultdict” forces a dictionary to only accept keys that are of the types specified when you created the “defaultdict” [such as string or integers].// CORRECT
Q15.What is the correct syntax for defining a class called “Game”, if it inherits from a parent class called “LogicGame”?
class Game[LogicGame]: pass // CORRECT
Q16.What is the purpose of the “self” keyword when defining or calling instance methods?
self refers to the instance whose method was called // CORRECT
Q17.Which of these is NOT a characteristic of namedtuples?
No import is needed to use namedtuples because they are available in the standard library // CORRECT
Q18.What is an instance method?
Instance methods can modify the state of an instance or the state of its parent class //CORRECT
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.”]
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 // CORRECT
Q20.Which statement does NOT describe the object-oriented programming concept of encapsulation?
It only allows the data to be changed by methods // CORRECT
Q21.What is the purpose of an if / else statement?
An if / else statement executes one chunk of code if a condition it true, but a different chunk of code if the condition is false // CORRECT
Q22.What buit – in Python data type is commonly used to represent a queue?
list // CORRECT
Q23.What is the correct syntax for instantiating a new object of the type Game?
class[Game]
my_game = [Game] // CORRECT
Q24.What does the built-in map[] function do?
It applies a function to each item in an iterable and returns the value of that function. // CORRECT
Q25.If you don’t explicitly return a value from a function, what happens?
If the return keyword is absent, the function will return None. // CORRECT
Q26.What is the purpose of the pass statement in
Python?
It is a null operation used mainly as a placeholder in functions, classes, etc. // CORRECT
Q27.What is the term used to describe items that may be passed into a function?
arguments // CORRECT
Q28.Which collection type is used to associate values with unique keys?
dictionary // CORRECT
Q29.When does a for loop stop iterating?
when it encounters an if / else statement that contains a break keyword // CORRECT
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?
The runtime is O[n] because in the worst case, the node you are searching for is the last node, and every node in the linked list must be visited.// CORRECT
Q31.Given the following three lists, 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]]
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] // CORRECT
Q32.What happens when you use the built-in function all[] on a list?
The all[] function returns True if all items in the list evaluate to True. Otherwise, it returns False.
// CORRECT
Q33.What is the correct syntax for calling an instance method on a class named Game?
>> > dice.roll[] // CORRECT
Q34.Correct representation of doctest for function in Python
def sum[a, b]: //CORRECT
“””
>>> a = 1
>>> b = 2
>>> sum[a, b]
3
“””
return a + b