Python CBT Questions and Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

PYTHON CBT QUESTIONS AND ANSWERS

1. Python is a ___object-oriented programming language. (a) Special purpose (b) General


purpose (c) Medium level programming language (d) All of the mentioned above

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

19. The following is used to define a ___.

d={

<key>: <value>,

<key>: <value>,

<key>: <value>

(a) Group (b) List (c)Array (d) Dictionary

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

22. The if statement is the most fundamental decision-making statement?

(a) True (b) False (c) Not sure (d) None of the above

23. Amongst which of the following if syntax is true?


(a) if condition:

#Will executes this block if the condition is true

(b) if condition

#Will executes this block if the condition is true

(c) if(condition)

#Will executes this block if the condition is true

(d) None of the mentioned above

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

29. What will be the output of the following Python code?

a=7

if a>4:

print("Greater")

(a) Greater (b) 7 (c) 4 (d) None of the above


30. What will be the output of the following Python code?

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

31. What will be the output of the following Python code?

x=13

if x>12 or x<15 and x==16:

print("Given condition matched")

else:

print("Given condition did not match")

(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?

a = int(input("Enter an integer: "))

b = int(input("Enter an integer: "))

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.

(a) Switch (b) if…else (c) if (d) if…elif…else

35. In Python, the break and continue statements, together are called ___ statement. (a) Jump
(b) goto (c) compound (d) None of the mentioned above

36. What will be the output of the following Python code?

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

37. The elif statement allows us to check multiple expressions.

(a) True (b) False (c) Not sure (d) None of the above

38. What will be the output of the following Python code?

i=5

if i>11 :

print ("i is greater than 11")

(a) No output (b) Abnormal termination of program (c) Both A and B (d) None of the mentioned
above

39. What will be the output of the following Python code?


a = 13

b = 15

print("A is greater") if a > b else print("=") if a == b else print("B is greater")

(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

47. What will be the output of the following Python code?

for i in range(6):

print(i)

(a) 0,1,2,3,4,5 (b) 0,1,2,3,4 (c) 0,1,2,3,4,5,6 (d) 1,2,3,4,5

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?

(a) def function_name(parameters):

...

Statements

...

(b) def function function_name:

...

Statements

...

(c) def function function_name(parameters):

...

Statements

...

(d) None of the mentioned above

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

55. What will be the output of the following Python code?

def show(id,name):

print("Your id is :",id,"and your name is :",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

83. Which is the correct operator for power(xy)?

(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

(a) i,ii,iii,iv,v,vi (b) ii,i,iii,iv,v,vi (c) ii,i,iv,iii,v,vi (d) i,ii,iii,iv,vi,v

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)

96. What will be the output of above Python code?

str1="6/4"

print("str1")

(a) 1 (b) 6/4 (c) 1.5 (d) str1

97. What will be the output of the following Python statement?

>>>"a"+"bc"

(a) a (b) bc (c) abc (d) bca

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

101. What is the output when following code is executed?

print r"\nhello"

The output is (a) Error (b) \nhello (c) a new line and hello (d) the letter r and then hello

102. Which of the following will result in an error?

str1="python"

(a) str1[1]="x" (b) print(str1[2]) (c) print(str1[0:9]) (d) Both (b) and (c)

103. What will be the output of the following Python statement?

>>>"abcd"[2:]

(a) a (b) ab (c) cd (d) dc

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

107. What will be the output of below Python code?

str1="Information"

print(str1[2:8])

(a) orma (b) ormat (c) format (d) formatio

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

110. What is the output of the following code

list1 = ['xyz', 'zara', 'PYnative']

print (max(list1))

(a) PYnative (b) zara (c) xyz (d) None

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

114. What is the output of the following

l = [None] * 10
print(len(l))

(a) 0 (b) 10 (c) Syntax Error (d) None

115. What will be the output of the following Python code?

>>>names = ['Amir', 'Bear', 'Charlton', 'Daman']

>>>print(names[-1][-1])

(a) A (b) n (c) Daman (d) Error

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]

117. What is the output of the code shown below?

t=32.00

[round((x-32)*5/9) for x in t]

(a) 0 (b) [0.00] (c) Error (d) [0]

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

121. Which of the following will give output as [23,2,9,75] ?

If list1=[6,23,3,2,0,9,8,75] (a) print(list1[1:7:2]) (b) print(list1[0:7:2]) (c) print(list1[1:8:2]) (d)


print(list1[0:8:2])

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)

123. What is the output of the following code

my_list = ["Hello", "Python"]

print("-".join(my_list))

(a) HelloPython- (b) Hello-Python (c)-HelloPython (d) None


124. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is: (a) [0, 1, 2, 3] (b) [0, 1, 2, 3, 4] (c) [0.0,
0.5, 1.0, 1.5] (d) [0.0, 0.5, 1.0, 1.5, 2.0]

125. What will be the output of the following Python code?

>>>list1 = [11, 2, 23]

>>>list2 = [11, 2, 2]

>>>list1 < list2

(a) True (b) False (c) Error (d) None

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

127. What is the output of the code snippet below :

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

135. Which object will be created first?

class student

int marks;

};

student s1, s2, s3;

(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?

(a) Objects are real-world entities, while classes are not.

(b) Classes are real-world entities, while objects are not.

(c) Both objects and classes are real-world entities.

(d) Both objects and classes are not real.

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

139. _____ is used to create an object.

A. class

B. constructor

C. In-built functions

D. user-defined functions

140. What will be the output of the following Python code?

class test:

def __init__(self, a=""Hello World""):

self.a=a

def display(self):

print(self.a)

obj=test()

obj.display()
A. Nothing is displayed.

B. “Hello World” 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.

141. Which of the following is false with respect to Python code?

class Student:

def __init__(self,id,age):

self.id=id

self.age=age

std=Student(1,20)

A. Every class must have a constructor.

B. ID and age are called the parameters.

C. "std" is the reference variable for object Student(1,20).

D. None of the above

View Answer

142. What is setattr() 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

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

144. Which of the following is correct?


class A:

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

146. Which of the following is not a type of class?

a) Abstract Class

b) Final Class

