Python interview questions
- How is Python different from other programming languages?
1. Python has high-level built-in datatypes.
2. Python is a dynamically typed language.
3. Python programs are 3-5 times shorter than other languages like Java.
4. Python runs slower than other languages like Java.
- What is Monkey patching in Python?
1. Monkey patching refers to run-time modifications in class or modules in Python.
2. It is good for testing or mocking out behavior.
3. In general, it is referred to as adding, modifying, or suppressing the default behavior of class or modules during runtime without changing the source code.
- What is the difference b/w list and tuple in Python?
|Memory||It consume more memory.||It consumes less memory.|
|Mutable||Lists are mutable. ||Tuples are immutable.|
|Iteration||List iterations are time-consuming. ||Tuple iterations are faster.|
|Built-in||List has many built-in methods. ||Tuple doesn't have many built-in methods.|
- Why do we call Python an interpreted language?
Ans: Python code doesn't need to be compiled before running the code. Instead of a compiler Python uses an interpreter to convert code into machine understanding byte code.
- What are the built-in types in Python?
Ans: Python Built-in Datatypes List, Tuple, Dictionary, Set, and Frozenset.
- How garbage collection is done in Python?
Ans: Garbage collection was done during runtime. It gets triggered when the object reference count reaches zero.
- What are decorators in Python?
Ans: Decorators in Python is a function that takes another function as an argument and also returns a function.
- What is Self in Python?
Ans: Self in Python is an instance of a class. We can access the methods and attributes of that class.
- What is __init__ method in Python?
Ans: __init__ method is a type of constructor in Python. It is used to assign values to data members when an object of that class is created.
- What is break, continue and pass in Python?
Ans: Break - It is used to stop the flow of loops in Python.
Continue - It is used to continue the flow and skip the remaining statements of loops.
Pass - Pass is generally used to create an empty method or class in Python.
- What is a slice in Python?
Ans: Slicing is used to get the part of List, String, Tuple, etc. We can also modify or Delete the items of mutable data types in Python.
- What are namespaces in Python? Why are they used?
Ans: In the namespaces system every object has a unique name in Python. It is a way to implement scope.
There are three types of namespaces in Python- Local namespaces, Global namespaces, and Built-in namespaces.
Local namespaces: It is used to store the object of local variables.
Global namespaces: It is used to store the object of Global functions and variables.
Built-in namespaces: It is used to store the object of Built-in datatypes.
- What is lambda in Python? Why it is used?
Ans: Lambda function is also called Anonymous Function. It can be defined without name or one line function.
Example: addTwoNumbers = lambda a, b : a + b
Output : 3
- What are pickling and unpickling in Python?
Ans: Pickling and Unpickling refer to storing Python objects in a file/database. Also, referred, to as Serializing and De-Serializing Python objects into byte streams.
- What are generators in Python?
Ans: Generators are used to create user-defined iterators functions. Generator doesn't return a single value, return a sequence of values. In the generator, a yield statement is used in place of return.
Example: def count_generator():
n = 1
n += 1
countObj = count_generator()
- Why do we use *args and **kwargs in Python?
Ans: These are used to pass a variable number of parameters or arguments to Python functions. They add flexibility to pass variable arguments to functions.
*args: It sends a list of values to the python function.
**kwargs: It sends a dictionary of values
- Python supports negative indexes e.g arr[-1]?
Yes, Python supports negative index values.
1. -1 gives the last element of the array.
2. -2 gives the second last element of an array.
- What is the main() function in Python? How can we invoke it?
Ans: Main function is like the entry point of execution for any program. However, the Python interpreter executes the code from the first line.
- What are Keywords in Python? How many keywords are there in Python?
Ans: Keywords are predefined and reserved words in Python. There are around 35 keywords in Python.
In Python console:-
Here is a list of the Python keywords. Enter any keyword to get more help.
- Is Python a case-sensitive language?
Ans: Yes, Python is case sensitive language.
These questions will help you in Python interviews.
Thanks for Reading !!!
anonymous | Sept. 30, 2021, 1:36 p.m.
Really helpful 👍