0% found this document useful (0 votes)
2 views3 pages

Python Exam Sample Basic String Oop

The document is a sample Python programming exam consisting of three programming tasks: a parking fee calculator, a device code formatter, and a product and shopping cart management system. Each task has specific requirements, input/output formats, and constraints that must be followed. The exam is designed to test students' understanding of string functions and object-oriented programming in Python within a 60-minute duration.

Uploaded by

hieuk19bdtdvn
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)
2 views3 pages

Python Exam Sample Basic String Oop

The document is a sample Python programming exam consisting of three programming tasks: a parking fee calculator, a device code formatter, and a product and shopping cart management system. Each task has specific requirements, input/output formats, and constraints that must be followed. The exam is designed to test students' understanding of string functions and object-oriented programming in Python within a 60-minute duration.

Uploaded by

hieuk19bdtdvn
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

SAMPLE PYTHON PROGRAMMING EXAM

Basic Program - String Function - Object-Oriented Programming


Duration: 60 minutes

Student’s ID: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Student’s name: . . . . . . . . . . . . . . . . . . . . . . . . . . .

Instructions:

• Write your solution in Python.


• Follow the required function names, class names, input format, and output format exactly.
• Unless otherwise stated, inputs are assumed to be provided from standard input.

1 Easy (30 points)


E01. Parking Fee Calculator
Context:
A small campus parking area records the type of vehicle and the number of parking hours for each
visitor. The administration wants a simple Python program to calculate the parking fee automatically.
Problem Description:
Write a Python program that reads two lines from the standard input:

(1) the vehicle type, and


(2) the number of parking hours.

The valid vehicle types are BIKE, CAR, and BUS. The parking rates are:

Vehicle type Fee per hour


BIKE 5.0
CAR 20.0
BUS 35.0

If the number of parking hours is greater than 5, a 10% discount is applied to the total fee. If the
vehicle type is invalid, or if the number of hours is not a positive integer, the program must print
INVALID INPUT.
Constraints:

• The number of hours must be an integer from 1 to 24.


• Vehicle types are case-sensitive.

Input Format:
The first line contains the vehicle type.
The second line contains the number of parking hours.
Output Format:
Print one line containing the final parking fee with exactly 1 digit after the decimal point, or print
INVALID INPUT if the input is invalid.
For example:

Input Result
CAR 108.0
6
TRUCK INVALID INPUT
3

Page 1
2 Medium (20 points)
M01. Device Code Formatter
Context:
An electronics laboratory stores device codes written by different technicians. Some codes contain
spaces or hyphens, and some letters may be lowercase. Before the codes are stored in the database,
they must be standardized by a Python function.
Problem Description:
Write a Python function named format_device_code(raw_code).
The function receives a string raw_code and returns a standardized device code according to the
following rules:

(1) Remove all spaces and hyphens from the input string.
(2) Convert all alphabetic characters to uppercase.
(3) After cleaning, the code is valid only if it contains letters and digits only.
(4) The cleaned code must have length from 4 to 12 characters.
(5) If the cleaned code is valid, return it in groups of 4 characters separated by hyphens.
(6) If the cleaned code is invalid, return the string INVALID.

Function Signature:
def format_device_code(raw_code):
# your code here

Constraints:

• The input string length is at most 100 characters.


• The function must return a string and must not print the result directly.

Output Requirement:
The returned string must be either the formatted device code or INVALID.
For example:

Test Result
print(format_device_code(" ab-12 cd34 ")) AB12-CD34
print(format_device_code("x9 y8 z7")) X9Y8-Z7
print(format_device_code("A1@B2")) INVALID

3 Hard (10 points)


H01. Product and Shopping Cart Management
Context:
An online shop wants to use Object-Oriented Programming to represent products and a shopping cart.
Each product has an ID, a name, and a price. The cart stores multiple product objects and calculates
the total price of all products currently added to it.
Problem Description:
Implement the following components in Python.
Class Product

• __init__(self, product_id, name, price): initializes product_id as a string, name as a


string, and price as a float.

Page 2
• apply_discount(self, percent): decreases the product price by percent percent. For ex-
ample, a 10% discount changes 100.0 to 90.0.
• display_info(self): prints the product information in the format PRODUCT_ID:NAME:PRICE,
where PRICE has exactly 1 digit after the decimal point.

Class ShoppingCart

• __init__(self): initializes an empty internal collection of products.


• add_product(self, product_obj): adds a Product object to the cart.
• get_total_price(self): returns the total price of all products currently in the cart. If the
cart is empty, return 0.0.

Function process_products(raw_data, discount)


This function receives:

• raw_data: a list of strings, where each string has the format PRODUCT_ID,NAME,PRICE;
• discount: a percentage value.

The function must create a Product object for each record, apply the discount, and print its information
by calling display_info().
Constraints:

• Product IDs and product names are case-sensitive.


• If raw_data is empty, the function should produce no output.
• The discount value is between 0 and 100.

Input/Output Requirement:
The display_info() method must print one line following this format:

PRODUCT_ID:NAME:PRICE

where PRICE has exactly 1 digit after the decimal point.


For example:

Test Result
data = ["P01,Mouse,100.0", "P02,Keyboard,250.0"] P01:Mouse:90.0
process_products(data, 10) P02:Keyboard:225.0
cart = ShoppingCart() 200.0
cart.add_product(Product("P03", "Cable", 50.0))
cart.add_product(Product("P04", "Adapter", 150.0))
print(format(cart.get_total_price(), ".1f"))

Page 3

You might also like