c) Start class

d) String Class

147. Class is passed by _______.

a) Value

b) Reference

c) Value or reference, depending on the program

d) Copy

148. Which of the following best describes polymorphism?

(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?

(a) Class-based language

(b) Procedure-oriented language

(c) Object-based language

(d) If classes are supported, polymorphism will always be supported.

150. Which of the following best describes inheritance?

A. focuses on variables and the passing of variables to functions.

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

151. Which of the following statements is wrong about inheritance?

A. The inheriting class is called a subclass.

B. Inheritance is one of the features of OOP.

C. Protected members of a class can be inherited.

D. Private members of a class can be inherited and accessed.

152. What does "single-level inheritance" mean?

A single subclass derives from a single superclass.

B. Multiple base classes inherit a single derived class

A single superclass inherits from multiple subclasses.

D. A subclass derives from a class, which in turn derives from another class.

153. Which of the following statements is not true?

A derived class is a subset of a superclass.


B. A non-private method in a superclass can be overridden.

C. The value of a private variable in the superclass can be changed in the subclass.

D. When invoking the constructor of a subclass, the constructor of the superclass is


automatically invoked.

154. Which of the following best describes polymorphism?

A. focuses on variables and the passing of variables to functions.

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

155. What is the biggest reason for the use of polymorphism?

A. There is less program code to write.

B. Program code takes up less space.

C. It allows the programmer to think at a more abstract level.

D. The program will have a more elegant design and will be easier to maintain and update.

156. Which of these is not a fundamental feature of OOP?

A. Inheritance

B. Instantiation

C. Encapsulation

D. Polymorphism

View Answer

157. Which of the following is the most suitable definition for encapsulation?

A. focuses on variables and the passing of variables to functions.

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__

D. user-defined functions/in-built functions

159. Which of the following statements is true?

A. The __init__ method is defined in the object class.

B. The __repr__() method is defined in the object class.

C. The __eq(other) method is defined in the object class.

D. The __new__() method automatically invokes the __init__ method.

160. Which of the following statements is true?

A. Overriding isn’t possible in Python.

B. A private method in a superclass can be overridden.

A subclass method can be overridden by the superclass.

D. A non-private method in a superclass can be overridden.

161. Which of the following is false about protected class members?

A. They begin with one underscore.

B. They can be accessed within a class.

C. They can be accessed by subclasses.

D. They can be accessed by the name-mangling method.


162. What will be the output of the following Python code?

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

C. Error because class B inherits A but variable x isn’t inherited

D. Error because when an object is created, an argument must be passed, like Derived_Test(1).

163. What will be the output of the following Python code?

class A():

def disp(self):

print("A disp()")

class B(A):

pass obj = B()

obj.disp()

A. A. disp()
B. Nothing is printed.

C. Invalid syntax for inheritance

D. Error because when an object is created, an argument must be passed.

164. What type of inheritance is illustrated in the following Python code?

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

165. What will be the output of the following Python code?

>>> class A:

pass

>>> class B (A):

pass

>>> obj=B()

>>> isinstance(obj,A)

A. True
B. False

C. Invalid method for classes

D. Wrong syntax for the isinstance() method

View Answer

166. What will be the output of the following Python code?

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

C. Error, invalid syntax for object declaration

D. Error, a private class member can’t be accessed in a subclass.

167. What will be the output of the following Python code?

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.

168. What will be the output of the following Python code?

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.

C. Error, invalid syntax for object declaration

D. Error, a private class member can’t be accessed in a subclass.

View Answer

169. What will be the output of the following Python code?

class A:

def __init__(self):

self.multiply(15)

def multiply(self, i):

self.i = 4 * i;

class B(A):

def __init__(self):

super().__init__()

print(self.i)

def multiply(self, i):

self.i = 2 * i;

obj = B()

A. 15

B. 30
C. 60

D. An exception is thrown.

170. What will be the output of the following Python code?

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

171. What will be the output of the following Python code?

class objects:

def __init__(self):

self.color = None
self._shape = "Circle"

def display(self, s):

self._shape = s

obj=objects()

print(obj._objects_shape)

A. Error because the member shape is a private member

B. Error because the member shape is a protected member

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.

172. Which of the following is not true for polymorphism?

a) It is a feature of OOP.

