0% found this document useful (0 votes)
6 views

PYTHON

Uploaded by

hrb.raj01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PYTHON

Uploaded by

hrb.raj01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is a Python generator?

Answer: A generator is a function that returns an iterator and produces items lazily using the
yield keyword.

2. How do you create a virtual environment in Python?

Answer: Use python -m venv environment_name.

3. Which module is used to perform deep copying of objects?

Answer: copy module with copy.deepcopy() function.

4. What is the purpose of __name__ == "__main__" in Python?

Answer: It ensures that code runs only when the file is executed directly, not when imported.

5. What is a Python decorator?

Answer: A decorator is a function that modifies the behavior of another function or class.

6. How can you read a file line by line in Python?

Answer: Using for line in file: or readline() method.

7. What is the use of the with statement?

Answer: It simplifies resource management like file handling by ensuring automatic cleanup.

8. Which module is used for regular expressions in Python?

Answer: The re module.

9. How do you compile a regular expression in Python?

Answer: Using re.compile() method.

10. What does the map() function do?

Answer: It applies a function to every item in an iterable and returns a map object.

11. What does the filter() function do?

Answer: It filters elements from an iterable based on a condition (function).

12. How do you create anonymous functions in Python?

Answer: Using the lambda keyword.

13. What is the difference between is and == in Python?

Answer: is checks for object identity, while == checks for equality.

14. What is Python’s garbage collector?


Answer: It automatically manages memory and reclaims unused objects.

15. Which module provides the garbage collection functionality?

Answer: The gc module.

16. What is a list comprehension?

Answer: It is a concise way to create lists using [expression for item in iterable].

17. What is the use of the zip() function?

Answer: It combines multiple iterables element-wise.

18. What does the enumerate() function do?

Answer: It returns an index and the corresponding value in an iterable.

19. How do you create a Python set?

Answer: Using {} or the set() function.

20. What is a Python frozenset?

Answer: It is an immutable version of a set.

21. What does the super() function do in Python?

Answer: It calls methods of a parent class.

22. How do you handle exceptions in Python?

Answer: Using try, except, else, and finally blocks.

23. What is the difference between raise and assert?

Answer: raise manually throws an exception, while assert checks conditions and raises
AssertionError.

24. How do you open a file in binary mode?

Answer: Using open(file_name, "rb").

25. What is the use of Python’s sys.argv?

Answer: It is used to access command-line arguments passed to a script.

26. How do you measure the execution time of code in Python?

Answer: Using the time module or timeit module.


27. What is a metaclass in Python?

Answer: A metaclass defines the behavior and creation of a class.

28. How do you perform multithreading in Python?

Answer: Using the threading module.

29. What is the Global Interpreter Lock (GIL) in Python?

Answer: GIL ensures only one thread runs Python bytecode at a time.

30. How can you achieve multiprocessing in Python?

Answer: Using the multiprocessing module.

31. What is a Python module?

Answer: A file containing Python code (functions, classes, or variables).

32. What is a Python package?

Answer: A directory containing an __init__.py file and multiple modules.

33. What are Python's built-in data types?

Answer: int, float, str, bool, list, tuple, dict, set, and NoneType.

34. How do you reverse a list in Python?

Answer: Using list.reverse() or slicing list[::-1].

35. What is a Python iterator?

Answer: An object that implements the __iter__() and __next__() methods.

36. What is the difference between a list and a tuple?

Answer: Lists are mutable, while tuples are immutable.

37. What does the del statement do?

Answer: It deletes objects or variables.

38. What is the purpose of the id() function?

Answer: It returns the memory address of an object.

39. What is Python’s @property decorator used for?

Answer: It makes a method behave like a property (getter).

40. How do you convert a string to a list in Python?

Answer: Using list() or the split() method.


41. How do you handle JSON data in Python?

Answer: Using the json module with json.loads() and json.dumps().

42. What is the difference between deepcopy and copy?

Answer: copy() creates a shallow copy, while deepcopy() creates a deep copy.

43. What is the purpose of itertools in Python?

Answer: It provides efficient tools for working with iterators.

44. What is the difference between a function and a method?

Answer: A method is a function defined inside a class.

45. How do you handle time zones in Python?

Answer: Using the datetime and pytz modules.

46. How do you run external commands from a Python script?

Answer: Using the subprocess module.

47. What is the use of the hash() function?

Answer: It returns the hash value of an object (used in sets and dictionaries).

48. What is a singleton class in Python?

Answer: A class that allows only one instance to exist.

49. How do you serialize and deserialize objects in Python?

Answer: Using the pickle module.

50. What is the difference between mutable and immutable objects?

Answer: Mutable objects can be modified after creation (e.g., lists), while immutable objects
cannot (e.g., tuples, strings)

Here are 30 long questions and answers on Advanced Python Programming:

1. What are Python decorators, and how do they work?

Answer:
A decorator is a function that modifies the behavior of another function or method. It allows
us to wrap another function to add functionality without modifying the original function’s
code.

How they work:


 Decorators take a function as an argument and return a new function.
 They use the @decorator_name syntax before the function definition.

2. Explain Python's garbage collection mechanism.

Answer:
Python's garbage collector automatically reclaims memory occupied by unused objects. It
uses reference counting and a cyclic garbage collector to manage memory.

3. What is the Global Interpreter Lock (GIL), and how does it affect multithreading?

Answer:
The Global Interpreter Lock (GIL) is a mutex in CPython that allows only one thread to
execute Python bytecode at a time, even in multi-core systems.

How it affects multithreading:

 The GIL ensures thread-safety but prevents true parallel execution of threads.
 In CPU-bound tasks, the GIL can be a bottleneck, as threads cannot execute
simultaneously.

4. Explain the with statement in Python. Why is it used?

Answer:
The with statement simplifies resource management, such as opening and closing files. It
ensures resources are properly cleaned up, even if an exception occurs.

5. What are Python generators, and how are they different from lists?

Answer:
A generator is a function that produces values lazily, using the yield keyword, instead of
returning a list.

Key Differences:

1. Memory Efficient: Generators don’t store all values in memory, unlike lists.
2. On-Demand: Generators produce values one at a time.

6. What is metaprogramming in Python?

Answer:
Metaprogramming is writing code that manipulates other code, such as classes or functions,
at runtime.

Examples of metaprogramming:

1. Decorators: Modify the behavior of functions.


2. Metaclasses: Modify the behavior of class creation

7. What is the difference between a class method, static method, and instance method?

Answer:

1. Instance Method:
o Default method in a class.
o Uses self to access instance variables.
2. Class Method:
o Uses @classmethod decorator.
o Uses cls to access class variables.
3. Static Method:
o Uses @staticmethod decorator.
o Does not access instance or class variables.

8. What is the purpose of the subprocess module in Python?

Answer:
The subprocess module is used to execute external programs or commands from Python
scripts.

9. What are context managers in Python? Explain with an example.

Answer:
A context manager simplifies resource management. It uses __enter__ and __exit__ methods.

10. Explain the Python multiprocessing module.

Answer:
The multiprocessing module enables parallel execution of code by creating separate
processes.

You might also like