0% found this document useful (0 votes)
118 views6 pages

21cs15it Problem Solving and Python Programming Questions and Answers

Uploaded by

starun080905
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)
118 views6 pages

21cs15it Problem Solving and Python Programming Questions and Answers

Uploaded by

starun080905
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

PART A

1. List out the features of Python.

2. Evaluate the order of precedence of operators in Python.

3. Define function and mention its types.

4. What is the scope of variables

5. What is module and package in Python?

6. Difference between algorithm and pseudocode.

7. Define flowchart and list out the symbols.

8. Differentiate between local and global variables.

9. What do you mean by fruitful function?

10. Illustrate negative indexing in list with an example.

11. Discuss different modes of operation in python.

12. Define Variable.

13. Illustrate the flowchart of if-elif-else statements.

14. Write the syntax for function definition

15. Define Modules.

PART B
1. Explain in detail about the Operators in Python.

2. Explain in detail about various building blocks of algorithm?

3. Write the following Python programs.


To find the sum of ‘n’ natural numbers.
To convert Celsius to Fahrenheit.

4. Write algorithm, pseudo code and flowchart to find the eligibility for
voting?
Start
Input the age of the person.
Check if the age is 18 or above.
If true, the person is eligible to vote.
If false, the person is not eligible to vote.
Display the result.
End

