Whenever you write code for the exercises below, use functions to keep your solution reusable and scalable.
Q1.
What will be the output of the following programs?
You have to predict the output result without running/executing the code.
I.
func() def func(): print("func")II.
def func(): print("func") funcIII.
def funcB(): print("In funcB()") def funcA(): print("In funcA()") funcB() funcA()IV.
def funcA(): print("In funcA()") funcB() def funcB(): print("In funcB()") funcA()
Q2.
You need to display the same error message in multiple places in your program. Instead of writing the same print() statement repeatedly, design a reusable solution that makes your code more efficient and easier to maintain. Write a function that prints the error message and then demonstrate it by calling the function twice.
Expected Output:
Invalid command! // first time calling the function
Invalid command! // second time calling the functionQ3.
What will be the output of the following programs?
You have to predict the output result without running/executing the code.
I.
def getNumber(): return 10 n = 0 getNumber() print(n)II.
def printNumber(): n = 10 print(n) print(f"number: {printNumber()}")III.
def getChar(): return 'c' print(f"char: {getChar()}")IV.
def getChar(): char = 'c' print(f"char: {getChar()}")
Q4.
In a busy cafe, the barista needs to take orders from multiple customers, each time asking the number of cups for order and making sure it is a valid positive integer number.
To solve this problem, write a program that defines a function that repeatedly prompts the user to enter the number of cups for their order until a valid positive integer is provided, then the function should return the valid number of cups once it is obtained.
Expected Output:
Enter the number of cups for your order: -3
Invalid input! Please enter a positive integer.
Enter the number of cups: 0
Invalid input! Please enter a positive integer.
Enter the number of cups: 2
Thank you for your order!
You have ordered for 2 cups of coffee.Q5.
What will be the output of the following programs?
You have to predict the output result without running/executing the code.
I.
def getFirstNum(): return 7 def getSecondNum(): return 9 print(f"{getFirstNum() + getSecondNum()}")II.
def getFirstNum(): return 7 def getSecondNum(): return 9 print(f"{getFirstNum() + getSecondNum()}")III.
def getNumbers(): return 7, 9 print(f"numbers: {getNumbers()}")IV.
def getNumbers(): return 7 return 9 print(f"numbers: {getNumbers()}")V.
def func(x): if x % 7 == 0: return x n = func(45) if n: print("n exists.") else: print("n does not exist.")
Q6.
What will be the output of the following programs?
You have to predict the output result without running/executing the code.
I.
def sum(a, b): return a + b s1 = sum(9.5, 6.3) s2 = sum(5, 7.4) print(f"sum1: {s1}, sum2: {s2}")II.
def max(a, b): if a > b: return a return b print(f"max(3, 5): {max(3, 5)}") print(f"max(9, 7): {max(9, 7)}")III.
def func(x): x = 3 y = 4 print(f"func: x = {x}, y = {y}") def main(): x = 1 y = 2 print(f"main: x = {x}, y = {y}") func(x) print(f"main: x = {x}, y = {y}") main()IV.
def printNumbers(a, b): print(f"a: {a}, b: {b}") a = 3 b = 5 printNumbers(b, a)V.
def alertSpam(message, times = 1): print(message * times) alertSpam("SPAM! ") alertSpam("ALERT! ", 5)VI.
x = 0 def func(): print(x) x = 2 print(x) func() print(x)VII.
def func(a, b = 5, c = 10): print(f"a = {a}, b = {b}, c = {c}") func(3, 7) func(25, c = 24) func(c = 30, a = 100)
Q7.
Write a program that takes a person’s height in feet as input and converts it to centimeters.
Expected Output:
Enter person's height (in ft): 5.4 // user input
Person's height (in cm): 164.592
// 1 ft = 30.48 cmQ8.
Write a program that takes student marks as input, then gives the result grade accordingly.
- for more than 75 : A
- for in between 60 and 75 : B
- for in between 35 and 60 : C
- for less than 35 : F
Expected Output:
Enter student's marks (out of 100): 55 // user input
Student's grade: CQ9.
What will be the output of the following programs?
You have to predict the output result without running/executing the code.
I.
def func(a, b = 5, c): print(f"a = {a}, b = {b}, c = {c}") func(3, 7)II.
def func(x): y = 4 print(f"func: x = {x}, y = {y}") x = 3 print(f"func: x = {x}, y = {y}") def main(): x = 1 y = 2 print(f"main: x = {x}, y = {y}") func(x) print(f"main: x = {x}, y = {y}") main()III.
x = 5 def func(): print(x) func() print(x)IV. (Adv.)
def func(name, *subject_marks): print(f"Hello, {name}!") for subject in subject_marks: print(f"One of your subject marks is {subject}.") func("John", 75, 92, 58, 86, 63)V. (Adv.)
def func(business_name, **customers): print(f"Welcome to our {business_name} business!\n") print("Pending customer orders:") for customer, order in customers.items(): print(f"{customer} : {order}") func("Clothes", Magan=32, Chhagan=54, Lagan=67)VI. (Adv.)
x = 0 def func(): global x print(x) x = 2 print(x) func() print(x)
Q10.
In a school, when two new students enroll at the same time, the teacher needs to decide who gets the lower roll number by comparing their names alphabetically. Write a program that takes two students’ names as input and then announce which student should receive the earlier roll number based on dictionary order or note if their names are identical.
Expected Output:
Enter first student's name: Magan // user input
Enter second student's name: Gagan // user input
Gagan should receive the earlier roll number.Q11.
Write a program to calculate simple interest (i) for principal amount (p), rate of interest (r), number of years (n) given by user.
Expected Output:
Enter principal amount: 50000 // user input
Enter rate of interest: 5 // user input
Enter no of years: 2 // user input
Simple Interest: 5000
// i = (p*r*n)/100Q12.
Write a program to check whether a given year is a leap year or not.
Expected Output:
Enter year: 2024 // user input
2024 is a leap year.Q13.
In a digital calendar app, users enter meeting times in 24-hour format but you need to display them as 12-hour times with AM/PM. Write a program to convert a given 24-hour time to 12-hour format.
Expected Output:
Enter time in 24-hour format (HH MM): 14 30 // user input
12-hour format: 02:30 PMQ13.
You’ve decided to buy apples in bulk and want to get the best deal. You call three different suppliers and ask for their per-kilogram price. Write a program that asks for the price per kilogram from three suppliers and then tells you what is the lowest price.
Expected Output:
Enter price from Supplier 1 (per kg): 42.50
Enter price from Supplier 2 (per kg): 40.75
Enter price from Supplier 3 (per kg): 41.20
Lowest apple price: 40.75 per kgQ15. (Adv.)
On a remote island, treasure hunters find chests marked with numbers. Only the “magic chests” - those whose numbers can’t be split evenly into smaller piles, hold the rare gems. Write a program that asks the user for a chest number and then declares whether it’s a magic chest with a special treasure or just an ordinary chest.
Expected Output:
Enter chest number: 23 // user input
Magic chest! You've uncovered the hidden gem.