Here we provide frequently asked interview questions on Python. Which can help to clear your basics also…
Table of Contents
Technical Interview Questions on Python
1. What is python, What are the benefits of using it?
Ans: help() is a built-in function that can be used to return the Python documentation of a particular object, method, attributes, etc.dir() displays a list of attributes for the objects which are passed as an argument. If dir() is without the argument then it returns a list of names in the current local space.
2. What is PEP 8?
Ans: PEP 8 is a coding convention, a set of recommendations, about how to write your python code more readable.
3. Is Python a case-sensitive language?
4. What are the supported data types in Python?
Ans: Python has five standard data types –
5. What are functions in Python?
Ans: A function is a block of code that is executed only when it is called. To define a function in Python, the def keyword is used.
Ex. def Newfunc():
print(“Hi, Welcome to my world”)
Newfunc(); #calling the function
output: Hi, Welcome to my world
6. What is _init_?
Ans: _init_ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the _init_ method.
7. How Python is interpreted?
Ans: Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.
8. What are Python decorators?
Ans: A Python decorator is a specific change that we make in Python syntax to alter functions easily.
9. What are the generator functions in Python?
Ans: Any function that contains at least one yield statement is called. The difference between return and yield is, return statement saving all its states pauses and later continues from there on successive calls.
10. What happens in the background when you run a Python file?
Ans: When we run a .py file, it undergoes two phases. In the first phase, it checks the syntax and in the second phase, it compiles to bytecode (.pyc file is generated) using Python virtual machine, loads the bytecode into memory, and runs.
11. What is a module in Python?
Ans: A module is a .Py file in which variables, functions, and classes can be defined. It can also have a runnable code.
12. How do you reload a Python module?
Ans: There is a function called reload() in Python, which takes the module name as an argument and reloads the module.
13. How is multithreading achieved in Python?
Ans: A thread is a lightweight process, and multithreading allows us to execute multiple threads at once. As you know, Python is a multithreaded language. It has a multi-threading package.
The GIL(Global Interpreter Lock) ensures that a single thread executes at a time. A thread holds the GIL and does a little work before passing it on to the next thread. This makes for an illusion of parallel execution. But in reality, it is just threaded taking turns at the CPU. of course, all the passing around adds overhead to the execution.
14. How do you debug a program in Python?
Ans: To debug a Python program, we use the PDB module. This is the Python debugger; we will discuss it in a tutorial soon. If we start a program using PDB, it will let us step through the code.
15. How would you make a python script executable on Unix?
Ans: For this to happen, two conditions must be met:
- The script file’s mode must be executable
- The first line must begin with a hash (#). An example of this will be: #!/user/local/bin/python
16. What is the purpose of is, not and in operators?
Ans: Operators are special functions. They take one or more values and produce a corresponding result.
is: return true when 2 operands are true ( example” “a” is ‘a’)
not: return the inverse of the boolean value
in: Checks if some elements are present in some sequence.
17. What is the usage of the help() and dir() function in Python?
help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.
- Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
- Dir() function: The dir() function is used to display the defined symbol.
18. How can files be deleted in Python?
To delete a file in Python, you need to import the os Module. After that, you need to use the os.remove() function.
Ex. import os
19. What are the built-in types of python?
Built-in types in Python are as follows:-
- Complex numbers
- Built-in functions
20. Does Python have an OOPs concept?
Python is an object-oriented programming language. This means that any program can be solved in python by creating an object model. However, Python can be treated as procedural as well as structural language.
21. What are Python libraries? Name a few of them.
Ans: Python libraries are a collection of Python packages. Some of the majorly used python libraries are – Numpy, Pandas, Matplotlib, Scikit-learn, and many more.
22. Does Python support multiple inheritances?
Multiple Inheritance means that a class can be derived from more than one parent class. Python does support multiple inheritances, unlike Java.
23. What is Polymorphism in python?
Ans: Polymorphism means the ability to take multiple forms. So, for instance, if the parent class has a method named BCA then the child class also can have a method with the same name BCA having its own parameters and variables. Python allows polymorphism.
24. Define encapsulation in Python?
Ans: Encapsulation means binding the code and the data together. A Python class is an example of encapsulation.
25. How do you do data abstraction in Python?
Ans: Data Abstraction is providing only the required details and hiding the implementation from the world. It can be achieved in Python by using interfaces and abstract classes.
26. How will you compare the two lists?
Ans: cmp(list1,list2) -compares element of both lists.
27. How will you remove an object from a list?
Ans: list.remove(obj) -Removes object obj from list.
28. How will you reverse a list?
Ans: list.reverse() -Reverse objects of list in place.
29. How will you sort a list?
Ans: list.sort([func]) -Sorts objects of list, use compare func if given.
30. What is the lambda function in python?
Ans: ‘Lambda’ is a keyword in python which creates an anonymous function. Lambda does not contain a block of statements. It does not contain return statements.
31. Do you think Python has a compiler?
Ans: Yes it has a compiler that works automatically so we don’t notice the compiler of python.
32. Name the python library used for Machine learning?
Ans: Scikit-learn python library used for Machine learning.
33. Is A String Immutable or Mutable in Python?
Ans: Python strings are indeed immutable.
34. What is the Index in python?
Ans: An index is an integer data type that denotes a position within an ordered list or a string. In Python, strings are also lists of characters. We can access them using the index which begins from zero and goes to the length minus one.
35. What is Docstring in Python?
Ans: A docstring is a unique text that happens to be the first statement in the following Python constructs: Module, Function, Class, or Method definition.
36. How do we write functions In Python?
Ans: We can create a Python function in the following manner.
Step-1: To begin the function, start writing with the keyword def and then mention the function name.
Step-2: We can now pass the arguments and enclose them using the parentheses. A colon, in the end, marks the end of the function header.
Step-3: After pressing an enter, we can add the desired Python statements for execution.
37. What is “call by value” in Python?
In call-by-value, the arguments whether an expression or a value gets bound to the respective variable in the function. Python will treat that variable as local in the function-level scope. Any changes made to that variable will email local and will not reflect outside the function.
38. What is “Call by reference” in Python?
Ans: We use both “call-by-reference” and”pass-by-reference” interchangeably. When we pass an argument by reference, then it is available as an implicit reference to the function, rather than a simple copy. In such a case any modification to the arguments will also be visible to the caller.
39. What is a map function in Python?
Ans: The map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 argument, then many iterable are given.
40. Explain the use of with statement?
Ans: In Python generally “with” statement is used to open a file, process the data present in the file, and also close the file without calling a close() method. “with” statement makes the exception handling simpler by providing cleanup activities.
41. What do you mean by overriding methods?
Ans: Suppose class B inherits from class A. Both have the method say hello() -to each, their own version. B overrides the say hello() of class A. so, when we create an object of class B, it calls the version that class B has.
42. Explain Python in one line?
Ans: Python is a modern powerful interpreted language with threads, objects, modules, exception and also have the property of automatic memory management.
43. How can an object be copied in Python?
Ans: By using two ways object can be copied in Python: Shallow copy & Deep copy
44. Describe how to send an email from a python script?
Ans: The smtplib module is used to defines SMTP client session objects that can be used to send email using Pythons script.
45. What are ODBS modules for python?
- PythonWin ODBC module
46. How do you make use of Arrays in Python?
Ans: Python does not support Arrays. However, you can use the list collection type which can store an unlimited number of elements.
Most Important Technical Interview Questions on Python
1. Explain how you can session in flask?
Ans: A session basically allows you to remember information from one request to another. In a flask, it uses a signed cookie so the user can look at the session contents and modify them. The user can modify the session if only it has the secret key Flask.secret.key.
2. Mention the use of the split function in Python?
Ans: The use of the split function in Python is that it breaks a string into shorter strings using the defined separator. It gives a list of all words present in the string.
3. Explain how can you access a module written in Python from C?
Ans: You can access a module written in Python from C by the following method, Module == Pylmport_ImportModule(<modulename>);
4. How you can convert a number to a string?
Ans: In order to convert a number into a string, use the inbuilt function str(). If you want an octal or hexadecimal representation, use the inbuilt function oct() or hex()
5. What is docstring in Python?
Ans: A Python documentation string is known as docstring, it is a way of documenting Python functions, modules, and classes.
Interview Questions on Python Test
1. Is Python case Sensitive when dealing with identifiers?
2. What is the order of precedence in python?
3. What is the answer to this expression, 22 % 3 is?
4. Mathematical operations can be performed on a string. State whether true or false?
5. What is the output of this expression, 3*1**3?
Hope you have now some good idea on Interview Questions on Python. It is written by @Uttam_Kumar. If you want some more, download the app in the play store Interview questions and Answers for Python which will design by @Uttam_Kumar
Also-Read: Top 30+ Interview Questions on SQL