Python Interview Questions

1. Python Basics

  1. Write a Python program to reverse a string.
  2. How do you find the factorial of a number using recursion?
  3. Write a program to check if a number is prime.
  4. Find the second largest element in a list.
  5. Print only duplicate characters in a string.
  6. Explain difference between is and == in Python.
  7. What are Python decorators? Give a simple example.
  8. What is list comprehension? Write one example.
  9. Explain mutable vs immutable with examples.
  10. What are Python generators? Write a generator for Fibonacci numbers.

2. Python OOPs

  1. What is the difference between @staticmethod@classmethod, and normal methods?
  2. Write a class Car with attributes brand, model, year and a method to display details.
  3. Explain multiple inheritance in Python with example.
  4. What is method overriding? Give an example.
  5. Difference between __init__ and __new__.

3. File Handling & Data

  1. Write a Python program to read a text file and count word frequency.
  2. How to read/write JSON files in Python? Give an example.
  3. Write code to extract email addresses from a text using regex.
  4. Can Python read PDFs? Write sample code.
  5. How to connect Python with SQL database (example with SQLite/MySQL)?

4. Testing with Python (Pytest & Selenium)

  1. Write a pytest test case for login functionality with email & password.
  2. Why do we use fixtures in pytest? Write a fixture to open & quit a browser.
  3. Difference between implicit wait and explicit wait in Selenium.
  4. Python Selenium → Take a screenshot of a webpage & save it locally.
  5. Write code to handle dynamic elements using XPath in Selenium.
  6. How do you select a value from a dropdown in Selenium (Python)?
  7. Explain handling cookies in Selenium with Python.
  8. Use ActionChains in Selenium to drag & drop an element.
  9. Write Python code to verify if a file is downloaded (without relying on browser).
  10. Can we read a PDF opened in browser with Selenium + Python? How?

5. API Testing with Python

  1. Write Python code using requests library to send a GET request & print status code.
  2. Write a POST request with JSON payload & print the response.
  3. How do you send headers with a request in Python? Example.
  4. How to assert response body in Python API tests?
  5. Explain difference between response.text and response.json().

6. Advanced Python (Practice)

  1. Write a Python program to implement multithreading.
  2. Explain difference between multiprocessing and multithreading.
  3. Write a function with @lru_cache decorator and explain caching.
  4. Write a Python script to send an email.
  5. Explain how you would test an ML model with Python.

Leave a Comment

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