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 is main function in python? How do you invoke it? |
|
Answer» In the world of programming languages, the MAIN is considered as an entry point of execution for a PROGRAM. But in python, it is known that the interpreter serially interprets the FILE line-by-line. This means that python does not provide main() function explicitly. But this doesn't MEAN that we cannot simulate the execution of main. This can be done by defining user-defined main() function and by using the __name__ property of python file. This __name__ variable is a special built-in variable that points to the name of the current module. This can be done as SHOWN below: def main(): print("Hi Interviewbit!")if __name__=="__main__": main() |
|
| 2. |
Differentiate between deep and shallow copies. |
Answer»
|
|
| 3. |
Are there any tools for identifying bugs and performing static analysis in python? |
|
Answer» Yes, there are tools LIKE PyChecker and Pylint which are used as static analysis and linting tools RESPECTIVELY. PyChecker helps find bugs in PYTHON source CODE files and raises alerts for code issues and their complexity. Pylint checks for the module’s coding standards and supports different plugins to ENABLE custom features to meet this requirement. |
|
| 4. |
Define PIP. |
|
Answer» PIP stands for PYTHON INSTALLER Package. As the name INDICATES, it is used for installing different python modules. It is a command-line tool providing a seamless interface for installing different python modules. It searches over the internet for the package and installs them into the working DIRECTORY without the need for any interaction with the USER. The syntax for this is: pip install <package_name> |
|
| 5. |
Define PYTHONPATH. |
|
Answer» It is an environment variable used for incorporating additional directories during the import of a module or a PACKAGE. PYTHONPATH is used for checking if the imported PACKAGES or modules are available in the EXISTING directories. Not just that, the interpreter USES this environment variable to identify which module NEEDS to be loaded. |
|
| 6. |
Define GIL. |
|
Answer» GIL stands for Global Interpreter Lock. This is a mutex used for limiting access to python objects and AIDS in effective thread synchronization by avoiding deadlocks. GIL helps in achieving multitasking (and not parallel computing). The following DIAGRAM represents how GIL works. Based on the above diagram, there are three THREADS. FIRST Thread acquires the GIL first and starts the I/O execution. When the I/O operations are done, thread 1 releases the acquired GIL which is then taken up by the second thread. The process REPEATS and the GIL are used by different threads alternatively until the threads have completed their execution. The threads not having the GIL lock goes into the waiting state and resumes execution only when it acquires the lock. |
|
| 7. |
What are the differences between pickling and unpickling? |
|
Answer» PICKLING is the CONVERSION of python objects to binary form. Whereas, unpickling is the conversion of binary form DATA to python objects. The pickled objects are used for storing in disks or EXTERNAL memory locations. Unpickled objects are used for getting the data back as python objects upon which PROCESSING can be done in python. Python provides a pickle module for achieving this. Pickling uses the pickle.dump() method to dump python objects into disks. Unpickling uses the pickle.load() method to get back the data as python objects. |
|
| 8. |
Can you easily check if all characters in the given string is alphanumeric? |
|
Answer» This can be easily done by making use of the isalnum() method that returns TRUE in case the STRING has only alphanumeric characters. For Example - "abdc1321".isalnum() #Output: True"xyz@123$".isalnum() #Output: FALSEAnother way is to use match() method from the re (regex) module as shown: IMPORT reprint(BOOL(re.match('[A-Za-z0-9]+$','abdc1321'))) # Output: Trueprint(bool(re.match('[A-Za-z0-9]+$','xyz@123$'))) # Output: False |
|
| 9. |
How can you generate random numbers? |
|
Answer» Python PROVIDES a module called random using which we can generate random numbers.
|
|
| 10. |
What are lambda functions? |
|
Answer» LAMBDA functions are generally inline, ANONYMOUS functions represented by a single expression. They are USED for CREATING function objects during runtime. They can accept any number of parameters. They are usually used where functions are required only for a short period. They can be used as: mul_func = lambda x,y : x*yprint(mul_func(6, 4))# OUTPUT: 24 |
|
| 11. |
What are some of the most commonly used built-in modules in Python? |
|
Answer» Python modules are the FILES having python code which can be FUNCTIONS, variables or CLASSES. These go by .py EXTENSION. The most commonly AVAILABLE built-in modules are:
|
|
| 12. |
Differentiate between a package and a module in python. |
|
Answer» The module is a single python FILE. A module can IMPORT other modules (other python files) as objects. WHEREAS, a package is the folder/directory where different sub-packages and the modules reside. A python module is created by saving a file with the extension of .py. This file will have classes and functions that are reusable in the code as well as across modules. A python package is created by following the below steps:
|
|