Python development & Training in Jaipur

Frequently Asked Interview Questions: Python Basics

Python has become quite a popular language for enterprises and the development of quality projects in recent years. The language is versatile and is used for application development, writing scripts for desktop programs and in the configuration of servers to name a few. With so much use, “the glue” language has generated numerous job opportunities. Freshers looking to ace the Python interviews need to have a clear understanding of Python basics. To help learners with that, we are listing the most frequently asked interview questions in Python Basics.

Python Interview Questions

1. What is Python?

Python is an interpreted language which means we don’t need to compile Python codes before it is run. PHP and Ruby are some of the other interpreted languages.

Few features of Python are:

  • Python is dynamically typed, we don’t need to specify the types of variables while declaring them. For instance, x=111 and then x=”I’m a string” runs in Python without error.
  • Python is well suited to OOP and allows the definition of classes, composition, and inheritance. Python does not use access specifiers.
  • In Python, functions and class objects are first-class objects.
  • Python is quick and allows C based extensions like the numpy making it efficient.
  • Python is used in web applications, automation, scientific modelling, big data applications etc.
  • Python facilitates easy overriding of algorithms and structures.
2. What is the scope of multi-threading in Python?

Python doesn’t allow a complete multi-threading solution. Python’s construct Global Interpreter Lock (GIL) makes sure that only one thread is executed at any point in time. Threading library in Python doesn’t allow the use of any extra CPU cores. Though multi-threading can be done by the OS or external tools like Spark or Hadoop.

3. What will be the output of the code?
def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l) 
f(2)
f(3,[3,2,1])
f(3)

Output

[0, 1]

[3, 2, 1, 0, 1, 4] [0, 1, 0, 1, 4]

4. What is monkey patching?

Monkey patching is changing the behaviour of a function or object after it has already been defined. For example:

import datetime
datetime.datetime.now = lambda: datetime.datetime(2012, 12, 12)
5. What do *args and **kwargs mean?

These are identifiers and the terms *args and **kwargs are a convention.

We use *args when we aren’t sure of the number of arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function.

**kwargs is used in cases where we are not sure about the number of keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.

6. What are @classmethod@staticmethod@property?

These are decorators.

Decorators are a special kind of function that either takes/returns a function or a class. These are used on functions defined within classes. The @ symbol makes it easy to read.

7. How does Python garbage collection mechanism work?
  • Python maintains a count of the number of references to each object in memory. If the reference count goes to 0, the associated object is no longer live and the memory allocated to that object is available for something else.
  • The garbage collector also periodically looks for “reference cycles” and cleans them up.
  • Python uses some heuristics for speedy garbage collection. When an object is created, garbage collector assigns them to generations. Each object gets one generation, and younger generations are dealt with first.
8. What are the built-in data-types in Python?

Built-in datatypes in Python are of two types.

Immutable

  • Numbers
  • Strings
  • Tuples

Mutable

  • List
  • Sets
  • Dictionary
9. What do you understand by Python Package?

Python package is the collection of modules that are used to structure the module. It uses a dotted module nomenclature. For instance, A.B means that B is a submodule of the package A.

10. What’s the difference between lists and tuples?
Tuples Lists
Tuples are Immutable just requiring less memory Lists are Mutable and thus requires more memory
Tuples are faster compared to Lists   Lists are slower that tuples  
Tuples are heterogeneous data structures   Lists are homogenous sequences  
Tuples have structure Lists have order
We use parenthesis () to construct tuples We use parenthesis we use square brackets [ ] to get a new list
Conclusion

These Python interview questions are internationally limited to basics and are targeted towards freshers. Answers are more of explanatory so that learners get to understand the basics in a detailed manner. In technical interviews, learners are supposed to showcase their understanding while keeping the answers to the point and these questions are supposed to cater to that. IIHT wishes very good luck for your interviews.

Leave a Reply

Your email address will not be published. Required fields are marked *