Explore topic-wise InterviewSolutions in .

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.

51.

Which of the following is invalid?(a) _a = 1(b) __a = 1(c) __str__ = 1(d) none of the mentioned

Answer» Correct choice is (d) none of the mentioned

Easiest explanation - All the statements will execute successfully but at the cost of reduced readability.
52.

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

Answer» The correct answer is (b) a b c = 1000 2000 3000

To explain I would say: Spaces are not allowed in variable names.
53.

Which of the following cannot be returned by random.randrange(4)?(a) 0(b) 3(c) 2.3(d) none of the mentioned

Answer» The correct answer is (c) 2.3

The best explanation: Only integers can be returned.
54.

Which of the following functions returns a dictionary mapping group names to group numbers?(a) re.compile.group(b) re.compile.groupindex(c) re.compile.index(d) re.compile.indexgroup

Answer» Correct answer is (b) re.compile.groupindex

Best explanation: The function re.compile.groupindex returns a dictionary mapping group names to group numbers.
55.

Which of the following is an invalid variable?(a) my_string_1(b) 1st_string(c) foo(d) _

Answer» The correct choice is (b) 1st_string

Best explanation: Variable names should not start with a number.
56.

How are required arguments specified in the function heading?(a) identifier followed by an equal to sign and the default value(b) identifier followed by the default value within backticks (“)(c) identifier followed by the default value within square brackets ([])(d) identifier

Answer» Correct answer is (d) identifier

Explanation: Refer documentation.
57.

Which of the following is not a keyword?(a) eval(b) assert(c) nonlocal(d) pass

Answer» Right answer is (a) eval

The best I can explain: eval can be used as a variable.
58.

Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?(a) __add__(), __str__()(b) __str__(), __add__()(c) __sum__(), __str__()(d) __str__(), __sum__()

Answer» Right option is (a) __add__(), __str__()

For explanation: The function __add__() is called first since it is within the bracket. The function __str__() is then called on the object that we received after adding A and B.
59.

The command used to set only the x coordinate of the turtle at 45 units is:(a) reset(45)(b) setx(45)(c) xset(45)(d) xreset(45)

Answer» The correct choice is (b) setx(45)

Explanation: The command setx(45) is used to set the x coordinate of the turtle. Similarly, the command sety() is used to set the y coordinate of the turtle. The function reset() takes two values as arguments, one for the x-coordinate and the other for the y-coordinate.
60.

What is the interval of the value generated by the function random.random(), assuming that the random module has already been imported?(a) (0,1)(b) (0,1](c) [0,1](d) [0,1)

Answer» Correct choice is (d) [0,1)

To explain: The function random.random() generates a random value in the interval [0,1), that is, including zero but excluding one.
61.