5. Write algorithm, pseudo code and flowchart to check whether the number is
palindrome or not.
n=eval(input("enter a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum+a*a*a
n=n//10
if(sum==org):
print("The given number is Armstrong number")
else:
print("The given number is not Armstrong number")

6. Explain in detail about Iterative Statement with example.

7. Describe user defined function with example.

8. Explain about Built in function with example.

9. Define methods in a string with an example program using at least 5


methods.

10. How to access characters of a string?

11. Write a function that takes a string as a parameter and replaces the first
letter of every word with the corresponding uppercase letter.

12. Explain in detail about Conditional Statement with example.

13. Explain About Argument Types with example programs.

14. Analyze string slicing. Illustrate how it is done in python with an


example

15. Write a python code to search a string in the given list.

16. Describe python modules.

17. Describe python packages.

18. Explain in detail about various building blocks of algorithm?

19. Evaluate the different values (data types) and types of values that can be
used in Python.
20. Explain pseudocode and its rules also give examples for sequence,
selection and repetition type problems

21. Develop a flowchart to check whether the given number is a prime number or
not.

22. Write an algorithm find the square root of a number?

PART C
1. Write algorithm, pseudo code and flowchart to find Prime number or not.
Algorithm to Check if a Number is Prime:
Step-1:Start.
Step-2:Input the number n to be checked.
Step-3:If n <= 1, output that n is not prime and exit.
Step-4:for i from 2 to sqrt(n):
Step-5:If n % i == 0, then output that n is not prime and exit.
Step-6:If no divisors were found, output that n is prime.
Step-7:End.

2. Write algorithm, pseudo code and flowchart to check whether the number is
Armstrong or not.
Algorithm to Check if a Number is Armstrong:
Step-1:Start.
Step-2:Input the number n to check.
Step-3:Find the number of digits in n and store it in num_digits.
Step-4:Initialize sum to 0 and temp to n.
Step-5:while temp > 0:
Step-6:Extract the last digit of temp (digit = temp % 10).
Step-7:Raise the digit to the power num_digits and add the result to sum.
Step-8:Remove the last digit from temp (temp = temp // 10).
Step-9:If sum is equal to n, output that n is an Armstrong number.
Step-10:Otherwise, output that n is not an Armstrong number.
Step-11:End.

3. Write a python program to calculate electricity bill based on the number


of units consumed
1 to 100 units (Rs.2.5/Unit)
101 to 200 units (Rs.3.5/Unit)
201 to 300 units (Rs.4.5/Unit)
Above 301 units (Rs.6/Unit)
Tax = 2% of Total Bill. Using conditional statements.
# Function to calculate electricity bill
def calculate_bill(units):
# Initialize the bill amount
bill = 0
# Calculate based on units consumed
if units <= 100:
bill = units * 2.5
elif units <= 200:
bill = (100 * 2.5) + ((units - 100) * 3.5)
elif units <= 300:
bill = (100 * 2.5) + (100 * 3.5) + ((units - 200) * 4.5)
else:
bill = (100 * 2.5) + (100 * 3.5) + (100 * 4.5) + ((units - 300) * 6)
# Calculate tax (2% of total bill)
tax = 0.02 * bill
total_bill = bill + tax
return total_bill
# Input: Number of units consumed
units = int(input("Enter the number of units consumed: "))
# Calculate the total bill
total_bill = calculate_bill(units)
# Output: Display the total electricity bill
print("The total electricity bill is: Rs.",total_bill)
4. Write a program to explain the various operators involved in Python and
how an expression evaluated using precedence of operators.
# Python program to explain various operators and demonstrate operator
precedence
# Define a function to demonstrate each type of operator
def operators_demo():
# Arithmetic Operators
print("Arithmetic Operators:")
a = 10
b = 3
print(f"{a} + {b} = {a + b}") # Addition
print(f"{a} - {b} = {a - b}") # Subtraction
print(f"{a} * {b} = {a * b}") # Multiplication
print(f"{a} / {b} = {a / b}") # Division
print(f"{a} % {b} = {a % b}") # Modulus
print(f"{a} ** {b} = {a ** b}") # Exponentiation
print(f"{a} // {b} = {a // b}") # Floor Division
print()
# Comparison Operators
print("Comparison Operators:")
print(f"{a} > {b} = {a > b}") # Greater than
print(f"{a} < {b} = {a < b}") # Less than
print(f"{a} == {b} = {a == b}") # Equal to
print(f"{a} != {b} = {a != b}") # Not equal to
print(f"{a} >= {b} = {a >= b}") # Greater than or equal to
print(f"{a} <= {b} = {a <= b}") # Less than or equal to
print()
# Logical Operators
print("Logical Operators:")
x = True
y = False
print(f"{x} and {y} = {x and y}") # Logical AND
print(f"{x} or {y} = {x or y}") # Logical OR
print(f"not {x} = {not x}") # Logical NOT
print()
# Bitwise Operators
print("Bitwise Operators:")
print(f"{a} & {b} = {a & b}") # Bitwise AND
print(f"{a} | {b} = {a | b}") # Bitwise OR
print(f"{a} ^ {b} = {a ^ b}") # Bitwise XOR
print(f"~{a} = {~a}") # Bitwise NOT
print(f"{a} << 1 = {a << 1}") # Bitwise Left Shift
print(f"{a} >> 1 = {a >> 1}") # Bitwise Right Shift
print()
# Assignment Operators
print("Assignment Operators:")
a = 5
a += 3
print(f"a += 3 -> a = {a}")
a -= 2
print(f"a -= 2 -> a = {a}")
a *= 2
print(f"a *= 2 -> a = {a}")
a /= 2
print(f"a /= 2 -> a = {a}")
a %= 3
print(f"a %= 3 -> a = {a}")
a **= 2
print(f"a **= 2 -> a = {a}")
a //= 2
print(f"a //= 2 -> a = {a}")
print()
# Identity Operators
print("Identity Operators:")
print(f"{a} is {b} = {a is b}") # Identity operator: is
print(f"{a} is not {b} = {a is not b}") # Identity operator: is not
print()
# Membership Operators
print("Membership Operators:")
list_example = [1, 2, 3, 4, 5]
print(f"3 in {list_example} = {3 in list_example}")
print(f"6 not in {list_example} = {6 not in list_example}")
print()
# Call the function to show operator examples
operators_demo()
# Demonstrate Operator Precedence in an Expression
def precedence_demo():
print("Operator Precedence in an Expression:")
expression = 10 + 2 * 3 ** 2 - 5 / 5
print(f"Expression: 10 + 2 * 3 ** 2 - 5 / 5")
print("Steps:")
print("1. Exponentiation (3 ** 2) = 9")
print("2. Multiplication (2 * 9) = 18")
print("3. Division (5 / 5) = 1")
print("4. Addition (10 + 18) = 28")
print("5. Subtraction (28 - 1) = 27")
print(f"Result of expression = {expression}")
# Call the function to demonstrate precedence
precedence_demo()

5. Explain break statement and continue statement in Python with an example.

6. Write an algorithm and Pseudo code for the following:


(i) Calculating Area and Circumference of a Circle:
Input: Radius of the circle (R).
Process:
Calculate the area using the formula:
Area=π×R2
Calculate the circumference using the formula:
Circumference=2×𝜋×𝑅
(ii) Check if a Given Year is a Leap Year:
Input: Year (Y).
Process:
A year is a leap year if:
It is divisible by 4.
Otherwise, it is not a leap year.

You might also like