Skip to main content

Python interview questions. Part II. Middle



Questions

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

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

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

Q: Why shouldn't you make the default arguments an empty list?

Q: What id() function in Python is for?

Q: What is the yield keyword used for in Python?

Q: What is an iterator in Python? Can you write an example?

Q: What is a generator in Python? How are they different from iterators?

Q: What is the difference between __iter__ and __next__?

Q: How do you create a dictionary that can preserve the order of pairs?

Q: What is a context manager? How are they different from try ... finally?

Q: Which functions must be overridden in a class in order for its instances to implement the context manager protocol?

Q: What is the synchronous code? What is asynchronous code? How to write asynchronous code?

Q: What is unittest module in Python? How to write tests in Python?

Q: What is type checking? Why Python is a strongly typed language? Do we have types in Python?

Q: How can you copy an object in Python? How to make a deep copy?

Q: How memory is managed in Python? Why garbage collector exists in Python?

Q: How the garbage collector works in Python? Describe Python's garbage collection mechanism in brief.

Q: How can you share global variables across modules? Is it a good idea to do that?

Q: What is the __slots__ attribute used in a class for?

Q: What are metaclasses in Python?

Q: How to create a class without a class statement?

Code involving questions

Q: How to check that one tuple contains all elements of another tuple?

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

>>> list = ['a', 'b', 'c', 'd', 'e']
>>> print(list[10:]) # []

Q: How can I reload a previously imported module? (we assume that the module is a file module.py)

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

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

Q: What's wrong with the following code?

def foo():
    from .module import *
    print(f"{bar()}")

Q: The file is located in /usr/lib/python/person.py. The program is run with python /usr/lib/python/person.py. What will the output be?

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

    def getAge(self):
        print(__name__)

p = Person("John")
p.getAge()

Q: Write a timeit decorator to measure the time of function execution.

Q: Write a decorator that will catch errors and repeat the function a maximum of 3 times (configurable).

Q: What's the output of the following code?

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: Fix the following code to make it work.

class Repeater:
    ...
class RepeaterIterator:
    ...

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

Q: Write code to get unique values from a list of complex types (custom classes). Example: [A(1, "ab"), A(2, "ab"), A(2, "aa"), A(1, "ab)] -> [A(1, "ab"), A(2, "ab"), A(2, "aa")].

Q: We have the following code with the unknown function f(). In f(), we do not want to use a return, instead, we may want to use a 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: What's the output of the following code?

x = [[0], [1]] 

print(len(' '.join(list(map(str, x))))) 



Comments

Popular posts from this blog

Razormet Technology Internship 2024 For Junior Data Analyst | Work From Home

  Razormet Technology Internship 2024:  Razormet Technology Internship 2024 for its Junior Data Analyst. Razormet Technology Drive organizes off-campus drives for candidates in any batch. To apply, candidates should have a Bachelor’s degree in mathematics, engineering, economics, statistics, computer science, information management, or a related field. if you are interested, please apply as soon as possible. Company Name: Razormet Technology Role: Junior Data Analyst Location: Remote Experience: 0 – 1 years Qualification: Bachelor’s degree in mathematics, engineering, economics, statistics, computer science, information management or related field Batch: Any Job Type: Full Time Salary: INR 400,000 – 500,000 Razormet Technology Internship 2024 Job Description: As an Associate Data Analyst, you will become a subject-matter expert in automotive industry data and analysis. You will develop a working understanding of multiple data flows from ingestion to application, directly impac...

IBM Hiring Fresher Procurement Purchase Executive

At IBM, work is more than a job – it’s a calling: To build. To design. To code. To consult. To think along with clients and sell. To make markets. To invent. To collaborate. Not just to do something better, but to attempt things you’ve never thought possible. Are you ready to lead in this new era of technology and solve some of the world’s most challenging problems? If so, let's talk. IBM  looking for passionate fresher graduates with excellent communication skills, good Customer Service skills, and Interpersonal Skills for the role of  Procurement Purchase Executive. Job Designation: Procurement Purchase Executive Qualification:  Bachelor’s Degree Experience:  Freshers / 0 – 1 years Skill Set : Good Customer Service skills and Interpersonal Skills. Proficient working knowledge of MS Office. Familiar with procurement and purchase policies. Problem Solving – Identifies and resolves problems promptly; Gathers and analyses information skillfully. Customer Service – Mana...

Razormet Technologies Hiring Drive 2024 For Data Analyst / Analysis | Work From Home

Razormet Technologies Hiring Drive 2024:  Razormet Technologies Hiring Drive 2024 for its Data Analyst / Analysis. Razormet Technologies Drive organizes off-campus drives for candidates in the 2024 batch. To apply, candidates should have a Bachelor’s degree in Business Analytics, Computer Science, or a related field. if you are interested, please apply as soon as possible. Company Name: Razormet Technologies Role: Data Analyst / Analysis Location: Remote Experience: 0-1 years Qualification: Bachelor’s degree in Business Analytics, Computer Science, or a related field Batch: 2024 Job Type: Full Time Salary: INR 4.0 – 5.0 Razormet Technologies Hiring Drive 2024 Job Description: In this role, you will work with the Lead Data Engineer to serve as the technical experts within the Business Intelligence & Analytics group to support firm-wide reporting needs. A strong background in DOMO combined with writing complex R scripts is highly valued. An ideal candidate is adept at integrating...