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.

1.

What are negative indexes and why are they used?

Answer»
  • Negative indexes are the indexes from the END of the LIST or tuple or string.
  • Arr[-1] MEANS the last ELEMENT of array Arr[]
arr = [1, 2, 3, 4, 5, 6]#get the last elementprint(arr[-1]) #output 6#get the second last elementprint(arr[-2]) #output 5
2.

What does *args and **kwargs mean?

Answer»

*args

  • *args is a special syntax used in the FUNCTION definition to pass variable-length arguments.
  • “*” means variable length and “args” is the name used by convention. You can use any other.
def multiply(a, b, *argv): mul = a * b for num in argv: mul *= num return mulprint(multiply(1, 2, 3, 4, 5)) #output: 120

**kwargs

  • **kwargs is a special syntax used in the function definition to pass variable-length keyworded arguments.
  • Here, ALSO, “kwargs” is used just by convention. You can use any other name.
  • Keyworded argument means a variable that has a name when passed to a function.
  • It is actually a dictionary of the variable names and its value.
def tellArguments(**kwargs): for key, value in kwargs.items(): print(key + ": " + value)tellArguments(ARG1 = "argument 1", arg2 = "argument 2", ARG3 = "argument 3")#output:# arg1: argument 1# arg2: argument 2# arg3: argument 3
3.

Explain split() and join() functions in Python?

Answer»
  • You can use split() function to split a string based on a delimiter to a LIST of STRINGS.
  • You can use JOIN() function to join a list of strings based on a delimiter to GIVE a SINGLE string.
string = "This is a string."string_list = string.split(' ') #delimiter is ‘space’ character or ‘ ‘print(string_list) #output: ['This', 'is', 'a', 'string.']print(' '.join(string_list)) #output: This is a string.
4.

Explain how to delete a file in Python?

Answer»

Use COMMAND os.remove(file_name)

IMPORT osos.remove("ChangedFile.csv")PRINT("File REMOVED!")
5.

What are iterators in Python?

Answer»
  • An iterator is an object.
  • It remembers its state i.e., where it is during iteration (SEE code below to see how)
  • __iter__() method initializes an iterator.
  • It has a __next__() method which returns the next item in iteration and points to the next element. Upon reaching the end of iterable object __next__() must return StopIteration exception.
  • It is also self-iterable.
  • Iterators are OBJECTS with which we can iterate over iterable objects LIKE lists, strings, etc.
class ArrayList: def __init__(self, number_list): self.numbers = number_list def __iter__(self): self.pos = 0 return self def __next__(self): if(self.pos < len(self.numbers)): self.pos += 1 return self.numbers[self.pos - 1] ELSE: raise StopIterationarray_obj = ArrayList([1, 2, 3])it = iter(array_obj)print(next(it)) #output: 2print(next(it)) #output: 3print(next(it))#Throws Exception#Traceback (most RECENT call last):#...#StopIteration
6.

How are arguments passed by value or by reference in python?

Answer»
  • Pass by value: COPY of the actual object is passed. Changing the value of the copy of the object will not change the value of the original object.
  • Pass by reference: Reference to the actual object is passed. Changing the value of the new object will change the value of the original object.

In PYTHON, arguments are passed by reference, i.e., reference to the actual object is passed.

def appendNumber(arr): arr.append(4)arr = [1, 2, 3]PRINT(arr) #Output: => [1, 2, 3]appendNumber(arr)print(arr) #Output: => [1, 2, 3, 4]
7.

How Python is interpreted?

Answer»
  • Python as a LANGUAGE is not interpreted or compiled. Interpreted or compiled is the property of the implementation. Python is a bytecode(set of interpreter READABLE instructions) interpreted generally.
  • SOURCE code is a file with .py extension.
  • Python compiles the source code to a set of instructions for a virtual machine. The Python interpreter is an implementation of that virtual machine. This intermediate format is called “bytecode”.
  • .py source code is FIRST compiled to give .pyc which is bytecode. This bytecode can be then interpreted by the official CPython or JIT(Just in TIME compiler) compiled by PyPy.
8.

What is the difference between .py and .pyc files?

Answer»
  • .py files contain the source code of a program. Whereas, .pyc file contains the bytecode of your program. We get bytecode after COMPILATION of .py file (source code). .pyc files are not created for all the files that you run. It is only created for the files that you import.
  • Before EXECUTING a python program python INTERPRETER checks for the compiled files. If the file is present, the virtual machine executes it. If not found, it checks for .py file. If found, COMPILES it to .pyc file and then python virtual machine executes it.
  • Having .pyc file saves you the compilation time.
9.

What is the use of help() and dir() functions?

Answer»

help() function in PYTHON is used to display the documentation of modules, classes, functions, keywords, etc. If no parameter is PASSED to the help() function, then an interactive help utility is launched on the console.
dir() function tries to return a valid LIST of attributes and methods of the object it is called upon. It behaves differently with different objects, as it aims to produce the most relevant data, rather than the complete information.

  • For Modules/Library objects, it returns a list of all attributes, CONTAINED in that module.
  • For CLASS Objects, it returns a list of all valid attributes and base attributes.
  • With no arguments passed, it returns a list of attributes in the current scope.
