

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.
1. |
To return the length of string s what command do we execute?(a) s.__len__()(b) len(s)(c) size(s)(d) s.size() |
Answer» Right choice is (a) s.__len__() Best explanation: Execute in shell to verify. |
|
2. |
The function random.randint(4) can return only one of the following values. Which?(a) 4(b) 3.4(c) error(d) 5 |
Answer» The correct choice is (c) error To explain I would say: Error, the function takes two arguments. |
|
3. |
What does math.sqrt(X, Y) do?(a) calculate the Xth root of Y(b) calculate the Yth root of X(c) error(d) return a tuple with the square root of X and Y |
Answer» Right option is (c) error For explanation: The function takes only one argument. |
|
4. |
What is the range of values that random.random() can return?(a) [0.0, 1.0](b) (0.0, 1.0](c) (0.0, 1.0)(d) [0.0, 1.0) |
Answer» The correct answer is (d) [0.0, 1.0) To explain: Any number that is greater than or equal to 0.0 and lesser than 1.0 can be returned. |
|
5. |
What is the value of x if x = math.sqrt(4)?(a) 2(b) 2.0(c) (2, -2)(d) (2.0, -2.0) |
Answer» The correct option is (b) 2.0 Explanation: The function returns one floating point number. |
|
6. |
What is math.factorial(4.0)?(a) 24(b) 1(c) error(d) none of the mentioned |
Answer» The correct option is (a) 24 Explanation: The factorial of 4 is returned. |
|
7. |
What is math.floor(0o10)?(a) 8(b) 10(c) 0(d) 9 |
Answer» Right answer is (a) 8 Explanation: 0o10 is 8 and floor(8) is 8. |
|
8. |
What is the return value of trunc()?(a) int(b) bool(c) float(d) None |
Answer» Correct choice is (a) int For explanation: Execute help(math.trunc) to get details. |
|
9. |
What will be the output of print(math.factorial(4.5))?(a) 24(b) 120(c) error(d) 24.0 |
Answer» Right choice is (c) error The explanation is: Factorial is only defined for non-negative integers. |
|
10. |
What is the output of print(math.trunc(‘3.1’))?(a) 3(b) 3.0(c) error(d) none of the mentioned |
Answer» Right answer is (c) error For explanation: TypeError, a string does not have __trunc__ method. |
|
11. |
Is the output of the function abs() the same as that of the function math.fabs()?(a) sometimes(b) always(c) never(d) none of the mentioned |
Answer» Right option is (a) sometimes Easy explanation - math.fabs() always returns a float and does not work with complex numbers whereas the return type of abs() is determined by the type of value that is passed to it. |
|
12. |
What will be the output of print(math.copysign(3, -1))?(a) 1(b) 1.0(c) -3(d) -3.0 |
Answer» The correct option is (d) -3.0 The best explanation: The copysign function returns a float whose absolute value is that of the first argument and the sign is that of the second argument. |
|
13. |
What is the length of sys.argv?(a) number of arguments(b) number of arguments + 1(c) number of arguments – 1(d) none of the mentioned |
Answer» The correct answer is (b) number of arguments + 1 Explanation: The first argument is the name of the program itself. Therefore the length of sys.argv is one more than the number arguments. |
|
14. |
The pickle module defines ______ exceptions and exports _______ classes.(a) 2, 3(b) 3, 4(c) 3, 2(d) 4, 3 |
Answer» The correct option is (c) 3, 2 The best I can explain: The pickle module defines three exceptions, namely, pickle.PickleError, pickle.PicklingError, pickle.UnpickleError and exports two classes, namely, pickle.Pickler and pickle.Unpickler. |
|
15. |
Pick the correct statement regarding pickle and marshal modules.(a) The pickle module supports primarily .pyc files whereas marshal module is used to sterilize Python objects(b) The pickle module keeps track of the objects that have already been sterilized whereas the marshal module does not do this(c) The pickle module cannot be used to sterilize user defined classes and their instances whereas marshal module can be used to perform this task(d) The format of sterilization of the pickle module is not guaranteed to be supported across all versions of Python. The marshal module sterilization is compatible across all the versions of Python |
Answer» The correct choice is (b) The pickle module keeps track of the objects that have already been sterilized whereas the marshal module does not do this To explain I would say: The correct statement from among the above options is that the python module keeps track of the objects that have already been sterilized whereas the marshal module does not do this. The rest of the statements are incorrect. |
|
16. |
To remove string “hello” from list1, we use which command?(a) list1.remove(“hello”)(b) list1.remove(hello)(c) list1.removeAll(“hello”)(d) list1.removeOne(“hello”) |
Answer» The correct answer is (a) list1.remove(“hello”) The explanation is: Execute in the shell to verify. |
|
17. |
The module _______________ is a comparatively faster implementation of the pickle module.(a) cPickle(b) nPickle(c) gPickle(d) tPickle |
Answer» The correct option is (a) cPickle Easy explanation - The module cPickle is a comparatively faster implementation of the pickle module. |
|
18. |
To obtain a list of all the functions defined under sys module, which of the following functions can be used?(a) print(sys)(b) print(dir.sys)(c) print(dir[sys])(d) print(dir(sys)) |
Answer» Right option is (d) print(dir(sys)) Easy explanation - The function print(dir(sys)) helps us to obtain a list of all the functions defined under the sys module. The function can be used to obtain the list of functions under any given module in Python. |
|
19. |
Which of the following functions can be used to find the protocol version of the pickle module currently being used?(a) pickle.DEFAULT(b) pickle.CURRENT(c) pickle.CURRENT_PROTOCOL(d) pickle.DEFAULT_PROTOCOL |
Answer» Correct answer is (d) pickle.DEFAULT_PROTOCOL The explanation is: The function pickle.DEFAULT_PROTOCOL can be used to find the protocol version of the pickle module currently being used by the system. |
|
20. |
Which of the following aren’t defined in the math module?(a) log2()(b) log10()(c) logx()(d) none of the mentioned |
Answer» Correct choice is (c) logx() To explain I would say: log2() and log10() are defined in the math module. |
|
21. |
Which of the following functions is not defined under the sys module?(a) sys.platform(b) sys.path(c) sys.readline(d) sys.argv |
Answer» Correct answer is (c) sys.readline The explanation is: The functions sys.platform, sys.path and sys.argv are defined under the sys module. The function sys.readline is not defined. However, sys.stdin.readline is defined. |
|
22. |
Which of the following functions can help us to find the version of python that we are currently working on?(a) sys.version(b) sys.version()(c) sys.version(0)(d) sys.version(1) |
Answer» The correct answer is (a) sys.version Explanation: The function sys.version can help us to find the version of python that we are currently working on. For example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc along with the version. |
|
23. |
What does random.shuffle(x) do when x = [1, 2, 3]?(a) error(b) do nothing, it is a placeholder for a function that is yet to be implemented(c) shuffle the elements of the list in-place(d) none of the mentioned |
Answer» Correct choice is (c) shuffle the elements of the list in-place For explanation: The elements of the list passed to it are shuffled in-place. |
|
24. |
Which type of elements are accepted by random.shuffle()?(a) strings(b) lists(c) tuples(d) integers |
Answer» Right option is (b) lists For explanation: Strings and tuples are immutable and an integer has no len(). |
|
25. |
How do you close a file object (fp)?(a) close(fp)(b) fclose(fp)(c) fp.close()(d) fp.__close__() |
Answer» Right choice is (c) fp.close() The explanation is: close() is a method of the file object. |
|
26. |
Which of the following are the modes of both writing and reading in binary format in file?(a) wb+(b) w(c) wb(d) w+ |
Answer» The correct choice is (a) wb+ For explanation: Here is the description below “w” Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. “wb” Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. “w+” Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. “wb+” Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
|
27. |
Which of the following statements is false about recursion?(a) Every recursive function must have a base case(b) Infinite recursion can occur if the base case isn’t properly mentioned(c) A recursive function makes the code easier to understand(d) Every recursive function must have a return value |
Answer» Correct choice is (d) Every recursive function must have a return value Best explanation: A recursive function needn’t have a return value. |
|
28. |
What happens if the base condition isn’t defined in recursive programs?(a) Program gets into an infinite loop(b) Program runs once(c) Program runs n number of times where n is the argument given to the function(d) An exception is thrown |
Answer» Correct option is (a) Program gets into an infinite loop The explanation is: The program will run until the system gets out of memory. |
|
29. |
Is it possible to create a text file in python?(a) Yes(b) No(c) Machine dependent(d) All of the mentioned |
Answer» The correct choice is (a) Yes Best explanation: Yes we can create a file in python. Creation of file is as shown below. file = open(“newfile.txt”, “w”) file.write(“hello world in the new file”) file.write(“and another line”) file.close(). |
|
30. |
Which function is used to close a file in python?(a) Close()(b) Stop()(c) End()(d) Closefile() |
Answer» Right choice is (a) Close() Best explanation: f.close()to close it and free up any system resources taken up by the open file. |
|
31. |
What is the use of tell() method in python?(a) tells you the current position within the file(b) tells you the end position within the file(c) tells you the file is opened or not(d) none of the mentioned |
Answer» The correct choice is (a) tells you the current position within the file Easy explanation - The tell() method tells you the current position within the file; in other words, the next read or write will occur at that many bytes from the beginning of the file. |
|
32. |
Is Python case sensitive when dealing with identifiers?(a) yes(b) no(c) machine dependent(d) none of the mentioned |
Answer» The correct option is (a) yes For explanation: Case is always significant. |
|
33. |
What is Instantiation in terms of OOP terminology?(a) Deleting an instance of class(b) Modifying an instance of class(c) Copying an instance of class(d) Creating an instance of class |
Answer» Right choice is (d) Creating an instance of class Explanation: Instantiation refers to creating an object/instance for a class. |
|
34. |
Which of these is not a fundamental features of OOP?(a) Encapsulation(b) Inheritance(c) Instantiation(d) Polymorphism |
Answer» Correct choice is (c) Instantiation To explain: Instantiation simply refers to creation of an instance of class. It is not a fundamental feature of OOP. |
|
35. |
Which of the following is the most suitable definition for encapsulation?(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 implementation of elegant software that is well designed and easily modified |
Answer» The correct choice is (b) Means of bundling instance variables and methods in order to restrict access to certain class members To explain: The values assigned by the constructor to the class members is used to create the object. |
|
36. |
Bitwise _________ gives 1 if either of the bits is 1 and 0 when both of the bits are 1.(a) OR(b) AND(c) XOR(d) NOT |
Answer» The correct option is (c) XOR The explanation: Bitwise XOR gives 1 if either of the bits is 1 and 0 when both of the bits are 1. |
|
37. |
The formatting method {1: |
Answer» Correct answer is (b) second, left Explanation: The formatting method {1:<10} represents the second positional argument, left justified in a 10 character wide field. |
|
38. |
Which of the following represents the bitwise XOR operator?(a) &(b) ^(c) |(d) ! |
Answer» The correct option is (b) ^ To explain: The ^ operator represent bitwise XOR operation. &: bitwise AND, | : bitwise OR and ! represents bitwise NOT. |
|
39. |
_____ represents an entity in the real world with its identity and behaviour.(a) A method(b) An object(c) A class(d) An operator |
Answer» Right option is (b) An object Explanation: An object represents an entity in the real world that can be distinctly identified. A class may define an object. |
|
40. |
Which function overloads the // operator?(a) __div__()(b) __ceildiv__()(c) __floordiv__()(d) __truediv__() |
Answer» The correct choice is (c) __floordiv__() For explanation: __floordiv__() is for //. |
|
41. |
The function pow(x,y,z) is evaluated as:(a) (x**y)**z(b) (x**y) / z(c) (x**y) % z(d) (x**y)*z |
Answer» Correct option is (c) (x**y) % z To explain: The built-in function pow() can accept two or three arguments. When it takes in two arguments, they are evaluated as x**y. When it takes in three arguments, they are evaluated as (x**y)%z. |
|
42. |
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 |
Answer» Right option is (a) Left to Right The best I can explain: None. |
|
43. |
Which of the following is not a standard exception in Python?(a) NameError(b) IOError(c) AssignmentError(d) ValueError |
Answer» Correct option is (c) AssignmentError To explain: NameError, IOError and ValueError are standard exceptions in Python whereas Assignment error is not a standard exception in Python. |
|
44. |
Which of the following is the truncation division operator?(a) /(b) %(c) //(d) | |
Answer» Right option is (c) // To explain: // is the operator for truncation division. It is called so because it returns only the integer part of the quotient, truncating the decimal part. For example: 20//3 = 6. |
|
45. |
Which of the following expressions is an example of type conversion?(a) 4.0 + float(3)(b) 5.3 + 6.3(c) 5.0 + 3(d) 3 + 7 |
Answer» Correct answer is (a) 4.0 + float(3) For explanation: Type conversion is nothing but explicit conversion of operands to a specific type. Options 5.3 + 6.3 and 5.0 + 3 are examples of implicit conversion whereas option 4.0 + float(3) is an example of explicit conversion or type conversion. |
|
46. |
Which of the following expressions involves coercion when evaluated in Python?(a) 4.7 – 1.5(b) 7.9 * 6.3(c) 1.7 % 2(d) 3.4 + 4.6 |
Answer» The correct choice is (c) 1.7 % 2 To explain I would say: Coercion is the implicit (automatic) conversion of operands to a common type. Coercion is automatically performed on mixed-type expressions. The expression 1.7 % 2 is evaluated as 1.7 % 2.0 (that is, automatic conversion of int to float). |
|
47. |
Which module in Python supports regular expressions?(a) re(b) regex(c) pyregex(d) none of the mentioned |
Answer» Correct choice is (a) re The explanation is: re is a part of the standard library and can be imported using: import re. |
|
48. |
To find the decimal value of 1111, that is 15, we can use the function:(a) int(1111,10)(b) int(‘1111’,10)(c) int(1111,2)(d) int(‘1111’,2) |
Answer» The correct choice is (d) int(‘1111’,2) The best explanation: The expression int(‘1111’,2) gives the result 15. The expression int(‘1111’, 10) will give the result 1111. |
|
49. |
Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“susan”]?(a) Since “susan” is not a value in the set, Python raises a KeyError exception(b) It is executed fine and no exception is raised, and it returns None(c) Since “susan” is not a key in the set, Python raises a KeyError exception(d) Since “susan” is not a key in the set, Python raises a syntax error |
Answer» Correct choice is (c) Since “susan” is not a key in the set, Python raises a KeyError exception Easiest explanation - Execute in the shell to verify. |
|
50. |
The expression a{5} will match _____________ characters with the previous regular expression.(a) 5 or less(b) exactly 5(c) 5 or more(d) exactly 4 |
Answer» The correct choice is (b) exactly 5 The explanation: The character {m} is used to match exactly m characters to the previous regular expression. Hence the expression a{5} will match exactly 5 characters and not less than that. |
|