Python interview questions. Part II. Middle

Python interview questions. Part II. Middle


Q: What is the output: -12 % 10?

Q: What is the output: -12 // 10?

Q: What is the sequence of call operators in the expression a ** b ** c?

Q: What are the steps required to make a script executable on Unix?

Q: What is an iterator? What is a generator? What is the difference between them?

Q: Why shouldn't the default argument be to make an empty list?

Q: Let A and B be objects of class Foo. What functions and in what order is called when print(A() + B()) is executed?

Q: Is there an assignment operation in python?

Q: How are arguments passed by value or by reference?

Q: How are Dict and Set implemented internally?

Q: How memory is managed in Python?

Q: How garbage collector works in Python?

Q: How can you copy an object in Python? / What is the difference between deep and shallow copy?

Q: Can we write a multithreaded application in Python?

Q: What is GIL? Why GIL is still existing?

Q: Difference between multithreading and multiprocessing libraries in Python?

Q: What is a decorator? How to create custom decorator?

Q: What is map, filter, and reduce operations in Python?

Q: What is globals(), locals() and vars()?

Q: What is context managers in Python? How are they different from try ... finally?

Q: What functions need to be redefined in class so that its instances implement the context manager protocol?

Q: Does Python fully support OOP? How to create a private attribute in Python object?

Q: What are dunder methods in Python?

Q: What are __init__ functions?

Q: How to call a constructor method in Python?

Q: What is __dict__ attribute of Python object? Make an example.

Q: How can you share global variables across modules?

Q: You have a function that takes other functions as an argument. How to validate the value of an argument?

Q: You need to implement a function that should use a static variable (for example, a call counter). You cannot write any code outside the function and you do not have information about external variables (outside your function). How to do it?

Q: What is a descriptor? What is a decorator? Is there a difference between a descriptor and a decorator?

Code involving questions:

Q: What will be the output of the following code?

>>> a = [[]]*3
>>> a[1].append(1)
>>> print(a)

Q: What's wrong with this code?

def foo():
     from .module import *

Q: The file is located in /usr/lib/python/person.py. The program starts as python /usr/lib/python/person.py. What is the output?

class Person:
    def __init__(self, name):
        __name__ = name

    def getAge(self):

p = Person('John')

Q: Write timeit decorator for measure time of function execution.

Q: Write repeater decorator that will catch errors several times(customizable).

Q: Output?

class parent:
     def __init__(self, param):
         self.v1 = param

class child:
     def __init__(self, param):
         self.v2 = param

obj = child(11)
print(obj.v1 + " " + obj.v2)

Q: Add some code to make it work

class Repeater:
class RepeaterIterator:

repeater =Repeater('Hello')
for I in repeater:
    print (i)  # hello

Q: Write the code for getting unique values from list complex types.

Q: We have the following code with the unknown function f(). In f(), we do not want to use return, instead, we may want to use generator.

for x in f(5):
    print x,

The output looks like this:

0 1 8 27 64

Write a function f() so that we can have the output above.

Q: Output?

x = [[0], [1]]
print(len(' '.join(list(map(str, x)))))

Q: Write a program that prints the numbers from 1 to 20. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Q: Output? What is wrong? How to fix?

class Developer(object):
    coffee_cups = 0

    def __init__(self, name):
        self.name = name
        self.coffee_cups += 1

    def speak(self):

        print("I'm %s and I've had %d cups of coffee today" % (self.name, self.coffee_cups))

rover = Developer("Steve")
spot = Developer("Bob")


Python interview questions. Part I. Junior


Python interview questions. Part III. Senior

Support author