What is the pickling?(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» Right answer is (a) It is used for object serialization

The explanation: Pickle is the standard mechanism for object serialization. Pickle uses a simple stack-based virtual machine that records the instructions used to reconstruct the object. This makes pickle vulnerable to security risks by malformed or maliciously constructed data, that may cause the deserializer to import arbitrary modules and instantiate any object.
62.

What does the function re.search do?(a) matches a pattern at the start of the string(b) matches a pattern at any position in the string(c) such a function does not exist(d) none of the mentioned

Answer» Correct choice is (b) matches a pattern at any position in the string

For explanation: It will look for the pattern at any position in the string.
63.

Which of the following statements regarding the output of the function re.match is incorrect?(a) ‘pq*’ will match ‘pq’(b) ‘pq?’ matches ‘p’(c) ‘p{4}, q’ does not match ‘pppq’(d) ‘pq+’ matches ‘p’

Answer» Right answer is (d) ‘pq+’ matches ‘p’

Explanation: All of the above statements are correct except that ‘pq+’ match ‘p’. ‘pq+’ will match ‘p’ followed by any non-zero number of q’s, but it will not match ‘p’.
64.

What does the function re.match do?(a) matches a pattern at the start of the string(b) matches a pattern at any position in the string(c) such a function does not exist(d) none of the mentioned

Answer» The correct option is (a) matches a pattern at the start of the string

To explain I would say: It will look for the pattern at the beginning and return None if it isn’t found.
65.

Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?(a) d.size()(b) len(d)(c) size(d)(d) d.len()

Answer» The correct choice is (b) len(d)

The explanation: Execute in the shell to verify.
66.

Which of the codes shown below results in a match?(a) re.match(‘George(?=Washington)’, ‘George Washington’)(b) re.match(‘George(?=Washington)’, ‘George’)(c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)(d) re.match(‘George(?=Washington)’, ‘Georgewashington’)

Answer» Right option is (c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)

Best explanation: The code shown above demonstrates the use of the function re.match, along  with the special character ?=. This results in a match only when ‘George’ is immediately followed by ‘Washington’. Also, we have not used the module to ignore case. Hence the match is case-sensitive. Therefore the only option which results in a match is:

re.match(‘George(?=Washington)’, ‘GeorgeWashington’)
67.

Which of the following functions can be used to read data from a file using a file descriptor?(a) os.reader()(b) os.read()(c) os.quick_read()(d) os.scan()

Answer» The correct choice is (b) os.read()

For explanation: None of the other functions exist.
68.

What does print(Test.__name__) display (assuming Test is the name of the class)?(a) ()(b) Exception is thrown(c) Test(d) __main__

Answer» Right answer is (c) Test

Easy explanation - __name__ built-in class attribute is used to display the class name.
69.

What does print(os.geteuid()) print?(a) the group id of the current process(b) the user id of the current process(c) both the group id and the user of the current process(d) none of the mentioned

Answer» The correct option is (b) the user id of the current process

Explanation: os.geteuid() gives the user id while the os.getegid() gives the group id.
70.

Which of the following is not a declaration of the dictionary?(a) {1: ‘A’, 2: ‘B’}(b) dict([[1,”A”],[2,”B”]])(c) {1,”A”,2”B”}(d) { }

Answer» The correct choice is (c) {1,”A”,2”B”}

The explanation: Option c is a set, not a dictionary.
71.

Which of these about a frozenset is not true?(a) Mutable data type(b) Allows duplicate values(c) Data type with unordered values(d) Immutable data type

Answer» Right choice is (a) Mutable data type

Explanation: A frozenset is an immutable data type.
72.

Which of the statements about dictionary values if false?(a) More than one key can have the same value(b) The values of the dictionary can be accessed as dict[key](c) Values of a dictionary must be unique(d) Values of a dictionary can be a mixture of letters and numbers

Answer» The correct choice is (c) Values of a dictionary must be unique

The best I can explain: More than one key can have the same value.
73.

Which of the following is false about protected class members?(a) They begin with one underscore(b) They can be accessed by subclasses(c) They can be accessed by name mangling method(d) They can be accessed within a class

Answer» Correct answer is (c) They can be accessed by name mangling method

Explanation: Protected class members can’t be accessed by name mangling.
74.

If a is a dictionary with some key-value pairs, what does a.popitem() do?(a) Removes an arbitrary element(b) Removes all the key-value pairs(c) Removes the key-value pair for the key given as an argument(d) Invalid method for dictionary

Answer» Right option is (a) Removes an arbitrary element

Explanation: The method popitem() removes a random key-value pair.
75.

Which of these about a dictionary is false?(a) The values of a dictionary can be accessed using keys(b) The keys of a dictionary can be accessed using values(c) Dictionaries aren’t ordered(d) Dictionaries are mutable

Answer» Right choice is (b) The keys of a dictionary can be accessed using values

Best explanation: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.
76.

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

Answer» Right answer is (d) all of the mentioned

The explanation: Slicing is allowed in lists just as in the case of strings.
77.

Which of these is the definition for packages in Python?(a) A folder of python modules(b) A set of programs making use of Python modules(c) A set of main modules(d) A number of files containing Python definitions and statements

Answer» Right choice is (a) A folder of python modules

To explain: A folder of python programs is called as a package of modules.
78.

What is setattr() used for?(a) To access the attribute of the object(b) To set an attribute(c) To check if an attribute exists or not(d) To delete an attribute

Answer» The correct option is (b) To set an attribute

For explanation: setattr(obj,name,value) is used to set an attribute. If attribute doesn’t exist, then it would be created.
79.

What is getattr() used for?(a) To access the attribute of the object(b) To delete an attribute(c) To check if an attribute exists or not(d) To set an attribute

Answer» The correct choice is (a) To access the attribute of the object

Easy explanation - getattr(obj,name) is used to get the attribute of an object.
80.

Is Python code compiled or interpreted?(a) Python code is only compiled(b) Python code is both compiled and interpreted(c) Python code is only interpreted(d) Python code is neither compiled nor interpreted

Answer» Correct answer is (b) Python code is both compiled and interpreted

Easiest explanation - Many languages have been implemented using both compilers and interpreters, including C, Pascal, and Python.
81.

How many except statements can a try-except block have?(a) zero(b) one(c) more than one(d) more than zero

Answer» Right choice is (d) more than zero

To explain: There has to be at least one except statement.
82.

When is the finally block executed?(a) when there is no exception(b) when there is an exception(c) only if some condition that has been specified is satisfied(d) always

Answer» The correct answer is (d) always

For explanation: The finally block is always executed.
83.

When will the else part of try-except-else be executed?(a) always(b) when an exception occurs(c) when no exception occurs(d) when an exception occurs in to except block

Answer» Right answer is (c) when no exception occurs

Best explanation: The else part is executed when no exception occurs.
84.

Which of the following is true about top-down design process?(a) The details of a program design are addressed before the overall design(b) Only the details of the program are addressed(c) The overall design of the program is addressed before the details(d) Only the design of the program is addressed

Answer» Correct answer is (c) The overall design of the program is addressed before the details

For explanation: Top-down design is an approach for deriving a modular design in which the overall design.
85.

Suppose list1 is [1, 5, 9], what is sum(list1)?(a) 1(b) 9(c) 15(d) Error

Answer» Right answer is (c) 15

The explanation: Sum returns the sum of all elements in the list.
86.

______ is a string literal denoted by triple quotes for providing the specifications of certain program elements.(a) Interface(b) Modularity(c) Client(d) Docstring

Answer» Correct option is (d) Docstring

The explanation is: Docstring used for providing the specifications of program elements.
87.

Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?(a) Error(b) None(c) 25(d) 2

Answer» Correct answer is (c) 25

To explain I would say: -1 corresponds to the last index in the list.
88.

Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?(a) 3(b) 5(c) 25(d) 1

Answer» The correct choice is (d) 1

Easy explanation - Min returns the minimum element in the list.
89.

Which function is used to write all the characters?(a) write()(b) writecharacters()(c) writeall()(d) writechar()

Answer» Right option is (a) write()

The explanation: To write a fixed sequence of characters to a file

fh = open(“hello.txt”,”w”)

write(“Hello World”).
90.

What will be displayed by print(ord(‘b’) – ord(‘a’))?(a) 0(b) 1(c) -1(d) 2

Answer» Correct option is (b) 1

Easy explanation - ASCII value of b is one more than a. Hence the output of this code is 98-97, which is equal to 1.
91.

Which function is used to read single line from file?(a) Readline()(b) Readlines()(c) Readstatement()(d) Readfullline()

Answer» The correct choice is (b) Readlines()

To explain: The readline function reads a single line from the file fh = open(“filename”, “r”)

content = fh.readline().
92.

Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?(a) [2, 33, 222, 14](b) Error(c) 25(d) [25, 14, 222, 33, 2]

Answer» Right option is (a) [2, 33, 222, 14]

The explanation is: Execute in the shell to verify.
93.

Program code making use of a given module is called a ______ of the module.(a) Client(b) Docstring(c) Interface(d) Modularity

Answer» Correct answer is (a) Client

The explanation is: Program code making use of a given module is called the client of the module. There may be multiple clients for a module.
94.

What is the use of duck typing?(a) More restriction on the type values that can be passed to a given method(b) No restriction on the type values that can be passed to a given method(c) Less restriction on the type values that can be passed to a given method(d) Makes the program code smaller

Answer» The correct choice is (c) Less restriction on the type values that can be passed to a given method

The explanation: In Python, any set of classes with a common set of methods can be treated similarly. This is called duck typing. Hence duck typing imposes less restrictions.
95.

Suppose list1 is [2445,133,12454,123], what is max(list1)?(a) 2445(b) 133(c) 12454(d) 123

Answer» The correct option is (c) 12454

The best I can explain: Max returns the maximum element in the list.
96.

Which function is used to read all the characters?(a) Read()(b) Readcharacters()(c) Readall()(d) Readchar()

Answer» Correct choice is (a) Read()

The best I can explain: The read function reads all characters fh = open(“filename”, “r”)

content = fh.read().
97.

What will be the output of the “hello” +1+2+3?(a) hello123(b) hello(c) Error(d) hello6

Answer» The correct option is (c) Error

The explanation is: Cannot concatenate str and int objects.
98.

Which of the following is not an advantage of using modules?(a) Provides a means of reuse of program code(b) Provides a means of dividing up tasks(c) Provides a means of reducing the size of the program(d) Provides a means of testing individual parts of the program

Answer» Right option is (c) Provides a means of reducing the size of the program

Explanation: The total size of the program remains the same regardless of whether modules are used or not. Modules simply divide the program.
99.

Which of the following is not a valid namespace?(a) Global namespace(b) Public namespace(c) Built-in namespace(d) Local namespace

Answer» Right answer is (b) Public namespace

Easy explanation - During a Python program execution, there are as many as three namespaces – built-in namespace, global namespace and local namespace.
100.

Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?(a) 5(b) 4(c) None(d) Error

Answer» The correct answer is (a) 5

Explanation: Execute in the shell and verify.