

InterviewSolution
Saved Bookmarks
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
101. |
What is the use of “a” in file handling?(a) Read(b) Write(c) Append(d) None of the mentioned |
Answer» Right choice is (c) Append The explanation is: This opens the fhe file in appending mode. That means, it will be open for writing and everything will be written to the end of the file. fh =open(“filename_here”, “a”). |
|
102. |
What is the use of truncate() method in file?(a) truncates the file size(b) deletes the content of the file(c) deletes the file size(d) none of the mentioned |
Answer» The correct answer is (a) truncates the file size The best explanation: The method truncate() truncates the file size. Following is the syntax for truncate() method: fileObject.truncate( [ size ]) Parameters size — If this optional argument is present, the file is truncated to (at most) that size. |
|
103. |
Given a string example=”hello” what is the output of example.count(‘l’)?(a) 2(b) 1(c) None(d) 0 |
Answer» Right answer is (a) 2 Explanation: l occurs twice in hello. |
|
104. |
What is the use of “w” in file handling?(a) Read(b) Write(c) Append(d) None of the mentioned |
Answer» Right choice is (b) Write The best I can explain: This opens the file for writing. It will create the file if it doesn’t exist, and if it does, it will overwrite it. fh = open(“filename_here”, “w”). |
|
105. |
Which of these definitions correctly describes a module?(a) Denoted by triple quotes for providing the specification of certain program elements(b) Design and implementation of specific functionality to be incorporated into a program(c) Defines the specification of how it is to be used(d) Any program that reuses code |
Answer» The correct answer is (b) Design and implementation of specific functionality to be incorporated into a program To explain: The term “module” refers to the implementation of specific functionality to be incorporated into a program. |
|
106. |
Which of the following is false about “from-import” form of import?(a) The syntax is: from modulename import identifier(b) This form of import prevents name clash(c) The namespace of imported module becomes part of importing module(d) The identifiers in module are accessed directly as: identifier |
Answer» The correct answer is (b) This form of import prevents name clash For explanation: In the “from-import” form of import, there may be name clashes because names of the imported identifiers aren’t specified along with the module name. |
|
107. |
Which of these is false about a package?(a) A package can have subfolders and modules(b) Each import package need not introduce a namespace(c) import folder.subfolder.mod1 imports packages(d) from folder.subfolder.mod1 import objects imports packages |
Answer» The correct option is (b) Each import package need not introduce a namespace To explain: Packages provide a way of structuring Python namespace. Each import package introduces a namespace. |
|
108. |
If a={5,6,7}, what happens when a.add(5) is executed?(a) a={5,5,6,7}(b) a={5,6,7}(c) Error as there is no add function for set data type(d) Error as 5 already exists in the set |
Answer» Correct option is (b) a={5,6,7} For explanation: There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify. |
|
109. |
Which of the following is false about “import modulename” form of import?(a) The namespace of imported module becomes part of importing module(b) This form of import prevents name clash(c) The namespace of imported module becomes available to importing module(d) The identifiers in module are accessed as: modulename.identifier |
Answer» Correct answer is (a) The namespace of imported module becomes part of importing module The explanation: In the “import modulename” form of import, the namespace of imported module becomes available to, but not part of, the importing module. |
|
110. |
Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?(a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5](b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5](c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5](d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5] |
Answer» Right option is (a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5] The best explanation: Execute in the shell to verify. |
|
111. |
Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?(a) [3, 4, 5, 20, 5, 25, 1, 3](b) [1, 3, 3, 4, 5, 5, 20, 25](c) [25, 20, 5, 5, 4, 3, 3, 1](d) [3, 1, 25, 5, 20, 5, 4, 3] |
Answer» The correct choice is (d) [3, 1, 25, 5, 20, 5, 4, 3] Explanation: Execute in the shell to verify. |
|
112. |
Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?(a) 0(b) 4(c) 1(d) 2 |
Answer» The correct option is (d) 2 The explanation is: Execute in the shell to verify. |
|
113. |
What is the biggest reason for the use of polymorphism?(a) It allows the programmer to think at a more abstract level(b) There is less program code to write(c) The program will have a more elegant design and will be easier to maintain and update(d) Program code takes up less space |
Answer» Right choice is (c) The program will have a more elegant design and will be easier to maintain and update To explain: Polymorphism allows for the implementation of elegant software. |
|
114. |
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] |
Answer» Right option is (c) [1, 3, 2, 1, 3, 2] To explain I would say: Execute in the shell and verify. |
|
115. |
What is the use of seek() method in files?(a) sets the file’s current position at the offset(b) sets the file’s previous position at the offset(c) sets the file’s current position within the file(d) none of the mentioned |
Answer» Correct choice is (a) sets the file’s current position at the offset Easiest explanation - Sets the file’s current position at the offset. The method seek() sets the file’s current position at the offset. Following is the syntax for seek() method: fileObject.seek(offset[, whence]) Parameters offset — This is the position of the read/write pointer within the file. whence — This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end. |
|
116. |
In file handling, what does this terms means “r, a”?(a) read, append(b) append, read(c) write, append(d) none of the mentioned |
Answer» The correct answer is (a) read, append The best I can explain: r- reading, a-appending. |
|
117. |
What is unpickling?(a) It is used for object serialization(b) It is used for object deserialization(c) None of the mentioned(d) All of the mentioned |
Answer» Correct choice is (b) It is used for object deserialization The best I can explain: We have been working with simple textual data. What if we are working with objects rather than simple text? For such situations, we can use the pickle module. This module serializes Python objects. The Python objects are converted into byte streams and written to text files. This process is called pickling. The inverse operation, reading from a file and reconstructing objects is called deserializing or unpickling. |
|
118. |
If a={5,6,7,8}, which of the following statements is false?(a) print(len(a))(b) print(min(a))(c) a.remove(5)(d) a[2]=45 |
Answer» The correct choice is (d) a[2]=45 Best explanation: The members of a set can be accessed by their index values since the elements of the set are unordered. |
|
119. |
Which of the following best describes polymorphism?(a) Ability of a class to derive members of another class as a part of its own definition(b) Means of bundling instance variables and methods in order to restrict access to certain class members(c) Focuses on variables and passing of variables to functions(d) Allows for objects of different types and behaviour to be treated as the same general type |
Answer» Right option is (d) Allows for objects of different types and behaviour to be treated as the same general type For explanation: Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified. |
|
120. |
To concatenate two strings to a third what statements are applicable?(a) s3 = s1 . s2(b) s3 = s1.add(s2)(c) s3 = s1.__add__(s2)(d) s3 = s1 * s2 |
Answer» The correct option is (c) s3 = s1.__add__(s2) The best I can explain: __add__ is another method that can be used for concatenation. |
|
121. |
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] |
Answer» Right choice is (c) [0.0, 0.5, 1.0, 1.5] Best explanation: Execute in the shell to verify. |
|
122. |
Suppose s is “ World”, what is s.strip()?(a) World(b) World(c) WORLD(d) World |
Answer» Right choice is (d) World The explanation: Execute help(string.strip) to find details. |
|
123. |
To read two characters from a file object infile, we use ____________(a) infile.read(2)(b) infile.read()(c) infile.readline()(d) infile.readlines() |
Answer» Right answer is (a) infile.read(2) Explanation: Execute in the shell to verify. |
|
124. |
What is the current syntax of remove() a file?(a) remove(file_name)(b) remove(new_file_name, current_file_name,)(c) remove(() , file_name))(d) none of the mentioned |
Answer» Correct option is (a) remove(file_name) The explanation: remove(file_name) |
|
125. |
Correct syntax of file.readlines() is?(a) fileObject.readlines( sizehint );(b) fileObject.readlines();(c) fileObject.readlines(sequence)(d) none of the mentioned |
Answer» The correct answer is (a) fileObject.readlines( sizehint ); The best I can explain: The method readlines() reads until EOF using readline() and returns a list containing the lines. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Syntax Following is the syntax for readlines() method: fileObject.readlines( sizehint ); Parameters sizehint — This is the number of bytes to be read from the file. |
|
126. |
Correct syntax of file.writelines() is?(a) file.writelines(sequence)(b) fileObject.writelines()(c) fileObject.writelines(sequence)(d) none of the mentioned |
Answer» Right answer is (c) fileObject.writelines(sequence) The best I can explain: The method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. Syntax Following is the syntax for writelines() method: fileObject.writelines( sequence ). |
|
127. |
What is the correct syntax of open() function?(a) file = open(file_name [, access_mode][, buffering])(b) file object = open(file_name [, access_mode][, buffering])(c) file object = open(file_name)(d) none of the mentioned |
Answer» Correct option is (b) file object = open(file_name [, access_mode][, buffering]) To explain I would say: Open() function correct syntax with the parameter details as shown below: file object = open(file_name [, access_mode][, buffering]) Here is parameters’ detail: file_name: The file_name argument is a string value that contains the name of the file that you want to access. access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r). buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior). |
|
128. |
Which of the following statements is used to create an empty set?(a) { }(b) set()(c) [ ](d) ( ) |
Answer» Correct option is (b) set() To explain: { } creates a dictionary not a set. Only set() creates an empty set. |
|
129. |
Which of the following statement prints helloexample est.txt?(a) print(“helloexample est.txt”)(b) print(“hello\example\test.txt”)(c) print(“hello”example”test.txt”)(d) print(“hello”example” est.txt”) |
Answer» Right option is (b) print(“hello\example\test.txt”) The best explanation: is used to indicate that the next is not an escape sequence. |
|
130. |
To add a new element to a list we use which command?(a) list1.add(5)(b) list1.append(5)(c) list1.addLast(5)(d) list1.addEnd(5) |
Answer» Right choice is (b) list1.append(5) For explanation: We use the function append to add an element to the list. |
|
131. |
Given a function that does not return any value, What value is thrown by default when executed in shell.(a) int(b) bool(c) void(d) None |
Answer» The correct option is (d) None The explanation is: Python shell throws a NoneType object back. |
|
132. |
What does built-in function help do in context of classes?(a) Determines the object name of any value(b) Determines the class identifiers of any value(c) Determines class description of any built-in type(d) Determines class description of any user-defined built-in type |
Answer» The correct choice is (c) Determines class description of any built-in type Easy explanation - help() usually gives information of the class on any built-in type or function. |
|
133. |
Which of the following is not a type of inheritance?(a) Double-level(b) Multi-level(c) Single-level(d) Multiple |
Answer» The correct answer is (a) Double-level The best I can explain: Multiple, multi-level, single-level and hierarchical inheritance are all types of inheritance. |
|
134. |
Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.(a) [x in range(1, 1000) if x%3==0](b) [x for x in range(1000) if x%3==0](c) [x%3 for x in range(1, 1000)](d) [x%3=0 for x in range(1, 1000)] |
Answer» Correct answer is (b) [x for x in range(1000) if x%3==0] The best I can explain: The list comprehension [x for x in range(1000) if x%3==0] produces a list of numbers between 1 and 1000 that are divisible by 3. |
|
135. |
Which of the following statements are true?(a) When you open a file for reading, if the file does not exist, an error occurs(b) When you open a file for writing, if the file does not exist, a new file is created(c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file(d) All of the mentioned |
Answer» Correct answer is (d) All of the mentioned The explanation is: The program will throw an error. |
|
136. |
Which of the following is not the correct syntax for creating a set?(a) set([[1,2],[3,4]])(b) set([1,2,2,3,4])(c) set((1,2,3,4))(d) {1,2,3,4} |
Answer» Correct choice is (a) set([[1,2],[3,4]]) For explanation: The argument given for the set must be an iterable. |
|
137. |
What does built-in function type do in context of classes?(a) Determines the object name of any value(b) Determines the class name of any value(c) Determines class description of any value(d) Determines the file name of any value |
Answer» Right answer is (b) Determines the class name of any value Easiest explanation - For example: >>> type((1,)) gives |
|
138. |
Which of these in not a core data type?(a) Lists(b) Dictionary(c) Tuples(d) Class |
Answer» Correct answer is (d) Class To explain I would say: Class is a user defined data type. |
|
139. |
Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].(a) [(2**x) for x in range(0, 13)](b) [(x**2) for x in range(1, 13)](c) [(2**x) for x in range(1, 13)](d) [(x**2) for x in range(0, 13)] |
Answer» Right answer is (a) [(2**x) for x in range(0, 13)] To explain: The required list comprehension will print the numbers from 1 to 12, each raised to 2. The required answer is thus, [(2**x) for x in range(0, 13)]. |
|
140. |
To open a file c:scores.txt for appending data, we use ____________(a) outfile = open(“c:\scores.txt”, “a”)(b) outfile = open(“c:\scores.txt”, “rw”)(c) outfile = open(file = “c:scores.txt”, “w”)(d) outfile = open(file = “c:\scores.txt”, “w”) |
Answer» Right answer is (a) outfile = open(“c:\scores.txt”, “a”) The explanation is: a is used to indicate that data is to be appended. |
|
141. |
What type of data is: a=[(1,1),(2,4),(3,9)]?(a) Array of tuples(b) List of tuples(c) Tuples of lists(d) Invalid type |
Answer» Correct option is (b) List of tuples Explanation: The variable a has tuples enclosed in a list making it a list of tuples. |
|
142. |
Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?(a) A.__init__(self)(b) B.__init__(self)(c) A.__init__(B)(d) B.__init__(A) |
Answer» The correct choice is (a) A.__init__(self) Easy explanation - To invoke the __init__ method in A from B, either of the following should be written: A.__init__(self) or super().__init__(self). |
|
143. |
Which of the following is a Python tuple?(a) [1, 2, 3](b) (1, 2, 3)(c) {1, 2, 3}(d) {} |
Answer» Correct option is (b) (1, 2, 3) Easy explanation - Tuples are represented with round brackets. |
|
144. |
What is the data type of (1)?(a) Tuple(b) Integer(c) List(d) Both tuple and integer |
Answer» Right choice is (b) Integer For explanation: A tuple of one element must be created as (1,). |
|
145. |
Which of the following is not a complex number?(a) k = 2 + 3j(b) k = complex(2, 3)(c) k = 2 + 3l(d) k = 2 + 3J |
Answer» Right option is (c) k = 2 + 3l The explanation is: l (or L) stands for long. |
|
146. |
Suppose t = (1, 2, 4, 3), which of the following is incorrect?(a) print(t[3])(b) t[3] = 45(c) print(max(t))(d) print(len(t)) |
Answer» Correct answer is (b) t[3] = 45 To explain: Values cannot be modified in the case of tuple, that is, tuple is immutable. |
|
147. |
What is the type of inf?(a) Boolean(b) Integer(c) Float(d) Complex |
Answer» The correct choice is (c) Float Best explanation: Infinity is a special case of floating point numbers. It can be obtained by float(‘inf’). |
|
148. |
Which of the following is incorrect?(a) float(‘inf’)(b) float(‘nan’)(c) float(’56’+’78’)(d) float(’12+34′) |
Answer» The correct choice is (d) float(’12+34′) Easy explanation - ‘+’ cannot be converted to a float. |
|
149. |
Which of the following is incorrect?(a) x = 0b101(b) x = 0x4f5(c) x = 19023(d) x = 03964 |
Answer» The correct option is (d) x = 03964 To explain I would say: Numbers starting with a 0 are octal numbers but 9 isn’t allowed in octal numbers. |
|
150. |
What is the order of namespaces in which Python looks for an identifier?(a) Python first searches the global namespace, then the local namespace and finally the built-in namespace(b) Python first searches the local namespace, then the global namespace and finally the built-in namespace(c) Python first searches the built-in namespace, then the global namespace and finally the local namespace(d) Python first searches the built-in namespace, then the local namespace and finally the global namespace |
Answer» Right option is (b) Python first searches the local namespace, then the global namespace and finally the built-in namespace Easiest explanation - Python first searches for the local, then the global and finally the built-in namespace. |
|