0% found this document useful (0 votes)
3 views7 pages

python interview question

Uploaded by

Boy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views7 pages

python interview question

Uploaded by

Boy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Basic Python Questions

1. What are Python's key features?


○ Answer: Python is interpreted, dynamically-typed, object-oriented, has extensive
libraries, and supports multiple paradigms like procedural, functional, and
object-oriented programming.
2. What are Python’s applications?
○ Answer: Web development, data analysis, AI/ML, desktop apps, scripting,
automation, and scientific computing.
3. How is Python interpreted?
○ Answer: Python code is executed line-by-line by the Python interpreter.
4. What are Python’s data types?
○ Answer: int, float, str, bool, list, tuple, dict, set, NoneType.
5. What is PEP 8?
○ Answer: Python Enhancement Proposal 8 is a style guide for Python code.

Intermediate Python Questions

6. What is a Python decorator?


○ Answer: A decorator is a function that modifies another function or class,
typically used for logging, access control, or memoization.
7. Differentiate between is and ==.
○ Answer: is checks object identity; == checks value equality.
8. What is the difference between mutable and immutable data types?
○ Answer: Mutable types (e.g., list, dict) can change after creation; immutable
types (e.g., tuple, str) cannot.
9. Explain Python’s Global Interpreter Lock (GIL).
○ Answer: GIL ensures only one thread executes Python bytecode at a time,
affecting multithreaded performance.
10. What is the difference between deep copy and shallow copy?
○ Answer: A shallow copy copies the outer object, while a deep copy copies both
the outer and nested objects.

Advanced Python Questions

11. What is Python's with statement?


○ Answer: Used for resource management (e.g., file handling) to ensure resources
are released after use.
12. How is memory managed in Python?
○ Answer: Python uses automatic memory management with reference counting
and garbage collection.
13. What are Python’s key libraries for data science?
○ Answer: NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow.
14. Explain list comprehensions.
○ Answer: A concise way to create lists: [expression for item in
iterable if condition].
15. What is the difference between @staticmethod and @classmethod?
○ Answer: @staticmethod does not take self or cls as an argument.
@classmethod takes cls and can modify the class state.

Coding Questions
Write a Python program to reverse a string.

def reverse_string(s):
return s[::-1]
print(reverse_string("Python"))

Write a Python program to check if a number is prime.


def is_prime(n):

if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print(is_prime(7))

Write code to find the largest element in a list.


lst = [10, 20, 30]

print(max(lst))

Write code to find the factorial of a number.


def factorial(n):

return 1 if n == 0 else n * factorial(n - 1)


print(factorial(5))
Write a Fibonacci sequence generator.
def fibonacci(n):

a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(5)

Write a Fibonacci sequence generator

def fibonacci(n):

a, b = 0, 1

for _ in range(n):

print(a, end=" ")

a, b = b, a + b

fibonacci(5)

OOPs in Python

21. What is Python’s OOPs support?


○ Answer: Python supports classes, objects, inheritance, polymorphism,
encapsulation, and abstraction.
22. What is __init__?
○ Answer: A constructor method that initializes object attributes.
23. Explain multiple inheritance in Python.
○ Answer: A class can inherit attributes/methods from multiple classes.
24. What are __str__ and __repr__?
○ Answer: __str__ is user-friendly; __repr__ is for developers and debugging.
25. What is method overriding?
○ Answer: A subclass provides a specific implementation of a method already
defined in its superclass.
Error Handling
How is error handling done in Python?
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
finally:
print("Done.")

What is the difference between try and finally?

Answer: finally always executes, regardless of an exception.

How can you raise an exception in Python?

raise ValueError("Invalid input!")

What is the assert statement used for?

Answer: For debugging by checking a condition.

What is the else block in try-except?

○ Answer: Executes only if no exception occurs.

File Handling
How do you read/write a file in Python?
python
Copy code
with open("file.txt", "r") as file:
data = file.read()
with open("file.txt", "w") as file:
file.write("Hello!")

31.
32. Explain modes in Python file handling.
○ Answer: r (read), w (write), a (append), rb/wb (binary).
How to check if a file exists?
python
Copy code
import os
print(os.path.exists("file.txt"))

33.

How to copy a file?


python
Copy code
import shutil
shutil.copy("source.txt", "destination.txt")

34.
35. What is the difference between read() and readlines()?
○ Answer: read() returns the entire content as a string, while readlines()
returns a list of lines.

Miscellaneous

36. What is Python's lambda function?


○ Answer: A single-line anonymous function: lambda x: x + 1.
37. What are Python’s *args and **kwargs?
○ Answer: *args handles variable positional arguments, **kwargs handles
keyword arguments.
38. What is Python’s GIL?
○ Answer: Ensures only one thread executes Python bytecode at a time.

How to sort a list in Python?


python
Copy code
lst = [3, 1, 2]
lst.sort()
print(lst)

39. What is a generator in Python?


○ Answer: A function that yields items using yield and is memory efficient.
Data Structures

What is a tuple? How is it different from a list?

Answer: Tuple is immutable, whereas a list is mutable.

How to implement a stack in Python?


python
Copy code
stack = []
stack.append(1)
stack.pop()

How to implement a queue in Python?


python
Copy code
from collections import deque
queue = deque()
queue.append(1)
queue.popleft()

What is the difference between a set and a dictionary?

Answer: A set stores unique items, while a dictionary stores key-value pairs.

How to merge two dictionaries?


python
Copy code
dict1 = {"a": 1}
dict2 = {"b": 2}
dict1.update(dict2)

Libraries and Frameworks

46. What is the difference between NumPy and Pandas?


○ Answer: NumPy handles numerical computations; Pandas handles tabular data.
47. What is Flask?
○ Answer: A lightweight web framework for Python.
48. How does Django differ from Flask?
○ Answer: Django is feature-rich and follows the "batteries-included" approach;
Flask is minimal and extensible.
49. What is TensorFlow?
○ Answer: A library for machine learning and deep learning.
50. Explain Python’s os and sys modules.
○ Answer: os handles file and directory operations, while sys provides
system-specific parameters and functions.

You might also like