

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Coverage: Variables, Input/Output, Data Types, Operators, and if / elif / else (no loops)
Focus: practical, applied, less on algorithmic complexity
A1. Write a Python program that asks the user to enter the name of a product and its quantity, then displays a message:
"You have ordered units of ."
A2. A café wants a program that asks the customer for the price of a coffee and displays the price after applying a 10% student discount. Write the code, ensuring the output is displayed to 2 decimal places.
A3. Write a program that asks the user to enter a temperature in Celsius. Using if, elif, and else, display:
"Freezing" if temperature ≤ 0
"Cold" if temperature > 0 and ≤ 15
"Warm" if temperature > 15 and ≤ 30
"Hot" if temperature > 30
A4. A theme park charges different entry fees:
Child (under 10): $
Teen (10–17): $
Adult (18 and above): $
Write a program that asks for the visitor's age and outputs the correct entry fee using if elif else.
A5. Explain, in your own words, the difference between int() and str() when used with input(). Give one example of each.
A6. State the data type Python would assign to each of the following values, and explain your reasoning:
18
"18"
False
A7. What will be displayed by the following code? Explain your answer in one sentence.
price = 250 print("The price is $" + price)
Focus: precise syntax, logic, tracing, and understanding of control structures
B1. Identify and correct the errors in the following code:
Score = input("Enter your score") Grade = input("Enter your grade) print("You scored " + Score "and got grade" + Grade)
B2. Write an algorithm (as Python code) that:
B3. Trace through the following code and state the output for each of these inputs: 3, 12, 21.
num = int(input("Enter a number: ")) if num < 5: print("Small") elif num < 15: print("Medium") else: print("Large")
B4. A program is required to validate a username's length. If the username entered has more than 12 characters, the program should output "Too long". If it has 12 or fewer characters, it should output "Valid length". Write the Python code, using the len() function and an if else statement.
B5. Explain why the following code produces an error, and rewrite it correctly:
height = input("Enter your height in cm: ") if height > 150: print("Tall enough for the ride")
B6. Using pseudocode-to-Python translation skills, convert the following pseudocode into working Python code:
INPUT temperature IF temperature > 37 OUTPUT "Fever" ELSE OUTPUT "Normal" ENDIF
B7. A program should classify a year entered by the user (assume it is a valid 4-digit year):
If the year is divisible by 400 → "Leap year"
Else if divisible by 100 → "Not a leap year"
Else if divisible by 4 → "Leap year"
Otherwise → "Not a leap year"
Write the Python code using if elif else and the modulus operator (%).
B8. State what each of the following expressions evaluates to (True or False). Show your working.
(a) 7 == 7. (b) 10 != "10" (c) (5 > 3) and (2 > 4) (d) (5 > 3) or (2 > 4) (e) not (6 < 2)
B9. What is displayed when the following code runs? Explain each line of output.
a = 4 b = 7