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

Important_Python_Questions_FULL

The document contains a compilation of important questions and answers related to Python programming for the MSBTE Summer 2025 course. It covers various topics including data structures, variable types, loops, functions, exception handling, modules, classes, and file operations. Each question is accompanied by explanations and example code snippets for clarity.

Uploaded by

milee1722
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)
13 views

Important_Python_Questions_FULL

The document contains a compilation of important questions and answers related to Python programming for the MSBTE Summer 2025 course. It covers various topics including data structures, variable types, loops, functions, exception handling, modules, classes, and file operations. Each question is accompanied by explanations and example code snippets for clarity.

Uploaded by

milee1722
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/ 4

MSBTE Python Programming (PWP) –

Summer 2025
Most Important Questions with Answers

MSBTE Python Programming (PWP) – Summer 2025


Most Important Questions with Answers

1. Differentiate between list and tuple with example.


List is mutable (can be changed), defined using square brackets [].
Tuple is immutable (cannot be changed), defined using parentheses ().

Example:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)

2. What is the difference between local and global variables?


Global Variable: Declared outside functions and accessible everywhere.
Local Variable: Declared inside a function and accessible only within that function.

Example:
g = 10
def func():
l=5
print("Local:", l)
print("Global:", g)
func()

3. Explain for and while loop with example.


For loop:
for i in range(3):
print("For loop:", i)

While loop:
i=0
while i < 3:
print("While loop:", i)
i += 1

4. What is a dictionary? Give its operations with example.


A dictionary stores key-value pairs.

Example:
d = {"name": "John", "age": 22}
d["city"] = "Mumbai"
d.pop("age")
print(d)
5. Explain types of function arguments in Python.
- Positional Arguments
- Default Arguments
- Keyword Arguments
- Variable Length Arguments

Example:
def func(a, b=2, *args, **kwargs):
print(a, b, args, kwargs)
func(1, 3, 4, 5, name="John")

6. What is exception handling? Name the keywords used.


Exception handling manages errors using: try, except, else, finally, raise

Example:
try:
x=5/0
except ZeroDivisionError:
print("Cannot divide by zero")

7. What is a module? How do you import it?


A module is a Python file with functions, classes, etc.

Example:
import math
print(math.sqrt(16))

8. Define class and object in Python with syntax.

class Student:
def __init__(self, name):
self.name = name
def show(self):
print("Name:", self.name)

s = Student("Alice")
s.show()

9. Write a menu-driven program to perform arithmetic operations.

def add(a, b): return a + b


def sub(a, b): return a - b
def mul(a, b): return a * b
def div(a, b): return a / b

while True:
print("1.Add 2.Sub 3.Mul 4.Div 5.Exit")
ch = int(input("Choice: "))
if ch == 5: break
x = int(input("x: "))
y = int(input("y: "))
if ch == 1: print(add(x, y))
elif ch == 2: print(sub(x, y))
elif ch == 3: print(mul(x, y))
elif ch == 4: print(div(x, y))

10. Write a program to calculate factorial using recursion.

def fact(n):
if n == 1:
return 1
return n * fact(n-1)
print(fact(5))

11. Write a program to accept N numbers and find the largest using list.

lst = [int(x) for x in input("Enter numbers: ").split()]


print("Largest:", max(lst))

12. Write a Python program to create and read from a file.

# Write
f = open("test.txt", "w")
f.write("Python file example")
f.close()

# Read
f = open("test.txt", "r")
print(f.read())
f.close()

13. Write a class Student with methods to accept and display data.

class Student:
def __init__(self):
self.name = input("Enter name: ")
self.roll = input("Enter roll: ")
def display(self):
print(self.name, self.roll)

s = Student()
s.display()

14. Write a program to demonstrate single inheritance in Python.

class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def display(self):
print("Student:", self.name)

s = Student("John")
s.display()

15. Write a program to handle division by zero using try-except.

try:
x = int(input("Enter a number: "))
print(10 / x)
except ZeroDivisionError:
print("Division by zero is not allowed")

You might also like