PSP
PSP
(2 Marks)
Algorithm:
An algorithm is a finite sequence of well-defined steps used to solve a problem or perform a computation.
(i) Statements:
Statements are individual instructions in an algorithm that perform actions such as assignment, input, or
output.
Algorithm:
An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.
2. Step-by-step refinement – Develop the solution gradually from simple to detailed steps
1. Start
1. Start
2. Read radius r
3. Area = π × r × r
4. Circumference = 2 × π × r
6. Stop
1. Start
2. Read year y
3. If (y % 400 == 0) then
Print “Leap Year”
5. Else if (y % 4 == 0) then
Print “Leap Year”
6. Else
Print “Not a Leap Year”
7. Stop
Q7. Explain Towers of Hanoi. Write the Pseudocode for Towers of Hanoi. (8 Marks)
Explanation:
The Towers of Hanoi is a classical problem involving three rods and n disks.
The objective is to move all disks from the source rod to the destination rod following these rules:
If n == 1
Move disk from source to destination
Else
End If
Q8. Outline the Towers of Hanoi problem. Suggest a solution with relevant diagrams. (8 Marks)
Outline:
• Initially, all disks are placed on the source peg in decreasing size
Solution Strategy:
Key Point:
• Easy to understand
• Helps in debugging
Disadvantages of Flowchart
• Time-consuming to draw
• Difficult to modify
1. Start
2. Read number n
3. Set i = 2
4. If n ≤ 1 → Not Prime
5. If n % i == 0 → Not Prime
7. Else → Prime
8. Stop
Start
Read a, b
Sum = a + b
Difference = a - b
Product = a * b
Quotient = a / b
Display results
Stop
**Q11. (i) What is a flowchart? List symbols and rules for writing flowchart
(i) Flowchart
A flowchart is a graphical representation of an algorithm using standard symbols to show the sequence of
steps in solving a problem.
Flowchart Symbols
Symbol Name Use
1. Start
2. Set i = 1
3. Print i
4. Increment i = i + 1
5. If i ≤ 10, go to step 3
6. Stop
Q12. Define iteration and recursion. Write an algorithm to find factorial using iteration and recursion. (8
Marks)
Iteration
Recursion
1. Start
2. Read number n
3. Set fact = 1
4. For i = 1 to n
fact = fact × i
5. Display fact
6. Stop
1. Start
2. Read number n
3. If n = 0 return 1
5. Stop
Q13. What is function? Write a program to display largest of three numbers using function. (8 Marks)
Function
Python Program
return a
return b
else:
return c
1. Start
2. Read numbers a, b
3. While b ≠ 0
r=a%b
a=b
b=r
4. Display a as GCD
5. Stop
1. Start
2. If n = 0 return 0
3. If n = 1 return 1
5. Stop
Q15. Design Flowchart, Algorithm, Pseudocode for factorial using loop. (8 Marks)
Algorithm
1. Start
2. Read number n
3. Set fact = 1
5. Display fact
6. Stop
Pseudocode
START
READ n
fact ← 1
WHILE n > 0
fact ← fact * n
n←n-1
END WHILE
PRINT fact
STOP
Flowchart (Explanation)
Q16. Using pseudocode, outline the steps to insert a new card into a sorted list of cards while preserving
the sorted order. (8 Marks)
Pseudocode
START
READ new_card
list[position] = list[position-1]
position = position - 1
END WHILE
list[position] = new_card
STOP
The algorithm shifts larger elements to the right and inserts the new card at the correct position to maintain
sorted order.
Q17. Analyse the iterative solution for the Towers of Hanoi problem and compare it with the recursive
approach. Discuss trade-offs. (8 Marks)
Comparison Table
Aspect Recursive Iterative
Conclusion
Recursive approach is preferred for clarity, while iterative is better for efficiency.
Q18. Write an algorithm and design a flowchart to check whether a given number is a perfect number or
not. (8 Marks)
Algorithm
1. Start
2. Read number n
3. Set sum = 0
4. For i = 1 to n−1
If n % i == 0
sum = sum + i
5. If sum == n
Print “Perfect Number”
6. Else
Print “Not a Perfect Number”
7. Stop
Flowchart (Explanation)
Start → Input n → Initialize sum → Loop → Check divisor → Add → Compare sum → Print result → Stop
Q19. Write a Python program to find factorial of a given number using recursion function. (8 Marks)
Python Program
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
Explanation
The function calls itself until the base condition is met and multiplies values while returning.
(ii) Define iteration and explain with example to find sum of first N natural numbers. (8 Marks)**
(i) Recursion
Recursion is a technique where a function calls itself until a base condition is satisfied.
if n == 0:
return 1
(ii) Iteration
n = int(input("Enter n: "))
sum = 0
sum += i
1. Arithmetic operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Membership operators
7. Identity operators
Q22. Explain with an example, break statement and continue statement using while loop in Python. (2
Marks)
Break statement:
Terminates the loop immediately when a condition is satisfied.
Continue statement:
Skips the current iteration and continues with the next iteration.
Example:
i=0
while i < 5:
i += 1
if i == 3:
continue
if i == 5:
break
print(i)
Q24. (i) Write a Python program to find the greatest among three numbers. (2 Marks)
a = int(input())
b = int(input())
c = int(input())
print(a)
elif b > c:
print(b)
else:
print(c)
Q25. (ii) Write a Python program to find the sum of N natural numbers. (2 Marks)
n = int(input())
sum = 0
sum += i
print(sum)
Q26. List the different types of conditional control statements and explain them with suitable examples.
(8 Marks)
1. Simple if
a = 10
if a > 5:
2. if–else
a=3
if a % 2 == 0:
print("Even")
else:
print("Odd")
3. if–elif–else
marks = 75
print("Grade A")
print("Grade B")
else:
print("Grade C")
4. Nested if
a = 10
b = 20
if a > 5:
if b > 15:
Explanation:
Conditional statements control the flow of execution based on conditions.
**Q27.
(i) Write a python program to implement a student mark system using chained conditional if
(ii) Write a python program to find whether a number is positive, negative or zero using nested if (8 Marks)**
print("Grade A")
print("Grade B")
print("Grade C")
else:
print("Fail")
if n >= 0:
if n == 0:
print("Zero")
else:
print("Positive")
else:
print("Negative")
Q28. Explain any ten string methods in Python with examples. (8 Marks)
Example:
print([Link]())
print([Link]())
**Q29.
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
count = 0
for ch in s:
if ch in "aeiouAEIOU":
count += 1
print("Vowels:", count)
Q30. Explain different types of operators in Python with suitable examples. (8 Marks)
Types of Operators
1. Arithmetic – + - * / %
print(10 + 5)
print(5 > 3)
print(5 & 3)
5. Assignment – = += -=
a=5
a += 2
print('a' in 'apple')
Q31. Discuss the working of the continue and break statements in the for loop with suitable examples. (8
Marks)
Break Statement
Example:
if i == 4:
break
print(i)
Continue Statement
Example:
if i == 3:
continue
print(i)
break continue
**Q32.
(i) Write a Python program to check whether a year is a leap year
(ii) Write a Python program to check whether a number is prime or not (8 Marks)**
print("Leap Year")
else:
if n > 1:
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
Q33. Write a Python program using if-elif-else to check divisibility by 2, 3, and 5. (8 Marks)
if n % 2 == 0 and n % 3 == 0 and n % 5 == 0:
elif n % 2 == 0:
print("Divisible by 2")
elif n % 3 == 0:
print("Divisible by 3")
elif n % 5 == 0:
print("Divisible by 5")
else:
**Q34.
freq = {}
for ch in s:
freq[ch] = [Link](ch, 0) + 1
print(freq)
Q35. Explain join(), split(), strip(), replace(), lower(), and upper() string methods with examples. (8 Marks)
Q36. Write a Python program that finds sum of all numbers in a list until it encounters a negative number
using break statement. (8 Marks)
Program
total = 0
for num in numbers:
if num < 0:
break
total += num
Explanation
**Q37.
(i) Write a Python program to find the square root of a number using Iterative Newton’s Method
(ii) Write a Python program to find simple interest. (8 Marks)**
guess = n / 2
for i in range(10):
si = (p * r * t) / 100
**Q38.
(i) Write a Python program to calculate sum of even and odd numbers separately in a list
(ii) Write a Python program to check whether a number is positive, negative or zero. (8 Marks)**
even_sum = 0
odd_sum = 0
if num % 2 == 0:
even_sum += num
else:
odd_sum += num
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
**Q39.
(i) Write a Python program to create and display elements of list and tuple
(ii) Write a Python program to check whether a number is odd or even. (8 Marks)**
print("List:", lst)
print("Tuple:", tup)
if n % 2 == 0:
print("Even")
else:
print("Odd")
Recursion
Recursion is a programming technique where a function calls itself to solve a smaller part of the same
problem until a base condition is reached.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
Explanation
Q42. What is tuple in Python? How does it differ from list? (2 Marks)
Difference:
Creating tuple:
t1 = (1, 2, 3)
t2 = 1, 2, 3
Tuple assignment:
a, b = (10, 20)
**Q44. Write a Python program to check whether elements ‘y’ and ‘a’ belong to the tuple
mytuple = ('p','y','t','h','o','n')
print('y' in mytuple)
print('a' in mytuple)
Q45. Write a Python program to delete all duplicate elements in a list. (2 Marks)
lst = [1, 2, 2, 3, 4, 4]
lst = list(set(lst))
print(lst)
Q46.
(i) Write a Python program to sort ‘n’ numbers in a list using selection sort
(ii) Write a Python program to perform linear search. (8 Marks)
n = len(lst)
for i in range(n):
min_index = i
min_index = j
for i in range(len(lst)):
if lst[i] == key:
break
else:
Q47. Demonstrate the working of +, , and slice operators in Python lists and tuples. (8 Marks)
List Operators
a = [1, 2, 3]
b = [4, 5]
print(a + b) # Concatenation
print(a * 2) # Repetition
print(a[0:2]) # Slicing
Tuple Operators
t1 = (10, 20)
t2 = (30, 40)
print(t1 + t2)
print(t1 * 2)
print(t1[1:])
Q48.
(i) Write Python code to find the minimum among 10 numbers in a list
(ii) Demonstrate code to draw histogram in Python. (8 Marks)**
min_val = lst[0]
min_val = num
data = [2, 3, 4, 5, 6, 7, 8]
[Link](data)
[Link]()
Q49. Describe the set and explain its operations with suitable examples. (8 Marks)
Set
Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # Union
print(A - B) # Difference
Q50. Describe different functions associated with sets with suitable examples. (8 Marks)
Set Functions
s = {1, 2, 3}
[Link](4)
[Link](2)
[Link]()
[Link]()
print(s)
Explanation
Q51. Explain in detail about dictionaries and their operations with suitable examples. (8 Marks)
Dictionary
Example
student = {
"name": "Rahul",
"age": 20,
"marks": 85
Dictionary Operations
1. Accessing values
print(student["name"])
2. Updating values
student["marks"] = 90
student["course"] = "Python"
4. Deleting elements
del student["age"]
5. Traversing dictionary
print(key, value)
**Q52.
(i) Using a dictionary, write Python code to find frequency of each character in a sentence
(ii) Write Python code to remove multiple keys from a dictionary using del. (8 Marks)**
(i) Character Frequency Program
freq = {}
for ch in sentence:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
print(freq)
del data["b"]
del data["d"]
print(data)
Q53. What is a function? How is a function defined and called in Python? Explain with a simple program.
(8 Marks)
Function
Syntax
def function_name(parameters):
statements
Example Program
return a + b
Explanation
• def keyword defines function
Q54. List the different types of arguments used in functions with suitable examples. (8 Marks)
1. Positional arguments
print(a + b)
add(5, 3)
2. Keyword arguments
add(b=3, a=5)
3. Default arguments
def greet(name="User"):
print("Hello", name)
greet()
4. Variable-length arguments
def total(*n):
print(sum(n))
total(1, 2, 3)
**Q55.
Using recursion
(i) Write a Python program to find the GCD of two numbers
(ii) Write a Python program to find the exponent of a number. (8 Marks)**
if b == 0:
return a
else:
return gcd(b, a % b)
print(gcd(48, 18))
if n == 0:
return 1
else:
return a * power(a, n - 1)
print(power(2, 3))
print(lst[0])
print(lst[2])
lst[1] = 25
print(lst)
del lst[0]
[Link](30)
print(lst)
**Q57.
(i) Write a Python program using a function to generate first N Fibonacci numbers
(ii) Write a Python program to find factorial using recursion. (8 Marks)**
def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
fibonacci(5)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
Q58. Define exception and describe try-except-finally block with syntax and example. (8 Marks)
Exception
An exception is an error that occurs during program execution, disrupting normal flow.
Syntax of try-except-finally
try:
statements
except:
error handling
finally:
statements
Example
try:
a = 10 / 0
except ZeroDivisionError:
finally:
print("Execution completed")
**Q59.
(i) Write a program to catch divide by zero exception with finally block
(ii) Write a Python program to handle multiple exceptions. (8 Marks)**
try:
a = int(input())
b = int(input())
print(a / b)
except ZeroDivisionError:
finally:
print("Program ended")
try:
x = int(input())
y = int(input())
print(x / y)
except ZeroDivisionError:
except ValueError:
print("Invalid input")
Q60. Mention Python list methods with examples. Why are both append() and extend() necessary? (8
Marks)
List Methods
lst = [1, 2, 3]
[Link](4)
[Link]([5, 6])
[Link](1, 10)
[Link](2)
[Link]()
[Link]()
[Link]()
append() vs extend()
Example:
[Link]([7, 8])
[Link]([9, 10])
Q61. What are modules in Python? How will you import them? (2 Marks)
A module is a file containing Python code such as functions, variables, and classes.
Importing a module:
import math
A user-defined module is a Python file created by the user to reuse functions or variables in other programs.
Example:
import mymodule
Q63. Discuss the different modes for opening and closing a file. (2 Marks)
• r – Read
• w – Write
• a – Append
Q64. Tabulate the different modes for opening a file and briefly explain them. (2 Marks)
Mode Description
r Read only
a Append data
Q65. Explain the methods used to read and write into a file with examples. (2 Marks)
Example:
[Link]("Hello")
[Link]()
Q66.
(i) Write a Python code to copy the contents of one file to another
(ii) Write a Python code to count the number of words in a file. (8 Marks)**
[Link]([Link]())
[Link]()
[Link]()
Explanation:
The source file is opened in read mode and its contents are written into the destination file.
content = [Link]()
words = [Link]()
[Link]()
**Q67.
(i) Write a Python code to read and print the first 20 characters in a file
(ii) Explain seek() and tell() methods. (8 Marks)**
print([Link](20))
[Link]()
Example:
print([Link]())
[Link](5)
print([Link]())
[Link]()
Q68. Explain the role of Tkinter in Python GUI programming. Discuss its features and advantages. (8
Marks)
Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) library used to create desktop applications.
Features
• Event-driven programming
• Platform independent
Advantages
root = Tk()
[Link]("Simple App")
[Link]()
[Link]()
Applications of Tkinter
• Calculator
• Text Editor
• Login Forms
• Image Viewer
Q70. Provide examples of using the seek() method to move the file pointer to different positions within a
file. (8 Marks)
Example Program
[Link](0)
print([Link](10))
[Link](5)
print([Link](10))
[Link]()
Explanation
Program
import calendar
print([Link](year, month))
Explanation
• month() displays the calendar for the given year and month
Q72. Explain the significance of the math module in Python. Discuss the mathematical functions and
constants available. (8 Marks)
Math Module
The math module provides mathematical functions and constants for complex calculations.
Mathematical Functions
• pow() – Power
• factorial() – Factorial
Constants
• pi – Value of π
• e – Euler’s number
Example:
import math
print([Link](16))
print([Link])
Q73. Discuss the importance of date and time functions in Python. (8 Marks)
Python uses the datetime module to work with dates and time.
Importance
Example
now = [Link]()
print(now)
Q74. Describe the process of importing modules and packages in Python. Discuss different methods of
importing with advantages and disadvantages. (8 Marks)
Methods of Importing
1. import module
import math
✔ Simple
✖ Need module name every time
✔ Direct access
✖ Name conflicts
import math as m
✔ Shorter names
Q75. Write a Python program to find minimum and maximum in a list using user-defined functions. (8
Marks)
Program
def find_min(lst):
return min(lst)
def find_max(lst):
return max(lst)
print("Maximum:", find_max(numbers))
Explanation
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
if b != 0:
print("Division:", a / b)
else:
**Q77.
(i) Write a Python program to add new content to a file and display it
(ii) Write a Python program to add additional content to a file and display it. (8 Marks)**
[Link]("Hello Python\n")
[Link]()
print([Link]())
[Link]()
[Link]("File Handling\n")
[Link]()
print([Link]())
[Link]()
File Operations
• Create
• Read
• Write
• Append
• Close
Example:
[Link]("Python")
[Link]()
Q79. Explain the general syntax for creating files with various permissions. (8 Marks)
Syntax
File Modes
• r – Read
• w – Write
• a – Append
Example:
Python Packages
Examples
• math
• datetime
• os
Example Code:
import os
print([Link]())
Q81. Define a class Person in Python with attributes for name and age. (2 marks)
class Person:
[Link] = name
[Link] = age
Q82. What is a class and object in Python? Define it with an example. (2 marks)
Example:
class Student:
pass
s1 = Student()
Q83. Differentiate between instance methods, class methods, and static methods in Python. (2 marks)
Q84. How can access specifiers (public, private, protected) be implemented in Python? (2 marks)
• Public: name
• Protected: _name
• Private: __name
Q86. Write a Python program to create a Person class with name and age as instance variables.
Implement methods to display and update the age. (8 marks)
class Person:
[Link] = name
[Link] = age
def display(self):
print("Name:", [Link])
print("Age:", [Link])
[Link] = new_age
# Object creation
p1 = Person("Rahul", 20)
[Link]()
p1.update_age(21)
[Link]()
Q87. Implement a Bank account system using OOPS concepts in Python. (8 marks)
A Bank class is created with attributes and methods to deposit, withdraw, and display account details.
class Bank:
self.account_number = acc_no
self.account_holder = holder
self.account_balance = balance
self.account_balance += amount
self.account_balance -= amount
else:
print("Insufficient balance")
def get_account(self):
print("Holder:", self.account_holder)
print("Balance:", self.account_balance)
# Object creation
[Link](2000)
[Link](1000)
b1.get_account()
Q88. How can access specifiers be implemented in Python? Discuss their importance with examples. (8
marks)
class Sample:
def __init__(self):
self.public_var = 10
self._protected_var = 20
self.__private_var = 30
def show(self):
print(self.public_var)
print(self._protected_var)
print(self.__private_var)
obj = Sample()
[Link]()
Importance:
• Supports encapsulation
class Flower:
[Link] = price
[Link] = color
[Link] = smell
def get(self):
def display(self):
print("Price:", [Link])
print("Color:", [Link])
print("Smell:", [Link])
# Objects
[Link]()
Q90. Write a Python program to create a class named ROOM and calculate area. (8 marks)
class ROOM:
[Link] = length
[Link] = breadth
def area(self):
# Objects
Q91. Illustrate a program to create a class named Dog with name and color as attributes demonstrating
__init__() method. (8 marks)
The __init__() method is used to initialize the attributes of a class at the time of object creation.
class Dog:
[Link] = name
[Link] = color
def display(self):
print("Color:", [Link])
# Object creation
d1 = Dog("Tommy", "Brown")
[Link]()
Q92. Explain how to create multiple objects to the class with programming examples. (8 marks)
A class can have multiple objects. Each object has its own copy of instance variables.
class Student:
[Link] = name
[Link] = marks
def display(self):
print([Link], [Link])
# Multiple objects
s1 = Student("Amit", 85)
s2 = Student("Riya", 90)
s3 = Student("Karan", 78)
[Link]()
[Link]()
[Link]()
Explanation:
Features of OOP:
• Static Method:
o Uses @staticmethod
• Class Method:
o Uses @classmethod
class Demo:
x = 10
@staticmethod
def static_method():
print("Static method")
@classmethod
def class_method(cls):
print(cls.x)
Purpose of self:
Q95. Write a Python program to display information about employees in the organization using class. (8
marks)
class Employee:
self.emp_id = emp_id
[Link] = name
[Link] = salary
def display(self):
print("ID:", self.emp_id)
print("Name:", [Link])
print("Salary:", [Link])
# Object creation
[Link]()
[Link]()
Q96. Write a Python program to create a Person class with name, country and date of birth. Implement a
method to determine the person's age. (8 marks)
class Person:
[Link] = name
[Link] = country
[Link] = year
def age(self):
current_year = [Link]().year
print("Name:", [Link])
print("Country:", [Link])
print("Age:", [Link]())
Q97. Write a Python program to create a Calculator class. Include methods for basic arithmetic
operations. (8 marks)
class Calculator:
return a + b
return a - b
def multiply(self, a, b):
return a * b
if b != 0:
return a / b
else:
c = Calculator()
Q98. Explain how you get access to the data members and member functions of the class with a sample
program. (8 marks)
Data members and member functions are accessed using the object name with dot (.) operator.
class Student:
[Link] = name
[Link] = marks
def display(self):
print([Link], [Link])
s1 = Student("Aman", 88)
Explanation:
class Student:
[Link] = usn
[Link] = name
[Link] = branch
def display(self):
print("USN:", [Link])
print("Name:", [Link])
print("Branch:", [Link])
[Link]()
Q100. Write a Python program to create a class representing a Circle. Include methods to calculate its
area and circumference. (8 marks)
class Circle:
[Link] = radius
def area(self):
def circumference(self):
c1 = Circle(7)
print("Area:", [Link]())
print("Circumference:", [Link]())