Computer Workshop (Python Programming) (CY-306) - Important Questions
-
Unit 510 Marks Medium Priority
Write a Python program to read a text file named "input.txt" and compute: (a) total number of lines, (b) total number of words, and (c) total number of characters. Use the with statement and handle the case when the file does not exist by printing an appropriate error message.
Core file I/O programming task: common practical question to test reading, processing, and exception handling for text files.
-
Unit 57 Marks Medium Priority
Explain the different file modes in Python (for example 'r', 'w', 'a', 'rb', 'wb', 'r+'). For each mode give a short example of when it should be used. Demonstrate how to safely open and close files using the with statement.
Fundamental theory and usage of file modes and context managers; often asked as short-answer or short-coding question.
-
Unit 57 Marks Medium Priority
Write a Python function to copy a binary file (for example an image) from a source path to a destination path using a fixed-size buffer (for example 4096 bytes). Explain why buffered reads/writes are preferred over reading the entire file into memory for large files.
Practical binary file handling and buffered I/O; tests understanding of reading/writing in chunks and reasons for buffering.
-
Unit 510 Marks Medium Priority
Using the csv module, write a Python program to read a file named 'data.csv' which contains columns 'name', 'age', and 'city'. Filter the rows where age is greater than 30 and write those rows to 'filtered.csv' preserving the header. Include proper opening/closing of files and explain the role of csv.reader and csv.writer.
Use of Python standard library for common data formats; typical question combining module usage and simple data filtering logic.
-
Unit 57 Marks Medium Priority
Explain how the sys module provides access to command-line arguments in Python. Write a short program that accepts a filename as a command-line argument and prints the number of lines in that file. Also show how to set a non-zero exit code when an error occurs.
Command-line interface programming with sys module; straightforward short program often examined in practical papers.
-
Unit 57 Marks Medium Priority
Describe the purpose of the logging module in Python. Provide example code to configure a logger that writes messages with level WARNING and above to a file named 'app.log', including timestamps and log levels in each entry. Explain the difference between logging and printing to stdout.
Testing knowledge of logging configuration and best practices (levels, handlers, formatters).
-
Unit 510 Marks Medium Priority
Write a Python function using the re module to validate email addresses of the form 'local-part@domain'. Provide a suitable regular expression and explain each component (for example allowed characters, dot-separated domain labels). The function should return True for valid emails and False otherwise. Provide two examples of valid and two of invalid email strings.
Regular expressions are core to the unit; this question tests regex construction and use of re module.
-
Unit 57 Marks Medium Priority
Using the datetime module, write code to parse the string '31-12-2020' into a datetime.date or datetime.datetime object, add 45 days to it, and print the resulting date in ISO format (YYYY-MM-DD). Briefly explain the difference between naive and timezone-aware datetime objects.
Datetime parsing and arithmetic is a typical library question to test date/time API usage and formatting.
-
Unit 57 Marks Low Priority
Explain the purpose of the file object methods seek() and tell() in Python. Write a short program that opens a file in binary mode, reads the first 100 bytes, prints the current file position, uses seek() to go back to the beginning, and reads the first 100 bytes again.
File pointer manipulation methods and their applications; common short-coding or explanation question.
-
Unit 510 Marks Low Priority
Compare JSON and pickle for serializing Python objects. Write code that serializes a Python dictionary to a JSON file and then reads it back into a dictionary. Mention one security consideration when using pickle for serialization.
Serialization topics using standard libraries; practical knowledge of portability and security considerations.
Quick Add to Notes
Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.
Create free accountHave an account? Log in
Notes Panel