Python Viva Questions and Answers
1. What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its simplicity and
readability.
2. List some features of Python.
- Easy to learn and read
- Interpreted language
- Dynamically typed
- Supports object-oriented and functional programming
- Large standard library
3. What are Pythons key data types?
- int, float, str, bool, list, tuple, set, dict, NoneType
4. What is the difference between a list and a tuple?
- List: Mutable
- Tuple: Immutable
5. What is a dictionary in Python?
A collection of key-value pairs. Example: {'name': 'John', 'age': 25}
6. How is Python an interpreted language?
Python code is executed line-by-line by the Python interpreter.
7. What is indentation and why is it important in Python?
Python Viva Questions and Answers
Indentation defines blocks of code. It is required in Python to indicate a block.
8. How do you take user input in Python?
Using input() function: name = input('Enter your name: ')
9. What is the use of type() function?
Returns the data type of an object. Example: type(5) returns <class 'int'>
10. What is the difference between == and is?
== compares values, is compares object identities.
11. What are Python functions? How do you define one?
Functions are reusable blocks defined using def. Example:
def greet():
print('Hello')
12. What are *args and **kwargs?
*args allows variable number of positional args, **kwargs for keyword args.
13. What is a lambda function?
An anonymous, one-line function. Example: lambda x: x * x
14. What are Python modules and packages?
Modules are .py files, packages are directories with __init__.py containing modules.
Python Viva Questions and Answers
15. What is the purpose of if __name__ == '__main__'?
Ensures code runs only when script is executed directly.
16. What are exceptions? Name a few common ones.
Runtime errors like ZeroDivisionError, ValueError, etc.
17. How do you handle exceptions in Python?
Using try-except blocks.
18. What is a class in Python?
A blueprint for objects. Example:
class Person:
def __init__(self, name):
self.name = name
19. What is inheritance? Give an example.
Allows a class to inherit from another. Example:
class Dog(Animal): pass
20. What are instance and class variables?
Instance: unique to object. Class: shared among all instances.
21. What are decorators?
Functions that modify the behavior of other functions.
Python Viva Questions and Answers
22. What is a generator? How is it different from a function?
Uses yield to return values one at a time, saving memory.
23. What are iterators in Python?
Objects with __iter__ and __next__ methods.
24. What is the difference between deep copy and shallow copy?
Shallow copies references, deep copy copies all nested objects.
25. What is list comprehension? Give an example.
Short syntax to create lists. Example: [x*x for x in range(5)]
26. Explain garbage collection in Python.
Automatic memory cleanup using reference counting and GC.
27. How does Python manage memory?
Uses private heap space and built-in garbage collector.
28. What are Python's built-in data structures?
List, Tuple, Set, Dictionary
29. What are the differences between Python 2 and Python 3?
Python 3 has print() as function, better Unicode support, different integer division.
30. What are Python virtual environments and why are they useful?
Python Viva Questions and Answers
They isolate project dependencies, avoiding conflicts.