b) Ease of readability of the program

c) helps in redefining the same functionality.

d) Increases overhead of function definition always.

173. Which of the following is a type of constructors? (a) Default (b) Parameterized (c) Both a
and b (d) None of the above

174. Which of the following best describes encapsulation?

a) It is a way of combining various data members into a single unit.

b) It is a way of combining various member functions into a single unit.

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.

175. Which feature can be implemented using encapsulation?


a) Inheritance
b) Abstraction
c) Polymorphism
d) Overloading

176. Find which of the following uses encapsulation?


a) void main(){ int a; void fun( int a=10; cout<<a); fun(); }
b) class student{ int a; public: int b;};
c) class student{int a; public: void disp(){ cout<<a;} };
d) struct topper{ char name[10]; public : int marks; }

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

178. How can encapsulation be achieved?

a) Using Access Specifiers

b) using only private members

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?

a) Using an access declaration for private members of the base class

b) Using an access declaration for public members of the base class

c) Using an access declaration for the local variable of the main() function

d) Using access declarations for global variables


181. Which of the following can be a concept against encapsulation rules?

a) Using function pointers

b) Using a char* string pointer to be passed to a non-member function

c) Using an object array

d) Using any kind of pointer or array address in passing to another function.

182. Using encapsulation data security is ___________

a) Not ensured

b) Ensured to some extent

c) Purely ensured

d) Very low

183. Can one block of except statements handle multiple exceptions?

a) Yes, except TypeError, SyntaxError [,...]

b) yes, like except [TypeError, SyntaxError]

c) no

d) None of the mentioned

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

186. How many except statements can a try-except block have?

A. 0

B. 1

C. more than zero

D. more than one

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

193. Which function overloads the >> operator?

A. gt()

B. ge()

C. more()

D. None of the above

194. What is the value of the expression 100/25?

A. 0

B. 4

C. 4.0

D. 25

195. Operators with the same precedence are evaluated from

A. Left to Right

B. Right to Left
C. Depends on Compiler

D. None of the above

196. What is the output of print 0.1 + 0.2 = 0.3?

A. True

B. False

C. Machine-dependent

D. Error

197. What is the output of the following assignment operator?

y = 10

x = y += 2

print(x)

A. 10

B. 12

C. SynatxError

D. none

198. What is the output of the following code

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

200. What does 3^4 evaluate to?

A. 0.75

B. 7

C. 12

D. 81

Compiled by Tade of INS

You might also like