Python viva answers
Python viva answers
o Easy to learn, versatile, large standard library, strong community support, cross-
platform compatibility, and extensive use in AI, data science, and automation.
o Python is an interpreted language, meaning the code is executed line by line by the
Python interpreter.
o In a dynamically typed language, variable types are determined at runtime, and you
don’t need to declare the type of a variable explicitly.
o Mutable data types can be changed after creation (e.g., lists, dictionaries), while
immutable data types cannot be changed (e.g., strings, tuples).
o A dictionary stores data in key-value pairs and is unordered, while a list stores
ordered elements accessed by indices.
o Using try, except, finally, and raise statements to handle and manage errors.
15. What is the difference between a for loop and a while loop?
o A for loop iterates over a sequence, while a while loop continues as long as a
condition is true.
o Yes, functions are first-class objects in Python, so they can be passed as arguments to
other functions.
o Yes, indentation is required to define blocks of code, replacing the use of braces {} in
other languages.
o Variable scope defines where a variable can be accessed. Types include local (inside a
function), global (outside functions), and nonlocal (in nested functions).
21. What is the difference between break, continue, and pass in Python?
o break exits the loop, continue skips the current iteration, and pass is a placeholder
that does nothing.
o No, because tuples are immutable. However, you can use generator expressions.
28. What is the difference between a shallow copy and a deep copy?
o A shallow copy creates a new object but references the same elements, while a deep
copy creates a new object with new elements.
29. Which sorting technique is used by sort() and sorted() functions in Python?
o Timsort, a hybrid sorting algorithm derived from merge sort and insertion sort.
o Decorators are functions that modify the behavior of other functions or methods.
o Iterators are objects that allow traversal through all elements of a collection,
implementing __iter__() and __next__().
o Generators are functions that yield values lazily using the yield keyword.
o Yes, Python supports multiple inheritance, where a class can inherit from more than
one parent class.
o Polymorphism allows methods to behave differently based on the object that calls
them.
o Encapsulation is the bundling of data and methods that operate on the data within a
single unit (class).
37. How do you achieve data abstraction in Python?
o Using os.remove("filename").
o PIP is the package installer for Python, used to install and manage libraries.
o The zip() function combines multiple iterables into a single iterable of tuples.
o Pickling is the process of converting Python objects into a byte stream, and
unpickling is the reverse process.
45. What is the difference between @classmethod, @staticmethod, and instance methods?
o @classmethod works with the class, @staticmethod doesn’t depend on the class or
instance, and instance methods work with the instance.
python
Copy
print(datetime.now())
o Unit tests are used to test individual components of a program, typically using
the unittest module.
o The GIL is a mutex that prevents multiple threads from executing Python bytecode
simultaneously.
o Function annotations provide metadata about the types of arguments and return
values.
o Python uses a private heap for memory management and a garbage collector to
reclaim unused memory.
o Stack: Use a list with append() and pop(). Queue: Use collections.deque.
o is checks for identity (same object), while == checks for equality (same value).
o The with statement is used for resource management, ensuring proper acquisition
and release of resources.
58. What is the difference between deep and shallow copy in Python?
o Shallow copy creates a new object but references the same elements, while deep
copy creates a new object with new elements.
o JSON is a text-based format for serialization, while Pickle is a binary format specific
to Python.
63. What is the difference between import module and from module import function?
o import module imports the entire module, while from module import
function imports a specific function.
o Duck typing is a concept where the type of an object is determined by its behavior
rather than its class.
o Python 3 has better Unicode support, syntax improvements, and removed redundant
constructs.
o Using libraries like Pandas, you can use fillna(), dropna(), or interpolation.
o Python supports functional programming with features like lambda functions, map,
filter, and reduce.
o The enum module provides support for enumerations, which are sets of symbolic
names bound to unique values.
75. What is the difference between the sorted() function and the sort() method?
o sorted() returns a new sorted list, while sort() sorts the list in place.
o Python uses the threading module, but due to the GIL, it is not suitable for CPU-
bound tasks.
o The os module provides functions for interacting with the operating system,
while sys provides access to system-specific parameters.
o itertools is a module that provides functions for creating iterators for efficient
looping.
o Counter counts hashable objects, while defaultdict provides default values for
missing keys.
o time.sleep() pauses the entire program, while threading.sleep() pauses only the
current thread.
o By creating test cases that inherit from unittest.TestCase and using methods
like assertEqual().
o The sqlite3 module provides an interface for working with SQLite databases.
o The subprocess module allows you to spawn new processes and interact with them.
Miscellaneous Questions
103. What role does Python play in AI and data science in India?
- Python is the leading language for AI and data science due to its extensive libraries like
TensorFlow, PyTorch, and Pandas.
104. How can Python help in developing software solutions for Indian businesses?
- Python can automate processes, analyze data, and build scalable web applications for
businesses.
105. What are the career prospects for Python programmers in India?
- Python programmers are in high demand in fields like software development, data science,
AI, and automation.
107. How does Python compare with Java in terms of popularity in India?
- Python is more popular for data science and AI, while Java is widely used in enterprise
applications.
108. What are some popular Python communities and meetups in India?
- PyCon India, Python Pune, and Bangalore Python User Group are popular communities.