Top 10 Most Asked Python Interview Questions With Answers Part 05

Top 10 Most Asked Python Interview Questions With Answers Part 05 By Muhammad Umair

Q.1 What are higher ordered functions?

You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b.
Using nested scopes:

def linear(a,b):    
def result(x):
return a*x + b
return result


using a callable object:

class linear:    
def __init__(self, a, b):
self.a, self.b = a,b
def __call__(self, x):
return self.a * x + self.b

In both cases:

taxes = linear(0.3,2) gives a callable object where

taxes(10e6) == 0.3 * 10e6 + 2.

The callable object approach has the disadvantage that it is a bit slower and results in slightly longer code. However, note that a collection of callables can share their signature via inheritance:

class exponential(linear):    
__init__ inherited
def __call__(self, x):
return self.a * (x ** self.b)

Object can encapsulate state for several methods:

class counter:    
value = 0
def set(self, x):
self.value = x
def up(self):
def down(self):
count = counter()inc,
dec, reset = count.up, count.down, count.set

Here inc(), dec() and reset() act like functions which share the same counting variable.

Q.2 How do I copy a file? How to copy object in Python? Diff between shallow copy and deep copy?

The shutil module contains a copyfile() function.

A deep copy copies an object into another. This means that if you make a change to a copy of an object, it won’t affect the original object. In Python, we use the function deepcopy() for this, and we import the module copy. We use it like:

import copyb = copy.deepcopy (a)

A shallow copy, however, copies one object’s reference to another. So, if we make a change in the copy, it will affect the original object. For this, we have the function copy(), we use it like:

b = copy.copy(a)
  • Differentiate between lists and tuples.

The major difference is that a list is mutable, but a tuple is immutable. Examples:

Traceback (most recent call last):File “<pyshell#97>”, line 1, in <module> mytuple[1]=2
TypeError: ‘tuple’ object does not support item assignment

Q.3 What is the purpose of PYTHONSTARTUP, PYTHONCASEOK, PYTHONHOME & PYTHONPATH environment variables?

  • PYTHONSTARTUP − It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as in Unix and it contains commands that load utilities or modify PYTHONPATH.
  • PYTHONCASEOK − It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.
  • PYTHONHOME − It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.
  • PYTHONPATH − It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.

Q.4 Explain Inheritance in Python with an example.

When one class inherits from another, it is said to be the child/ derived/sub class inheriting from the parent/base/super class. It inherits/gains all members (attributes and methods). Inheritance lets us reuse our code, and also makes it easier to create and maintain applications.

Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability,makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived/child class.

  • They are different types of inheritance supported by Python:
  • Single Inheritance — where a derived class acquires the members of a single super class.
  • OR
  • Single Inheritance- A class inherits from a single base class.
  • Multi-level inheritance — a derived class d1 in inherited from base class base1, and d2 is inherited from base2.
  • OR
  • Multilevel Inheritance- A class inherits from a base class, which in turn, inherits from another base class.
  • Hierarchical inheritance — from one base class you can inherit any number of child classes
  • OR
  • Hierarchical Inheritance- Multiple classes inherit from a single base class.
  • Multiple inheritance — a derived class is inherited from more than one base class.
  • OR
  • Multiple Inheritance- A class inherits from multiple base classes.
  • Hybrid Inheritance- Hybrid inheritance is a combination of two or more types of inheritance.

Q.5 What is Hierarchical Inheritance?

The concept of inheriting the properties from one class into multiple classes separately is known as hierarchical inheritance.


class x(object):    
def m1(self):
print("in m1 of x")
class y(x):
def m2(self):
print("in m2 of y")
class z(x):
def m3(self):
print("in m3 of z")


M m1 of XIn m2 of Y2337815In m1 of XIn m3 of Z2099735

Q.6 Suppose class C inherits from classes A and B as class C(A,B).Classes A and B both have their own versions of method func(). If we call func() from an object of class C, which version gets invoked?

Ans. In our article on Multiple Inheritance in Python, we discussed Method Resolution Order (MRO). C does not contain its own version of func(). Since the interpreter searches in a left-to-right fashion, it finds the method in A, and does not go to look for it in B.

Q.7 Which methods/functions do we use to determine the type of instance and inheritance?

Ans. Here, we talk about three methods/functions- type(), isinstance() and issubclass().

a. type(): This tells us the type of object we're working with.


<class ‘int’>


<class ‘bool’>

type(lambda :print("Hi"))

<class ‘function’>


<class ‘type’>

b. isinstance()

This takes in two arguments- a value and a type. If the value is of the kind of the specified type, it returns True. Else, it returns False.







c. issubclass()

This takes two classes as arguments. If the first one inherits from the second, it returns True. Else, it returns False.

class A: pass class B(A): pass issubclass(B,A)




Q.8 Write a one-liner that will count the number of capital letters in a file. Your code should work even if the file is too big to fit in memory.

Let us first write a multiple line solution and then convert it to one liner code. 
pywith open(SOME_LARGE_FILE) as fh:
count = 0
text = character in text:
if character.isupper():
count += 1```

Q.9 Write a sorting algorithm for a numerical dataset in Python.

The following code can be used to sort a list in Python:
pylist = ["1", "4", "0", "6", "9"]
list = [int(i) for i in list]

Q.10 How will you remove last object from a list?

list.pop(obj=list[-1]) − Removes and returns last object or obj from list.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Umair

Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python