MCQs > IT & Programming > Python MCQs > Basic Python MCQs

Basic Python MCQ

1. Declare k as integer set k = ______ while names[k] != zzz write names[k] set k = k + 1 end while

Answer

Correct Answer: 0

Note: This Question is unanswered, help us to find answer for this one

2. Which statement about strings in Python is true?

Answer

Correct Answer: Strings can be enclosed by double quotes (

Note: This Question is unanswered, help us to find answer for this one

3. Which choice is not a native numerical type in Python?

Answer

Correct Answer: Double

Note: This Question is unanswered, help us to find answer for this one

4. Choose the option below for which instance of the class cannot be created

Answer

Correct Answer: Abstract Class

Note: This Question is unanswered, help us to find answer for this one

5. Which choice is an immutable data type?

Answer

Correct Answer: String

Note: This Question is unanswered, help us to find answer for this one

6. Suppose you have a variale named vector of type np.array with 10,000 elements. How can you turn vector into a variable named matrix with dimensions 100x100?

Answer

Correct Answer: Matrix = vector.reshape(100, 100)

Note: This Question is unanswered, help us to find answer for this one

7. What are the two main data structures in the Pandas library?

Answer

Correct Answer: Series and DataFrames

Note: This Question is unanswered, help us to find answer for this one

8. What is the correct syntax for creating a vaiable that is bound to a list?

Answer

Correct Answer: My_list = [2, 'apple', 3.5]

Note: This Question is unanswered, help us to find answer for this one

9. What two functions within the NumPy library could you use to solve a system of linear equations?

Answer

Correct Answer: Linalg.eig() and .matmul()

Note: This Question is unanswered, help us to find answer for this one

10. How do you add a comment to existing Python script?

Answer

Correct Answer: # This is a comment

Note: This Question is unanswered, help us to find answer for this one

11. What Python mechanism is best suited for telling a user they are using a deprecated function

Answer

Correct Answer: Warnings

Note: This Question is unanswered, help us to find answer for this one

12. In Python, how can the compiler identify the inner block of a for loop?

Answer

Correct Answer: Because of the level of indentation after the for loop

Note: This Question is unanswered, help us to find answer for this one

13. When would you use a try/except block in code?

Answer

Correct Answer: You use try/except blocks when you want to run some code, but need a way to execute different code if an exception is raised.

Note: This Question is unanswered, help us to find answer for this one

14. When an array is large, NumPy will not print the entire array when given the built-in print function. What function can you use within NumPy to force it to print the entire array?

Answer

Correct Answer: Set_printoptions

Note: This Question is unanswered, help us to find answer for this one

15. Which Python function allows you to execute Linux shell commands in Python?

Answer

Correct Answer: Os.system()

Note: This Question is unanswered, help us to find answer for this one

16. What built-in Python data type can be used as a hash table?

Answer

Correct Answer: Dictionary

Note: This Question is unanswered, help us to find answer for this one

17. NumPy allows you to multiply two arrays without a for loop. This is an example of _.

Answer

Correct Answer: Vectorization

Note: This Question is unanswered, help us to find answer for this one

18. Which syntax correctly creates a variable that is bound to a tuple?

Answer

Correct Answer: My_tuple = (2, 'apple', 3.5)

Note: This Question is unanswered, help us to find answer for this one

19. Elements surrounded by [] are _, {} are _, and () are _.

Answer

Correct Answer: Lists; dictionaries or sets; tuples

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: My_game = Game() my_game.roll_dice()

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Class Game: pass

Note: This Question is unanswered, help us to find answer for this one

22. What file is imported to use dates in python?

Answer

Correct Answer: Datetime

Note: This Question is unanswered, help us to find answer for this one

23. What does the // operator in Python 3 allow you to do?

Answer

Correct Answer: Perform integer division

Note: This Question is unanswered, help us to find answer for this one

24. You encounter a FileNotFoundException while using just the filename in the open function. What might be the easiest solution?

Answer

Correct Answer: Copy the file to the same directory as where the script is running from

Note: This Question is unanswered, help us to find answer for this one

25. When would you use a while loop?

Answer

Correct Answer: When you want some code to continue running as long as some condition is true

Note: This Question is unanswered, help us to find answer for this one

26. How does defaultdict work?

Answer

Correct Answer: If you try to read from a defaultdict with a nonexistent key, a new default key-value pair will be created for you instead of throwing a KeyError.

Note: This Question is unanswered, help us to find answer for this one

27. Describe the functionality of a queue?

Answer

Correct Answer: A queue adds items to either end and removes items from either end.

Note: This Question is unanswered, help us to find answer for this one

28. How is comment created?

Answer

Correct Answer: # This is a comment

Note: This Question is unanswered, help us to find answer for this one

29. What is the correct way to call a function?

Answer

Correct Answer: Get_max_num([57, 99, 31, 18])

Note: This Question is unanswered, help us to find answer for this one

30. What is the difference between class attributes and instance attributes?

Answer

Correct Answer: Class attributes are shared by all instances of the class. Instance attributes may be unique to just that instance

Note: This Question is unanswered, help us to find answer for this one

31. What does a generator return?

Answer

Correct Answer: An iterable object

Note: This Question is unanswered, help us to find answer for this one

32. What is the primary difference between lists and tuples?

Answer

Correct Answer: Lists are mutable, meaning you can change the data that is inside them at any time. Tuples are immutable, meaning you cannot change the data that is inside them once you have created the tuple.

Note: This Question is unanswered, help us to find answer for this one

33. What is a lambda function ?

Answer

Correct Answer: A small, anonymous function that can take any number of arguments but has only expression to evaluate

Note: This Question is unanswered, help us to find answer for this one

34. What is the correct way to run all the doctests in a given file from the command line?

Answer

Correct Answer: Python3 -m doctest <_filename_>

Note: This Question is unanswered, help us to find answer for this one

35. Why would you use a virtual environment?

Answer

Correct Answer: Virtual environments create a

Note: This Question is unanswered, help us to find answer for this one

36. Why is it considered good practice to open a file from within a Python script by using the with keyword?

Answer

Correct Answer: When you open a file using the with keyword in Python, Python will make sure the file gets closed, even if an exception or error is thrown.

Note: This Question is unanswered, help us to find answer for this one

37. What is a base case in a recursive function?

Answer

Correct Answer: A base case is the condition that allows the algorithm to stop recursing. It is usually a problem that is small enough to solve directly.

Note: This Question is unanswered, help us to find answer for this one

38. Which statement accurately describes how items are added to and removed from a stack?

Answer

Correct Answer: A stacks adds items to the top and removes items from the top.

Note: This Question is unanswered, help us to find answer for this one

39. What is the runtime complexity of adding an item to a stack and removing an item from a stack?

Answer

Correct Answer: Add items to a stack in O(1) time and remove items from a stack in O(1) time.

Note: This Question is unanswered, help us to find answer for this one

40. Why would you use mixin?

Answer

Correct Answer: If you have many classes that all need to have the same functionality, you'd use a mixin to define that functionality.

Note: This Question is unanswered, help us to find answer for this one

41. What is the runtime complexity of searching for an item in a binary search tree?

Answer

Correct Answer: The runtime for searching in a binary search tree is generally O(h), where h is the height of the tree.

Note: This Question is unanswered, help us to find answer for this one

42. What would happen if you did not alter the state of the element that an algorithm is operating on recursively?

Answer

Correct Answer: You would eventually get a KeyError when the recursive portion of the code ran out of items to recurse on.

Note: This Question is unanswered, help us to find answer for this one

43. When would you use a for loop?

Answer

Correct Answer: When you need to check every element in an iterable of known length.

Note: This Question is unanswered, help us to find answer for this one

44. Why would you use a decorator?

Answer

Correct Answer: You use the decorator to alter the functionality of a function without having to modify the functions code.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: For any given Node in a binary Search Tree, the child node to the left is less than the value of the given node and the child node to its right is greater than the given node.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: My_set = {0, 'apple', 3.5}

Note: This Question is unanswered, help us to find answer for this one

47. Describe the functionality of a deque.

Answer

Correct Answer: A deque adds items at either or both ends, and remove items at either or both ends.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: In all caps with underscores separating words -- e.g. MAX_VALUE = 255

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Def get_max_num(list_of_nums): # body of function goes here

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The amount of time it takes the function to complete grows linearly as the input size increases.

Note: This Question is unanswered, help us to find answer for this one

51. What statement about the class methods is true?

Answer

Correct Answer: A class method can modify the state of the class, but they can't directly modify the state of an instance that inherits from that class.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Self refers to the instance whose method was called.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The amount of space taken up in memory as a function of the input size

Note: This Question is unanswered, help us to find answer for this one

54. What value would be returned by this check for equality? 5 != 6

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: ==

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A tuple subclass with iterable named fields

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: An instance of the Game class will inherit whatever methods the BoardGame and LogicGame classes have.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Abstraction means the implementation is hidden from the user, and only the relevant data or information is shown.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A set is an unordered collection unique items. A list is an ordered collection of non-unique items.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: O(1), also called constant time

Note: This Question is unanswered, help us to find answer for this one

61. What is the algorithmic paradigm of quick sort?

Answer

Correct Answer: Divide and conquer

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The all() function returns True if all items in the list evaluate to True. Otherwise, it returns False.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: 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.

Note: This Question is unanswered, help us to find answer for this one

64. When does a for loop stop iterating?

Answer

Correct Answer: When it has assessed each item in the iterable it is working on or a break keyword is encountered

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Dictionary

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Arguments

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: It is a null operation used mainly as a placeholder in functions, classes, etc.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: If the return keyword is absent, the function will return None.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: It converts a complex value type into simpler value types.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: My_game = Game()

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: It executes one chunk of code if a condition is true, but a different chunk of code if the condition is false.

Note: This Question is unanswered, help us to find answer for this one

72. Which statement does NOT describe the object-oriented programming concept of encapsulation?

Answer

Correct Answer: It only allows the data to be changed by methods.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: No import is needed to use namedtuples because they are available in the standard library.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Self refers to the instance whose method was called.

Note: This Question is unanswered, help us to find answer for this one

75. What built-in Python data type is commonly used to represent a stack?

Answer

Correct Answer: List

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Class Game(LogicGame): pass

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: O(1), also called constant time.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: .pop() method

Note: This Question is unanswered, help us to find answer for this one

79. What are attributes?

Answer

Correct Answer: Attributes are a way to hold data or describe a state for a class or an instance of a class.

Note: This Question is unanswered, help us to find answer for this one

80. What statement about static methods is true?

Answer

Correct Answer: Static methods serve mostly as utility methods or helper methods, since they can't access or modify a class's state.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Linked list

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The any() function returns True if any item in the list evaluates to True. Otherwise, it returns False.

Note: This Question is unanswered, help us to find answer for this one

83. What is an abstract class?

Answer

Correct Answer: An abstract class exists only so that other

Note: This Question is unanswered, help us to find answer for this one

84. The starting value of an algorithm used to generate a range of numbers is called the _________.

Answer

Correct Answer: Seed

Note: This Question is unanswered, help us to find answer for this one

85. The class ____ is the base of the classes designed to handle exceptions.

Answer

Correct Answer: Exception

Note: This Question is unanswered, help us to find answer for this one

86. A variable's ________ is the part of a program in which the variable may be accessed.

Answer

Correct Answer: Local

Note: This Question is unanswered, help us to find answer for this one

87. You usually use the for loop with ____ loops.

Answer

Correct Answer: Definite

Note: This Question is unanswered, help us to find answer for this one

88. When a function is called by its name, then it is _____.

Answer

Correct Answer: Executed

Note: This Question is unanswered, help us to find answer for this one

89. The purpose of the __________ is to get the first input value for the validation of a loop.

Answer

Correct Answer: Priming read

Note: This Question is unanswered, help us to find answer for this one

90. The _____ of a local variable is the function in which the variable is created

Answer

Correct Answer: Scope

Note: This Question is unanswered, help us to find answer for this one

91. It is recommended that programmers avoid using ________ variables in a program whenever possible.

Answer

Correct Answer: Global

Note: This Question is unanswered, help us to find answer for this one

92. If the start index is ________ the end index, the slicing expression will return an empty string.

Answer

Correct Answer: Greater than

Note: This Question is unanswered, help us to find answer for this one

93. An example of an attribute of an object might be _______.

Answer

Correct Answer: ​a social security number

Note: This Question is unanswered, help us to find answer for this one

94. All objects within a(n) _____ share common attributes and methods.

Answer

Correct Answer: Class

Note: This Question is unanswered, help us to find answer for this one

95. A(n) ________ access file is also known as a direct access file.

Answer

Correct Answer: Random

Note: This Question is unanswered, help us to find answer for this one

96. A series of nested if statements can also be called a ____ if statement.

Answer

Correct Answer: Cascading

Note: This Question is unanswered, help us to find answer for this one

97. The ________ design technique can be used to break down an algorithm into functions.

Answer

Correct Answer: Top-down

Note: This Question is unanswered, help us to find answer for this one

98. A condition is a ________ expression

Answer

Correct Answer: Boolean

Note: This Question is unanswered, help us to find answer for this one

99. To append data to an existing file, you open it with the ____________ method.

Answer

Correct Answer: File.AppendText

Note: This Question is unanswered, help us to find answer for this one

100. The process of ____ is used to create an object from a class.

Answer

Correct Answer: Constructor

Note: This Question is unanswered, help us to find answer for this one

101. A loop controlled by the user is a type of ____ loop.

Answer

Correct Answer: Incrementing

Note: This Question is unanswered, help us to find answer for this one

102. The function header begins with the keyword ____ followed by the name of the function.

Answer

Correct Answer: Def

Note: This Question is unanswered, help us to find answer for this one

103. Python comes with ________ functions that have been already prewritten for the programmer.

Answer

Correct Answer: Standard

Note: This Question is unanswered, help us to find answer for this one

104. Which of the following statements about Python 3.4.2 is/are false?

Answer

Correct Answer: It is not an interpreted language.
It is not a case-sensitive language.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

105.

This question is based on the graphic shown below.



In the given image, which of the following line numbers in the Python code will result in a compilation error?

Answer

Correct Answer: Line number 15

Note: This Question is unanswered, help us to find answer for this one

106. Which of the following statements are correct?

Answer

Correct Answer: Lists in Python 3.4.2 are immutable.
Tuples in Python 3.4.2 are immutable

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

107.

In relation to the socket module in PythonoNetwork programming. which of the following options are the

correct parameters of the socket() function?


Answer

Correct Answer: E socket_type

Note: This Question is unanswered, help us to find answer for this one

108. Which of the following modules are used with the FTP protocol in Python?

Answer

Correct Answer: ftplib
urllib

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

109.

In relation to the socket() function in Python, which of the following constants represent the socket

types?


1. SOCK_DGRAM


2. SOCK_RDM


3. SOCK_RAW


4. SOCK_SEQPACKET


Answer

Correct Answer: All options 1. 2. 3. and 4.

Note: This Question is unanswered, help us to find answer for this one

110.

What will be the correct output of the following Python language code?

import threading

def p_c(num):

print("[}".format(num ‘ num ‘ num))

def p_s(num):

print("[}".format(num ’ num))

if _name_ = "_main_":

t1 = threading.Thread(target=p_s. args=l10.))