10.

What is PYTHONPATH in Python?

Answer»

PYTHONPATH is an environment VARIABLE which you can set to add ADDITIONAL directories where Python will look for modules and packages. This is ESPECIALLY useful in maintaining Python LIBRARIES that you do not WISH to install in the global default location.

11.

What are generators in Python?

Answer»

Generators are functions that return an iterable collection of items, one at a time, in a SET manner. Generators, in general, are used to create iterators with a different approach. They employ the use of YIELD keyword rather than return to return a generator object.
Let's try and build a generator for FIBONACCI numbers -

## generate fibonacci numbers UPTO ndef fib(n): p, q = 0, 1 while(p < n): yield p p, q = q, p + qx = fib(10) # create generator object ## iterating using __next__(), for Python2, use NEXT()x.__next__() # output => 0x.__next__() # output => 1x.__next__() # output => 1x.__next__() # output => 2x.__next__() # output => 3x.__next__() # output => 5x.__next__() # output => 8x.__next__() # error ## iterating using loopfor i in fib(10): print(i) # output => 0 1 1 2 3 5 8
12.

What is pickling and unpickling?

Answer»

Python library offers a feature - serialization out of the box. Serializing an object refers to transforming it into a format that can be stored, so as to be able to deserialize it, later on, to obtain the original object. Here, the pickle module comes into play.

Pickling:

  • Pickling is the name of the serialization process in Python. Any object in Python can be serialized into a byte STREAM and dumped as a file in the memory. The process of pickling is compact but pickle objects can be COMPRESSED further. Moreover, pickle keeps track of the objects it has serialized and the serialization is portable across versions.
  • The function used for the above process is pickle.dump().

Unpickling:

  • Unpickling is the complete inverse of pickling. It deserializes the byte stream to RECREATE the objects stored in the file and loads the object to memory.
  • The function used for the above process is pickle.load().

Note: Python has another, more primitive, serialization module called MARSHALL, which exists primarily to support .pyc files in Python and differs significantly from the pickle.

13.

What is the difference between xrange and range in Python?

Answer»

xrange() and range() are QUITE similar in terms of functionality. They both generate a sequence of integers, with the only difference that range() returns a Python list, whereas, xrange() returns an xrange object.

So how does that make a difference? It sure does, because unlike range(), xrange() doesn't generate a static list, it creates the value on the GO. This technique is commonly used with an object-type generator and has been termed as "yielding".

Yielding is crucial in applications where memory is a constraint. Creating a static list as in range() can lead to a Memory Error in such CONDITIONS, while, xrange() can handle it optimally by using just enough memory for the generator (significantly LESS in comparison).

for i in xrange(10): # numbers from o to 9 print i # output => 0 1 2 3 4 5 6 7 8 9for i in xrange(1,10): # numbers from 1 to 9 print i # output => 1 2 3 4 5 6 7 8 9for i in xrange(1, 10, 2): # skip by two for next print i # output => 1 3 5 7 9

Note: xrange has been deprecated as of Python 3.x. Now range does exactly the same as what xrange used to do in Python 2.x, since it was way better to use xrange() than the ORIGINAL range() function in Python 2.x.

14.

How do you copy an object in Python?

Answer»

In Python, the assignment STATEMENT (= operator) does not copy OBJECTS. Instead, it creates a binding between the existing object and the target variable name. To create copies of an object in Python, we need to use the copy module. Moreover, there are two WAYS of creating copies for the given object using the copy module -

Shallow Copy is a bit-wise copy of an object. The copied object created has an exact copy of the values in the original object. If either of the values is a REFERENCE to other objects, just the reference addresses for the same are copied.
Deep Copy copies all values recursively from source to target object, i.e. it even duplicates the objects referenced by the source object.

from copy import copy, deepcopylist_1 = [1, 2, [3, 5], 4]## shallow copylist_2 = copy(list_1) list_2[3] = 7list_2[2].append(6)list_2 # OUTPUT => [1, 2, [3, 5, 6], 7]list_1 # output => [1, 2, [3, 5, 6], 4]## deep copylist_3 = deepcopy(list_1)list_3[3] = 8list_3[2].append(7)list_3 # output => [1, 2, [3, 5, 6, 7], 8]list_1 # output => [1, 2, [3, 5, 6], 4]
15.

What is lambda in Python? Why is it used?

Answer»

LAMBDA is an ANONYMOUS function in PYTHON, that can accept any number of arguments, but can only have a single EXPRESSION. It is generally used in situations requiring an anonymous function for a short time period. Lambda functions can be used in either of the two ways:

mul = lambda a, b : a * bprint(mul(2, 5)) # output => 10
  • Wrapping lambda functions inside another function:
def myWrapper(n): return lambda a : a * nmulFive = myWrapper(5)print(mulFive(2)) # output => 10
16.

What are Dict and List comprehensions?

Answer»

Python comprehensions, like DECORATORS, are syntactic sugar constructs that help build altered and filtered lists, dictionaries, or SETS from a given list, dictionary, or set. Using comprehensions saves a lot of time and code that might be considerably more verbose (containing more LINES of code). Let's check out some examples, where comprehensions can be truly beneficial:

  • PERFORMING mathematical operations on the entire list
