Viva Questions
Important questions and answers for each unit.
Q1. What is Python? List its advantages.
Show Answer
Answer:
Python is a high-level, interpreted, dynamically-typed programming language created by Guido van Rossum in 1991.
Advantages:
- Easy to learn and read (simple syntax)
- Interpreted language (no compilation)
- Platform-independent (write once, run anywhere)
- Large standard library
- Supports multiple programming paradigms (OOP, procedural, functional)
- Dynamic typing
- Extensive third-party modules
- Good community support
Q2. What's the difference between a list and tuple?
Show Answer
Answer:
| Feature | List | Tuple |
|---|---|---|
| Mutability | Mutable (can be changed) | Immutable (cannot be changed) |
| Syntax | [1, 2, 3] | (1, 2, 3) |
| Performance | Slower | Faster |
| Dictionary Keys | Cannot be used | Can be used |
| Memory | More memory | Less memory |
Q3. Explain the difference between == and is operators.
Show Answer
Answer:
- == compares the VALUES of two objects
- is compares if two objects are the SAME OBJECT in memory
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True (same values)
print(a is b) # False (different objects)
print(a is c) # True (same object reference)
Q4. What is a lambda function?
Show Answer
Answer:
Lambda functions are small, anonymous functions defined with the lambda keyword.
Syntax: lambda arguments: expression
square = lambda x: x ** 2
print(square(5)) # 25
# Used with map, filter, sorted
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
Advantages:
- Useful for small, one-time operations
- Can be used with higher-order functions
- Reduces code verbosity
Q5. What is the difference between append() and extend()?
Show Answer
Answer:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# append() - adds entire object as single element
list1.append(list2)
print(list1) # [1, 2, 3, [4, 5, 6]]
# extend() - adds individual elements
list1 = [1, 2, 3]
list1.extend(list2)
print(list1) # [1, 2, 3, 4, 5, 6]
Q6. Explain the concept of inheritance in OOP.
Show Answer
Answer:
Inheritance is a mechanism to create new classes from existing classes, allowing code reuse and establishing relationships.
class Animal:
def speak(self):
return "Sound"
class Dog(Animal):
def speak(self):
return "Bark"
class Cat(Animal):
def speak(self):
return "Meow"
dog = Dog()
cat = Cat()
print(dog.speak()) # Bark
print(cat.speak()) # Meow
Types of inheritance:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
Q7. What is polymorphism? Give examples.
Show Answer
Answer:
Polymorphism means "many forms." It allows objects to take on multiple forms and enables using objects of different classes interchangeably.
Method Overriding (Runtime Polymorphism):
class Dog:
def speak(self):
return "Woof"
class Cat:
def speak(self):
return "Meow"
def make_sound(animal):
print(animal.speak())
make_sound(Dog()) # Woof
make_sound(Cat()) # Meow
Operator Overloading:
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
Q8. What is a decorator?
Show Answer
Answer:
A decorator is a function that modifies or enhances another function or class without permanently changing its source code.
def my_decorator(func):
def wrapper():
print("Before calling function")
func()
print("After calling function")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
# Output:
# Before calling function
# Hello!
# After calling function
Q9. Explain exception handling. What is try-except?
Show Answer
Answer:
Exception handling allows graceful error management in programs.
try:
# Code that might raise an exception
num = int(input("Enter a number: "))
result = 10 / num
except ValueError:
print("Please enter a valid number")
except ZeroDivisionError:
print("Cannot divide by zero")
except Exception as e:
print(f"An error occurred: {e}")
else:
print("No error occurred")
finally:
print("This always executes")
Common exceptions:
- ValueError, TypeError, ZeroDivisionError
- IndexError, KeyError, AttributeError
- FileNotFoundError, IOError
Q10. What is the difference between == and is?
Show Answer
Answer:
- == checks for VALUE equality
- is checks for IDENTITY (same object in memory)
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x == y) # True (same values)
print(x is y) # False (different objects)
print(x is z) # True (same reference)
Q11. What is slicing? Give examples.
Show Answer
Answer:
Slicing is a technique to extract a portion of a sequence using the slice operator [start:end:step].
s = "Python"
list = [1, 2, 3, 4, 5]
# Basic slicing
print(s[0:2]) # Py (start=0, end=2)
print(list[1:4]) # [2, 3, 4]
# With step
print(s[::2]) # Pto (every 2nd character)
print(list[::2]) # [1, 3, 5]
# Negative indices
print(s[-3:]) # hon (last 3 characters)
print(list[:-2]) # [1, 2, 3]
# Reverse
print(s[::-1]) # nohtyP
print(list[::-1]) # [5, 4, 3, 2, 1]
Q12. What is *args and **kwargs?
Show Answer
Answer:
*args: Allows passing variable number of non-keyword arguments
def add(*args):
return sum(args)
print(add(1, 2, 3, 4, 5)) # 15
**kwargs: Allows passing variable number of keyword arguments
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="John", age=25, city="NYC")
Both together:
def func(*args, **kwargs):
print("args:", args)
print("kwargs:", kwargs)
func(1, 2, 3, name="John", age=25)
Q13. What is a generator?
Show Answer
Answer:
A generator is a function that yields values one at a time instead of returning all at once. It's memory efficient.
def count_up(n):
i = 1
while i <= n:
yield i
i += 1
for num in count_up(5):
print(num) # 1, 2, 3, 4, 5
Fibonacci using generators:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
fib = fibonacci(8)
print(list(fib)) # [0, 1, 1, 2, 3, 5, 8, 13]
Q14. Explain the concept of scope in Python.
Show Answer
Answer:
Scope determines the visibility and accessibility of variables.
Types of scope (LEGB rule):
- Local: Inside function
- Enclosing: In outer function (nested functions)
- Global: Outside all functions
- Built-in: Python built-in scope
x = 10 # Global
def outer():
y = 20 # Enclosing (for inner)
def inner():
z = 30 # Local
print(x, y, z) # Can access all
inner()
outer()
Q15. What are the differences between pass, break, and continue?
Show Answer
Answer:
| Statement | Purpose | Example |
|---|---|---|
| pass | Null operation (do nothing) | if condition: pass |
| break | Exit the loop immediately | if i == 5: break |
| continue | Skip current iteration | if i % 2 == 0: continue |
# pass example
for i in range(5):
pass # Do nothing
# break example
for i in range(10):
if i == 5:
break
print(i) # Prints 0 to 4
# continue example
for i in range(5):
if i == 2:
continue
print(i) # Prints 0, 1, 3, 4
Unit-Wise Viva Questions
Access detailed viva questions organized by unit.
Unit 1: Introduction to Python
Intro, syntax, variables, data types, operators, I/O, core data structures, C→Python transition
View QuestionsUnit 2: Control Flow and Functions
Conditionals, loops, defining functions, parameters/returns, scope and lifetime
View QuestionsUnit 3: File Handling, Packaging and Debugging
File read/write, exception handling, modules/packages, executables, error types, debugging
View QuestionsUnit 4: Object Oriented Programming (OOP) in Python
Classes/objects, encapsulation, inheritance (single/multiple/multilevel), polymorphism, constructors
View QuestionsUnit 5: Advanced Python Concepts
Regular expressions, pattern matching, regex functions, GUI development with Python frameworks
View QuestionsUnit 6: Python Libraries
Overview of popular libraries: NumPy (numerical), Pandas (data), Matplotlib (visualization)
View QuestionsDownload Viva Question PDFs
Download complete viva questions with answers for each unit in PDF format. PDFs coming soon!
| Unit Name | Questions | Download |
|---|---|---|
| Unit 1: Introduction to Python | 30 Questions | Download |
| Unit 2: Control Flow and Functions | 25 Questions | Download |
| Unit 3: File Handling, Packaging and Debugging | 28 Questions | Download |
| Unit 4: Object Oriented Programming (OOP) in Python | 30 Questions | Download |
| Unit 5: Advanced Python Concepts | 32 Questions | Download |
| Unit 6: Python Libraries | 26 Questions | Download |
| Complete Viva Question Bank | 171 Questions | Download individual units above |