Python CBT Questions and Answers
Python CBT Questions and Answers
Python CBT Questions and Answers
2. Amongst the following, who is the developer of Python programming? (a) Guido van Rossum
(b) Denis Ritchie (c) Y.C. Khenderakar (d) None of the mentioned above
3. Amongst which of the following is / are the application areas of Python programming? (a)
Web Development (b) Game Development (c) Artificial Intelligence and Machine Learning (d)
All of the mentioned above
4. Amongst which of the following is / are the Numeric Types of Data Types? (a) int (b) float (c)
complex (d) All of the mentioned above
5. list, tuple, and range are the ___ of Data Types. (a) Sequence Types (b) Binary Types (c)
Boolean Types (d) None of the mentioned above
6. Float type of data type is represented by the float class. (a) True (b) False (c) Not sure (d)
None of the above
7. bytes, bytearray, memoryview are type of the ___ data type. (a) Mapping Type (b) Boolean
Type (c) Binary Types (d) None of the mentioned above
8. The type() function can be used to get the data type of any object. (a) True (b) False (c) Not
sure (d) None of the above
9. Binary data type is a fixed-width string of length bytes? (a) True (b) False (c) Not sure (d)
None of the above
10. Varbinary data type returns variable-width string up to a length of max-length bytes? (a)
True (b) False (c) Not sure (d) None of the above
11. Amongst which of the following is / are the logical operators in Python? (a) And (b) Not (c)
Or (d) All of the above
12. Is Python supports exception handling? (a) Yes (b) No (c) Not sure (d) None of the above
13. What is the name of the operator ** in Python? (a) Modulus (b) Floor division (c)
Exponentiation (d) None of the above
14. The % operator returns the ___. (a) Quotient (b) Divisor (c) Remainder (d) None of the
above
15. Amongst which of the following is / are the method of list? (a) Append() (b) Extend() (c)
Insert() (d) All of the above
16. The list.pop ([i]) removes the item at the given position in the list? (a) True (b) False (c) Not
sure (d) None of the above
17. The list.index(x[, start[, end]]) is used to ___. (a) Return zero-based index in the list (b)
Raises a ValueError if there is no such item (c) Both A and B (d) None of the above
18. Python Dictionary is used to store the data in a ___ format. (a) Key value pair (b) Group
value pair (c) Select value pair (d) None of the mentioned above
d={
<key>: <value>,
<key>: <value>,
<key>: <value>
20. Python Literals is used to define the data that is given in a variable or constant? (a) True (b)
False (c) Not sure (d) None of the above
21. Conditional statements are also known as ___ statements. (a) Decision-making (b) Array (c)
List (d) None of the above
(a) True (b) False (c) Not sure (d) None of the above
(b) if condition
(c) if(condition)
24. Amongst which of the following is / are the conditional statement in Python code? (a) if
a<=100: (b) if (a >= 10) (c) if (a => 200) (d) None of the mentioned above
25. Which of the following is not used as conditional statement in Python? (a) Switch (b) if…else
(c) if (d) if…elif…else
26. Which of the following is false regarding conditional statement in Python? (a) If-elif is the
shortcut for the if-else chain (b) We use the dictionary to replace the Switch case statement (c)
We cannot use python classes to implement the switch case statement (d) None of the
mentioned above
27. In Python, an else statement comes right after the block after 'if'? (a) True (b) False (c) Not
sure (d) None of the above
28. In a Python program, Nested if Statements denotes? (a) if statement inside another if
statement (b) if statement outside the another if statement (c) Both A and B (d) None of the
mentioned above
a=7
if a>4:
print("Greater")
x,y = 12,14
if(x+y==26):
print("True")
else:
print("False")
(a) True (b) False (c) Not sure (d) None of the above
x=13
else:
(a) Given condition matched (b) Given condition did not match (c) Both A and B (d) None of the
mentioned above
32. Consider the following code segment and identify what will be the output of given Python
code?
if a <= 0:
b = b +1
else:
a=a+1
(a) if inputted number is a negative integer then b = b +1 (b) if inputted number is a positive
integer then a = a +1 (c) Both A and B (d) None of the mentioned above
33. In Python, ___ defines a block of statements. (a) Block (b) Loop (c) Indentation (d) None of
the above
34. An ___ statement has less number of conditional checks than two successive ifs.
35. In Python, the break and continue statements, together are called ___ statement. (a) Jump
(b) goto (c) compound (d) None of the mentioned above
num = 10
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
(a) Positive number (b) Negative number (c) Real number (d)zero
(a) True (b) False (c) Not sure (d) None of the above
i=5
if i>11 :
(a) No output (b) Abnormal termination of program (c) Both A and B (d) None of the mentioned
above
b = 15
(a) A is greater (b) B is greater (c) Both A and B (d) None of the mentioned above
40. If a condition is true the not operator is used to reverse the logical state? (a) True (b) False
(c) Not sure (d) None of the above
41. Loops are known as ___ in programming. (a) Control flow statements (b) Conditional
statements (c) Data structure statements (d) None of the mentioned above
42. The for loop in Python is used to ___ over a sequence or other iterable objects. (a) Jump (b)
Iterate (c) Switch (d) All of the above
Jump
43. With the break statement we can stop the loop before it has looped through all the items?
(a) True (b) False (c) Not sure (d) None of the above
44. The continue keyword is used to ___ the current iteration in a loop. (a) Start (b) Initiate (c)
End (d) None of the above
45. Amongst which of the following is / are true about the while loop? (a) It continually
executes the statements as long as the given condition is true (b) It first checks the condition
and then jumps into the instructions (c) The loop stops running when the condition becomes
fail, and control will move to the next line of code. (d) All of the mentioned above
46. The ___ is a built-in function that returns a range object that consists series of integer
numbers, which we can iterate using a for loop. (a) Set() (b) Dictionary{} (c) range() (d) None of
the above
for i in range(6):
print(i)
48. The looping reduces the complexity of the problems to the ease of the problems?
(a) True (b) False (c) Not sure (d) None of the above
49. The while loop is intended to be used in situations where we do not know how many
iterations will be required in advance?
(a) True (b) False (c) Not sure (d) None of the above
50. Amongst which of the following is / are true with reference to loops in Python?
(a) It allows for code reusability to be achieved. (b) By utilizing loops, we avoid having to write
the same code over and over again. (c) We can traverse through the elements of data
structures by utilizing looping. (d) All of the above
51. A function is a group of related statements which designed specifically to perform a ___. (a)
Write code (b) Create executable file (c) Specific task (d) None of the mentioned above
52. Amongst which of the following is a proper syntax to create a function in Python?
...
Statements
...
...
Statements
...
...
Statements
...
53. Once we have defined a function, we can call it? (a) False (b) True (c) Not sure (d) None of
the above
54. Amongst which of the following shows the types of function calls in Python? (a) Call by
value (b) Call by reference (c) Both A and B (d) None of the above
def show(id,name):
show(12,"deepak")
(a) Your id is: 12 and your name is: Deepak (b) Your id is: 11 and your name is: Deepak (c) Your
id is: 13 and your name is: Deepak (d) None of the mentioned above
56. Amongst which of the following is a function which does not have any name? (a) Del
function (b) Show function (c) Lambda function (d) None of the mentioned above
57. Can we pass List as an argument in Python function? (a) Yes (b) No (c) Not sure (d) None of
the above
58. A method refers to a function which is part of a class? (a) False (b) True (c) Not sure (d)
None of the above
59. The return statement is used to exit a function? (a) True (b) False (c) Not sure (d) None of
the above
60. Scope and lifetime of a variable declared in a function exist till the function exists? (a) False
(b) True (c) Not sure (d) None of the above
61. File handling in Python refers the feature for reading data from the file and writing data into
a file?
(a) True (b) False (c) Not sure (d) None of the above
62. Amongst which of the following is / are the key functions used for file handling in Python?
(a) open() and close() (b) append() (c) read() and write() (d) All of the above
63. Amongst which of the following is / are needed to open an existing file? (a) filename (b)
mode (c) Both a and b (d) None
64. Binary files are stored in the form of 0s and 2s? (a) True (b) False (c) Not sure (d) None of
the above
65. The function file_object.close() is used to ___. (a) To open the existing file (b) To close an
opened file (c) To append in an opened file (d) None
66. Python always makes sure that any unwritten or unsaved data is written to the file before it
is closed? (a) False (b) True (c) Not sure (d) None of the above
67. The write() method takes a string as an argument and ___. (a) append in a text file (b) read
from the text file (c) writes it to the text file (d) None of the mentioned above
68. The seek() method is used to ___. (a) Saves the file in secondary storage (b) Deletes the file
form secondary storage (c) Position the file object at a particular position in a file (d) None
69. Amongst which of the following function is / are used to create a file and writing data? (a)
Append() (b) open() (c) close() (d) None
70. The readline() is used to read the data line by line from the text file.
(a) True (b) False (c) Not sure (d) None of the above
71. The module Pickle is used to ___. (a) Serializing Python object structure(b) De-serializing
Python object structure (c) Both a and b (d) None of the mentioned above
72. Amongst which of the following is / are the method of convert Python objects for writing
data in a binary file? (a) set() method (b) dump() method (c) load() method (d) None of the
above
73. Amongst which of the following is / are the method used to unpickling data from a binary
file? (a) set() method (b) dump() method (c) load() method (d) None of the above
74. A text file contains only textual information consisting of ___. (a) Alphabets (b) Numbers (c)
Special symbols (d) All of the mentioned above
75. The writelines() method is used to write a list of lines to the file? (a) False (b) True (c) Not
sure (d) None of the above
76. Is Python case sensitive when dealing with identifiers? (a) Yes (b) No (c) Machine dependent
(d) None of the mentioned
77. What is the maximum possible length of an identifier? (a) 31 characters (b) 63 characters (c)
79 characters (d) Any length
78. Which of the following is invalid? (a) _a = 1 (b) __a = 1 (c) _str_ = 1 (d) None of the
mentioned
79. Which of the following is an invalid variable? (a) my_string_1 (b) 1st_string (c) foo (d) _
80. Which of the following is true for variable names in Python? (a) unlimited length (b) all
private members must have leading and trailing underscores (c) underscore and ampersand are
the only two special characters allowed (d) none of the mentioned
81. Which of the following is an invalid statement? (a) abc = 1,000,000 (b) a b c = 1000 2000
3000 (c) a,b,c = 1000, 2000, 3000 (d) a_b_c = 1,000,000
82. Which of the following cannot be a variable? (a) _init_ (b) in (c) it (d) on
(a) x^y (b) x**y (c) x^^y (d) None of the mentioned
84. Which one of these is floor division? (a) / (b) // (c) % (d) None of the mentioned
85. What is the order of precedence in python? (i) Parentheses (ii) Exponential (iii)
Multiplication (iv) Division (v) Addition (vi) Subtraction
86. What is the answer to this expression, 22 % 3 is? (a) 7 (b) 1 (c) 0 (d) 5
87. Mathematical operations can be performed on a string. (a) True (b) False (c) Not sure (d)
None of the above
88. Operators with the same precedence are evaluated in which manner? (a) Left to Right (b)
Right to Left (c) Can’t say (d) None of the mentioned
89. What is the output of this expression, 3*1**3? (a) 27 (b) 9 (c) 3 (d) 1
90. Which one of the following has the same precedence level? (a) Addition and Subtraction (b)
Multiplication, Division, and Addition (c) Multiplication, Division, Addition, and Subtraction (d)
Addition and Multiplication
91. The expression Int(x) implies that the variable x is converted to an integer. (a) False (b) True
(c) Not sure (d) None of the above
92. Which one of the following has the highest precedence in the expression? (a) Exponential
(b) Addition (c) Multiplication (d) Parentheses
93. Which of these in not a core data type? (a) Lists (b) Dictionary (c) Tuples (d) Class
94. What will be the output of the following Python code? >>>str=""hello"" >>>str[:2] >>>
(a) he (b) lo (c) olleh (d) hello
95. Which of the following will run without errors? (a) round(45.8) (b) round(6352.898,2,5) (c)
round() (d) round(7463.123,2,1)
str1="6/4"
print("str1")
>>>"a"+"bc"
98. Python considered the character enclosed in triple quotes as String. (a) True (b) False (c) Not
sure (d) None of the above
99. What is the index value of ‘i’ in string “Learning” (a) 3 (b) 5 (c) 6 (d) 7
100. Index value in String should be of type __________. (a) integer (b) float (c) string (d)
boolean
print r"\nhello"
The output is (a) Error (b) \nhello (c) a new line and hello (d) the letter r and then hello
str1="python"
(a) str1[1]="x" (b) print(str1[2]) (c) print(str1[0:9]) (d) Both (b) and (c)
>>>"abcd"[2:]
104. The output of executing string.ascii_letters can also be achieved by: (a) string.letters (b)
string.ascii_lowercase_string.digits (c) string.lowercase_string.uppercase (d)
string.ascii_lowercase+string.ascii_uppercase
105. String traversal can be done using ‘for’ loop only. (a) True (b) False (c) Not sure (d) None of
the above
106. What arithmetic operators cannot be used with strings? (a) - (b) * (c) + (d) All of the
mentioned
str1="Information"
print(str1[2:8])
108. Which of the following would give an error? (a) list1=[] (b) list1=[]*3 (c) list1=[2,8,7] (d)
None of the above
109. Which of the following commands will create a list? (a) list1 = [] (b) list1 = list() (c) list1 =
list([1, 2, 3]) (d) All of the mentioned
print (max(list1))
111. Which of the following is True regarding lists in Python? (a) Lists are immutable. (b)
size(list1) command is used to find the size of lists. (c) Size of the lists must be specified before
its initialization (d) Elements of lists are stored in contagious memory location.
112. What is the output when we execute list(“hello”)? (a) [‘llo’] (b) [‘hello’] (c) [‘olleh’] (d) [‘h’,
‘e’, ‘l’, ‘l’, ‘o’]
113. Suppose listExample = [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)? (a) 4 (b) 5 (c) Error (d)
None
113. Suppose list1 is [2445,133,12454,123], what is max(list1)? (a) 133 (b) 123 (c) 2445 (d)
12454
l = [None] * 10
print(len(l))
>>>print(names[-1][-1])
116. Suppose list1 is [1, 3, 2], What is list1 * 2? (a) [2, 6, 4] (b) [1, 3, 2, 1, 3] (c) [1, 3, 2, 1, 3, 2]
(d) [1, 3, 2, 3, 2, 1]
t=32.00
[round((x-32)*5/9) for x in t]
118. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ? (a) 1 (b) 3 (c) 5 (d) 25
119. Suppose list1 is [1, 5, 9], what is sum(list1) ? (a) 1 (b) 9 (c) 15 (d) Error
120. To which of the following the “in” operator can be used to check if an item is in it? (a) Set
(b) Lists (c) Dictionary (d) All of the mentioned
122. To shuffle the list(say list1) what function do we use? (a) shuffle(list1) (b) list1.shuffle() (c)
random.shuffle(list1) (d) random.shuffleList(list1)
print("-".join(my_list))
>>>list2 = [11, 2, 2]
126. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing
operation? (a) print(list1[0]) (b) print(list1[:2]) (c) print(list1[:-2]) (d) all of the mentioned
1. class car:
2. def __init__(self,modelname, year):
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7.
8. c1 = car("Toyota", 2016)
9. c1.display()
(a) Error (b) Toyota,2016 (c) Toyota 2016 (d) None of the options
128. The___is a function that is associated with an object. (a) Inheritance (b) Method (c) Class
(d) None of the options
129. Polymorphism contains two words ___ and ___. (a) poly and morphs (b) poly and
morphins (c) Polytechnics and morphs (d) None of the options
130. Which of the following statements is correct regarding the object-oriented programming
concept in Python? (a) Classes are real-world entities while objects are not real (b) Objects are
real-world entities while classes are not real (c) Both objects and classes are real-world entities
(d) All of the above
131. What is the method inside the class in python language? (a) Object (b) Function (c)
Attribute (d) Argument
132. Which among the following is called first, automatically, whenever an object is created? (a)
Class (b) Constructor (c) New (d) Trigger
133. Which of the following is not a necessary condition for constructors? (a) Its name must be
the same as that of the class. (b) It must not have any return type. (c) It must contain a
definition body. (d) It can contain arguments.
134. How many types of constructors are available for general use (with respect to
parameters)? (a) 2 (b) 3 (c) 4 (d) 5
class student
int marks;
};
(a) s1 then s2 then s3 (b) s3 then s2 then s1 (c) s2 then s3 then s1 (d) all are created at same
time
136. Which of the following is correct with respect to the OOP concept in Python?
137. _____ represents an entity in the real world with its identity and behavior.
A. A class
B. An object
C. A. method
D. An operator
138. How many objects and reference variables are there for the given Python code?
class A:
print("Inside class")
A()
A()
obj=A()
A. 2 and 1
B. 3 and 3
C. 3 and 1
D. 3 and 2
A. class
B. constructor
C. In-built functions
D. user-defined functions
class test:
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
A. Nothing is displayed.
C. The program has an error display function that doesn’t have parameters.
D. The program has an error because the constructor can’t have default arguments.
class Student:
def __init__(self,id,age):
self.id=id
self.age=age
std=Student(1,20)
View Answer
A. To set an attribute
B. To delete an attribute
143. What is getattr() used for? (a) To set an attribute (b) To delete an attribute (c) To check if
an attribute exists or not (d) To access the attribute of the object
def __init__(self,name):
self.name=name
a1=A("john")
a2=A("john")
(a) id(a1) and id(a2) will have the same value. (b) id (a1) and id (a2) will have different values.
(c) Two objects with the same value of an attribute cannot be created. (d) None of the above
145. What is instantiation in terms of OOP terminology? (a) Creating an instance of class (b)
Copying an instance of class (c) Deleting an instance of class (d) Modifying an instance of class
a) Abstract Class
b) Final Class
c) Start class
d) String Class
a) Value
b) Reference
d) Copy
(a) It is the ability for a message or piece of data to be processed in more than one form.
(b) It is the ability for a message or piece of data to be processed in only one form.
(c) It is the ability for many messages or pieces of data to be processed in one way.
(d) It is the ability for undefined messages or data to be processed in at least one way.
149. What do you call the languages that support classes but not polymorphism?
B. Ability of a class to derive members of another class as a part of its own definition
C. Allows for the implementation of elegant software that is well designed and easily modified.
D. Means of bundling instance variables and methods in order to restrict access to certain class
members
D. A subclass derives from a class, which in turn derives from another class.
C. The value of a private variable in the superclass can be changed in the subclass.
B. Ability of a class to derive members of another class as a part of its own definition
C. Allows for objects of different types and behaviors to be treated as the same general type
D. Means of bundling instance variables and methods in order to restrict access to certain class
members
D. The program will have a more elegant design and will be easier to maintain and update.
A. Inheritance
B. Instantiation
C. Encapsulation
D. Polymorphism
View Answer
157. Which of the following is the most suitable definition for encapsulation?
B. Ability of a class to derive members of another class as a part of its own definition
allows for the implementation of elegant software that is well designed and easily modified.
D. Means of bundling instance variables and methods in order to restrict access to certain class
members
158. Methods of a class that provide access to private members of the class are called ______
and ______.
A. getters/setters
B. __repr__/__str__
C. __init__/__del__
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
A. 0 0
B. 0 1
D. Error because when an object is created, an argument must be passed, like Derived_Test(1).
class A():
def disp(self):
print("A disp()")
class B(A):
obj.disp()
A. A. disp()
B. Nothing is printed.
class A():
pass
class B(A):
pass
class C(B):
pass
A. Multiple inheritance
B. Single-level inheritance
C. Multi-level inheritance
D. Hierarchical inheritance
View Answer
>>> class A:
pass
pass
>>> obj=B()
>>> isinstance(obj,A)
A. True
B. False
View Answer
class A:
def __init__(self):
self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()
A. 0
B. 1
class Demo:
def __init__(self):
self.x = 1
def change(self):
self.x = 10
class Demo_derived(Demo):
def change(self):
self.x=self.x+1
return self.x
def main():
obj = Demo_derived()
print(obj.change())
main()
A. 1
B. 2
C. 11
D. An exception is thrown.
class A:
def __init__(self):
self._x = 5
class B(A):
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()
A. 5
B. Nothing is printed.
View Answer
class A:
def __init__(self):
self.multiply(15)
self.i = 4 * i;
class B(A):
def __init__(self):
super().__init__()
print(self.i)
self.i = 2 * i;
obj = B()
A. 15
B. 30
C. 60
D. An exception is thrown.
class A:
def one(self):
return self.two()
def two(self):
return 'A'
class B(A):
def two(self):
return 'B'
obj2=B()
print(obj2.two())
A. A
B. B
C. A B
D. An exception is thrown.
View Answer
class objects:
def __init__(self):
self.color = None
self._shape = "Circle"
self._shape = s
obj=objects()
print(obj._objects_shape)
C. Error because the proper syntax for name manipulation hasn’t been implemented.
D. The program runs fine because name manipulation has been properly implemented.
a) It is a feature of OOP.
173. Which of the following is a type of constructors? (a) Default (b) Parameterized (c) Both a
and b (d) None of the above
c) It is a way of combining various data members and member functions into a single unit that
can operate on any data.
d) It is a way of combining various data members and member functions that operate on those
data members into a single unit.
177. Which among the following should be encapsulated? (a) The data that is likely to change in
the near future (b) The data is prone to change in the long term. (c) The data that is intended to
be changed (d) The data that belongs to some other class
c) Using inheritance
d) Using Abstraction
179. Which of the following violates the principle of encapsulation almost always?
a) Local variables
b) Global variables
c) Public variables
d) Array variables
180. Which of the following would destroy the encapsulation mechanism if it were allowed in
programming?
c) Using an access declaration for the local variable of the main() function
a) Not ensured
c) Purely ensured
d) Very low
c) no
184. Which exception is raised when a calculation exceeds the maximum limit for a numeric
type?
A. StandardError
B. ArithmeticError
C. OverflowError
D. FloatingPointError
185. Which exception is raised in the case of failure of an attribute reference or assignment?
A. AttributeError
B. EOFError
C. ImportError
D. AssertionError
A. 0
B. 1
187. ___ is a python object that represents an error. (a) Interpreter (b) Exception (c) Compiler
(d) Module
188. ____ is a type of errors produce in python programs. (a) Runtime error (b) Name error (c)
Both a and b (d) None of these
189. ___ is raised by parser when syntax error is encountered. (a) Runtime error (b) Name error
(c) SyntaxError (d) None of these
190. What type of error will be shown after the following statement?
a=b
A. SyntaxError
B. TypeError
C. ValueError
D. NameError
191. What type of error will be shown after the following statement?
a = int('hello')
A. SyntaxError
B. TypeError
C. ValueError
D. NameError
192. What type of error will be shown after the following statement?
a = {7)
A. SyntaxError
B. TypeError
C. ValueError
D. NameError
A. gt()
B. ge()
C. more()
A. 0
B. 4
C. 4.0
D. 25
A. Left to Right
B. Right to Left
C. Depends on Compiler
A. True
B. False
C. Machine-dependent
D. Error
y = 10
x = y += 2
print(x)
A. 10
B. 12
C. SynatxError
D. none
x = 100
y = 50
print(x and y)
A. 50
B. 100
C. True
D. False
199. What does ~4 evaluate to?
A. -3
B. -4
C. -5
D. +3
A. 0.75
B. 7
C. 12
D. 81