my_list = [2, 3, 5, 7, 11]squared_list = [x**2 for x in my_list] # list comprehension# output => [4 , 9 , 25 , 49 , 121]squared_dict = {x:x**2 for x in my_list} # dict comprehension# output => {11: 121, 2: 4 , 3: 9 , 5: 25 , 7: 49}
  • Performing conditional filtering operations on the entire list
my_list = [2, 3, 5, 7, 11]squared_list = [x**2 for x in my_list if x%2 != 0] # list comprehension# output => [9 , 25 , 49 , 121]squared_dict = {x:x**2 for x in my_list if x%2 != 0} # dict comprehension# output => {11: 121, 3: 9 , 5: 25 , 7: 49}
  • Combining multiple lists into one

Comprehensions allow for multiple iterators and hence, can be used to combine multiple lists into one. 

a = [1, 2, 3]b = [7, 8, 9][(x + y) for (x,y) in zip(a,b)] # parallel iterators# output => [8, 10, 12][(x,y) for x in a for y in b] # nested iterators# output => [(1, 7), (1, 8), (1, 9), (2, 7), (2, 8), (2, 9), (3, 7), (3, 8), (3, 9)]
  • Flattening a multi-dimensional list

A similar approach of nested iterators (as above) can be applied to flatten a multi-dimensional list or work upon its inner elements. 

my_list = [[10,20,30],[40,50,60],[70,80,90]]flattened = [x for temp in my_list for x in temp]# output => [10, 20, 30, 40, 50, 60, 70, 80, 90]

Note: List comprehensions have the same effect as the map method in other languages. They follow the mathematical set builder notation rather than map and filter functions in Python.

17.

What are decorators in Python?

Answer»

Decorators in Python are essentially functions that add functionality to an EXISTING function in Python without changing the structure of the function itself. They are represented the @decorator_name in Python and are called in a bottom-up fashion. For example:

# decorator function to convert to lowercasedef lowercase_decorator(function): DEF wrapper(): func = function() string_lowercase = func.lower() return string_lowercase return wrapper# decorator function to split wordsdef splitter_decorator(function): def wrapper(): func = function() string_split = func.split() return string_split return wrapper@splitter_decorator # this is EXECUTED next@lowercase_decorator # this is executed firstdef hello(): return 'Hello World'hello() # output => [ 'hello' , 'world' ]

The beauty of the decorators LIES in the fact that besides adding functionality to the output of the method, they can even accept arguments for functions and can further MODIFY those arguments before passing it to the function itself. The inner nested function, i.e. 'wrapper' function, plays a significant role here. It is implemented to enforce encapsulation and thus, keep itself hidden from the global scope.

# decorator function to capitalize namesdef names_decorator(function): def wrapper(arg1, arg2): arg1 = arg1.capitalize() arg2 = arg2.capitalize() string_hello = function(arg1, arg2) return string_hello return wrapper@names_decoratordef say_hello(name1, name2): return 'Hello ' + name1 + '! Hello ' + name2 + '!'say_hello('sara', 'ansh') # output => 'Hello Sara! Hello Ansh!'
18.

What is Scope Resolution in Python?

Answer»

Sometimes objects within the same scope have the same name but function differently. In such cases, scope RESOLUTION comes into play in Python automatically. A few examples of such behavior are:

  • Python modules namely 'math' and 'cmath' have a lot of functions that are common to both of them - log10(), acos(), exp() etc. To resolve this ambiguity, it is necessary to prefix them with their respective module, like math.exp() and cmath.exp().
  • Consider the code below, an object temp has been initialized to 10 globally and then to 20 on function CALL. However, the function call didn't CHANGE the value of the temp globally. Here, we can OBSERVE that Python draws a clear line between global and local variables, treating their namespaces as separate identities.
temp = 10 # global-scope variabledef func(): temp = 20 # local-scope variable print(temp)print(temp) # output => 10func() # output => 20print(temp) # output => 10

This behavior can be overridden USING the global keyword inside the function, as shown in the following example:

temp = 10 # global-scope variabledef func(): global temp temp = 20 # local-scope variable print(temp)print(temp) # output => 10func() # output => 20print(temp) # output => 20
19.

What are Python namespaces? Why are they used?

Answer»

A namespace in PYTHON ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as dictionaries with 'name as key' mapped to a corresponding 'object as value'. This ALLOWS for multiple namespaces to use the same name and map it to a separate object. A few examples of namespaces are as follows:

  • Local Namespace includes local names inside a function. the namespace is temporarily created for a function call and gets cleared when the function returns.
  • Global Namespace includes names from various imported packages/ modules that are being used in the current project. This namespace is created when the PACKAGE is imported in the script and lasts until the execution of the script.
  • Built-in Namespace includes built-in functions of core Python and built-in names for various TYPES of exceptions.

The lifecycle of a namespace depends upon the scope of objects they are mapped to. If the scope of an object ends, the lifecycle of that namespace comes to an end. Hence, it isn't POSSIBLE to access inner namespace objects from an outer namespace.