InterviewSolution
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»
|
|
| 2. |
What does *args and **kwargs mean? |
|
Answer» *args
**kwargs
|
|
| 3. |
Explain split() and join() functions in Python? |
| Answer» 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»
|
|
| 6. |
How are arguments passed by value or by reference in python? |
Answer»
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»
|
|
| 8. |
What is the difference between .py and .pyc files? |
Answer»
|
|
| 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. |
|
| 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. |
|
| 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:
Unpickling:
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 9Note: 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. |
|
| 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:
|
|
| 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:
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)]
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]
|
|
| 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:
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:
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. |
|