PYTHON INTERVIEW QUESTIONS & ANSWERS
1. Basic Python
- Python is a high-level interpreted language.
- Key features: easy syntax, dynamic typing, portability.
- Data types: int, float, list, tuple, dict, set.
- List vs Tuple: list mutable, tuple immutable.
- == compares value, is compares memory.
- Indentation defines blocks.
- Keywords are reserved words.
- Variables store data.
- Dynamic typing: type decided at runtime.
- Mutable vs Immutable: list mutable, string immutable.
2. Data Structures
- List: ordered, mutable.
- Tuple: ordered, immutable.
- Dictionary: key-value pairs.
- Set: unique unordered elements.
- Remove duplicates: list(set(list)).
- List comprehension: [x*x for x in range(5)].
- Dict comprehension: {x:x*x for x in range(5)}.
- append adds one element, extend adds multiple.
- Dict uses hash table internally.
3. Functions
- Function is reusable code block.
- return gives value, print displays.
- Default arguments have preset values.
- Keyword arguments use names.
- *args: multiple positional args.
- **kwargs: multiple keyword args.
- Lambda: small anonymous function.
- Recursion: function calls itself.
- Generator: uses yield.
- yield returns values one by one.
4. OOP
- OOP uses objects and classes.
- Class is blueprint, object is instance.
- __init__ is constructor.
- Inheritance reuses code.
- Multiple inheritance uses multiple parents.
- Overriding: redefine method.
- Encapsulation: restrict access.
- Polymorphism: same method, different behavior.
- Abstraction hides details.
- staticmethod vs classmethod difference.
- self refers to instance.
5. Modules
- Module: .py file.
- Package: folder of modules.
- __name__ == "__main__": entry point.
- import methods: import, from import.
- [Link]: module search paths.
- pip: package manager.
6. Exception Handling
- Handles runtime errors.
- try-except-finally-else blocks.
- raise used to throw exception.
- Custom exception: user-defined.
- assert used for debugging.
7. File Handling
- open() to open file.
- Modes: r, w, a, rb.
- read(), readline(), readlines().
- write() to write data.
- with auto closes file.
8. Advanced Concepts
- Decorators modify functions.
- Iterator vs iterable.
- Generator uses yield.
- GIL allows one thread at a time.
- Multithreading vs multiprocessing.
- Async programming uses async/await.
- Coroutines are async functions.
- Monkey patching modifies code runtime.
9. Memory Management
- Uses heap memory.
- Garbage collection removes unused objects.
- Reference counting tracks usage.
- Memory leaks due to circular references.
10. Coding Questions (Examples)
- Reverse string: text[::-1]
- Factorial using loop or recursion.
- Palindrome check: text == text[::-1]
- Find duplicates using set or loop.
- Largest number using loop.
- Frequency count using dictionary.
- Sorting using bubble sort.
- Second largest using sorted list.
- Merge dict using **.
- Remove duplicates using set.