Python Quiz
zQ1. What is an abstract class?
Q2. What happens when you use the build-in function any()
on a list?
any()
on a list?example
Q3. What data structure does a binary tree degenerate to if it isn't balanced properly?
Q4. What statement about static methods is true?
Q5. What are attributes?
Explanation Attributes defined under the class, arguments goes under the functions. arguments usually refer as parameter, whereas attributes are the constructor of the class or an instance of a class.
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?
example
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, if it inherits from a parent class called LogicGame?
Explanation: The parent class which is inherited is passed as an argument to the child class. Therefore, here the first option is the right answer.
Q11. What is the correct way to write a doctest?
[✅] B
explanation - use ''' to start the doc and add output of the cell after >>>
Q12. What built-in Python data type is commonly used to represent a stack?
. You can only build a stack from scratch.
Q13. What would this expression return?
Q14. How does defaultdict
work?
defaultdict
work?Q15. What is the correct syntax for defining a class called "Game", if it inherits from a parent class called "LogicGame"?
repeated but labels will be different
Q16. What is the purpose of the "self" keyword when defining or calling instance methods?
Simple example
Q17. Which of these is NOT a characteristic of namedtuples?
**We need to import it using from collections import namedtuple
**
Q18. What is an instance method?
Q19. Which choice is the most syntactically correct example of the conditional branching?
[ ]
[ ]
[✅]
[ ]
Also see Question 85 for the same question with different answers.
Q20. Which statement does NOT describe the object-oriented programming concept of encapsulation?
Q21. What is the purpose of an if/else statement?
Q22. What built-in Python data type is best suited for implementing 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?
map()
function do?Explanation: - The synax for map()
function is list(map(function,iterable)
. the simple area finder using map would be like this
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?
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?
[ ]
[✅]
[ ]
[ ]
Q32. What happens when you use the built-in function all() on a list?
Explaination - all()
returns true if all in the list are True, see example below
Q33. What is the correct syntax for calling an instance method on a class named Game?
(Answer format may vary. Game and roll (or dice_roll) should each be called with no parameters.)
[✅]
[ ]
[ ]
[ ]
Q34. What is the algorithmic paradigm of quick sort?
Q35. What is runtime complexity of the list's built-in .append()
method?
.append()
method?Q36. What is key difference between a set
and a list
?
set
and a list
?Q37. What is the definition of abstraction as applied to object-oriented Python?
Q38. What does this function print?
[✅]
[ ]
[ ]
[ ]
Q39. Correct representation of doctest for function in Python
[ ]
[ ]
[✅]
[ ]
Explanation: Use """ to start and end the docstring and use >>> to represent the output. If you write this correctly you can also run the doctest using build-in doctest module
Q40. 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?
Q41. What does calling namedtuple on a collection type return?
Example
Q42. What symbol(s) do you use to assess equality between two elements?
Q43. Review the code below. What is the correct syntax for changing the price to 1.5?
Q44. What value would be returned by this check for equality?
5 != 6
Explanation - !=
is equivalent to not equal to in python
Q45. What does a class's init()
method do?
init()
method do?Example:
Q46. What is meant by the phrase "space complexity"?
Q47. What is the correct syntax for creating a variable that is bound to a dictionary?
Q48. 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}
Q49. What is the purpose of the self
keyword when defining or calling methods on an instance of an object?
self
keyword when defining or calling methods on an instance of an object?Explanation: - Try running the example of the Q45 without passing self
argument inside the __init__
, you'll understand the reason. You'll get the error like this __init__() takes 0 positional arguments but 1 was given
, this means that something is going inside even if haven't specified, which is instance itself.
Q50. What statement about the class methods is true?
Q51. What does it mean for a function to have linear runtime?
Q52. What is the proper way to define a function?
Q53. According to the PEP 8 coding style guidelines, how should constant values be named in Python?
Q54. Describe the functionality of a deque.
Explanation - deque
is used to create block chanin and in that there is first in first out approch, which means the last element to enter will be the first to leave.
Q55. What is the correct syntax for creating a variable that is bound to a set?
Q56. What is the correct syntax for defining an __init__()
method that takes no parameters?
__init__()
method that takes no parameters?[ ]
[ ]
[ ]
[✅]
Q57. Which of the following is TRUE About how numeric data would be organised in a binary Search tree?
Q58. Why would you use a decorator?
Q59. When would you use a for loop?
Q60. What is the most self-descriptive way to define a function that calculates sales tax on a purchase?
[ ]
[ ]
[ ]
[✅]
Q61. What would happen if you did not alter the state of the element that an algorithm is operating on recursively?
Q62. What is the runtime complexity of searching for an item in a binary search tree?
Q63. Why would you use mixin
?
mixin
?Q64. What is the runtime complexity of adding an item to a stack and removing an item from a stack?
Q65. Which statement accurately describes how items are added to and removed from a stack?
Explanation Stack uses the first in first out approach
Q66. What is a base case in a recursive function?
Q67. Why is it considered good practice to open a file from within a Python script by using the with
keyword?
with
keyword?Q68. Why would you use a virtual environment?
Q69. What is the correct way to run all the doctests in a given file from the command line?
Q70. What is a lambda function ?
Explanation: the lambda notation is basically an anonymous function that can take any number of arguments with only single expression (i.e, cannot be overloaded). It has been introducted in other programming languages, such as C++ and Java. The lambda notation allows programmers to "bypass" function declaration.
Q71. What is the primary difference between lists and tuples?
Q72. Which statement about static method is true?
Q73. What does a generator return?
Q74. What is the difference between class attributes and instance attributes?
Q75. What is the correct syntax of creating an instance method?
[ ]
[✅]
[ ]
[ ]
Q76. What is the correct way to call a function?
Q77. How is comment created?
Q78. What is the correct syntax for replacing the string apple in the list with the string orange?
Q79. What will happen if you use a while loop and forget to include logic that eventually causes the while loop to stop?
Q80. Describe the functionality of a queue?
Q81. Which choice is the most syntactically correct example of the conditional branching?
[✅]
[ ]
[ ]
[ ]
This question seems to be an updated version of Question 19.
Q82. How does defaultdict
work?
defaultdict
work?Updated version of Question 14.
Q83. What is the correct syntax for adding a key called variety
to the fruit_info
dictionary that has a value of Red Delicious
?
variety
to the fruit_info
dictionary that has a value of Red Delicious
?Q84. When would you use a while
loop?
while
loop?Simple Example
Q85. What is the correct syntax for defining an __init__()
method that sets instance-specific attributes upon creation of a new class instance?
__init__()
method that sets instance-specific attributes upon creation of a new class instance?[ ]
[ ]
[✅]
[ ]
Explanation: When instantiating a new object from a given class, the __init__()
method will take both attr1
and attr2
, and set its values to their corresponding object attribute, that's why the need of using self.attr1 = attr1
instead of attr1 = attr1
.
Q86. What would this recursive function print if it is called with no parameters?
[ ]
[ ]
[ ]
[✅]
Q87. In Python, when using sets, you use _ to calculate the intersection between two sets and _ to calculate the union.
Q88. What will this code fragment return?
Q89. You encounter a FileNotFoundException while using just the filename in the open
function. What might be the easiest solution?
open
function. What might be the easiest solution?Q90. what will this command return?
Q91. What does the // operator in Python 3 allow you to do?
Q92. This code provides the _ of the list of numbers
Q93. Which statement about the class methods is true?
Q94. What file is imported to use dates in python?
Q95. What is the correct syntax for defining a class called Game?
Q96. What does a class's init() method do?
Q97. What is the correct syntax for calling an instance method on a class named Game?
Q98. What is the output of this code? (NumPy has been imported as np.)?
Q99. Suppose you have a string variable defined as y=”stuff;thing;junk;”. What would be the output from this code?
explanation:
Q100. What is the output of this code?
explanation:
Q101. What is the correct syntax for creating an instance method?
Last updated