t2 = threading.Thread(target=p_c, args=(10,))

t1.start()

t2.start0

11.join0

t2.join()

prinl("1")


Answer

Correct Answer: 100 1000 1

Note: This Question is unanswered, help us to find answer for this one

111.

Choose the correct output of the Python 3.4 code that is given in the image?

Answer

Correct Answer: Two 0.0 -0x2a

Note: This Question is unanswered, help us to find answer for this one

112.

Find the output of the following Python code and choose the correct answer from the given options.

class Son:

def __init_(self. n):

self‌in = n

print(self.n)

def sayh(self):

print(self.n)

p = Son('0') p-sayho

Answer

Correct Answer: o 0

Note: This Question is unanswered, help us to find answer for this one

113.

Choose the correct output of the given Python code.

Answer

Correct Answer: [36.0, 40.0, 52.0, 72.0, 100.0,136.0,180.0, 232.0] 266.5

Note: This Question is unanswered, help us to find answer for this one

114.

What will be the correct output of the following Python code?

lis1 =[2. 1. 3, 5]

lisZ = [6, 4, 3]

lis1.extend[lis2)

print (end=")

for i in range(0, len(lis1)):

print(end=" ")

lisl.clear()

print (end=")

for i in range(2. len(lis1)):

print(lisl[i]+1. end=" ")


Answer

Correct Answer: 2 13 5 5 4 3
4 2 61012 8 6
3 2 4 5 7 5 4

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

115.

What will be the correct output of the following Python code?

class Ba(object):

def _init_(se|f, x):

self‌ix = x+2

class De(Ba):

def _init_(self. x. y):

Bax = x‘2

self.y = y+2

def printXY(self):

print(Ba.x+2, self.y‘2)

d = De(10’2, 20+2)

d.printXYO


Answer

Correct Answer: 42 48

Note: This Question is unanswered, help us to find answer for this one

116. Which of the following options is not a method of xmlparser objects?

Answer

Correct Answer: Error-Handler

Note: This Question is unanswered, help us to find answer for this one

117.

What will be the correct output of the following Python code?

c = {"A”:1, "B":2}

print(c.get("A"))

print(c.get("C"))

print(c.get("C"."0"))