0% found this document useful (0 votes)
30 views4 pages

Python Practical Exam Questions

Python practice questions

Uploaded by

24f2001016
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)
30 views4 pages

Python Practical Exam Questions

Python practice questions

Uploaded by

24f2001016
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

Python Practical Questions

—----------------------------------------------------------------------------------------------------------------------------

1. Question

Problem statement

A student wants to learn the fundamentals of Python by printing a personalized greeting and
exploring how variables are stored in memory.

Your Task:
Write a Python program that takes your name as input and prints a greeting using the print()
function. Add a function to display the memory address of the variable storing your name using
the id() function.

2. Question

Problem statement

A developer wants to create a voting eligibility checker while learning about Python's dynamic
typing.

Your Task:
Create a program to accept a user's age and check if they are eligible to vote. Include a function
that dynamically determines the data type of user input using type().

3. Question

Problem statement

A company wants to organize employee data in a matrix and access records efficiently.

Your Task:
Implement a program to create a 2D array (matrix) that stores employee data (e.g., ID, Name,
and Salary). Add functionality to retrieve employee details by their ID.

4. Question

Problem statement
A photographer wants to experiment with creating images programmatically.

Your Task:
Write a program to create an image with random colors using OpenCV and save it locally using
imwrite().

5. Question

Problem statement

A developer working on input sanitization wants to detect and handle escape sequences.

Your Task:
Write a program that reads user input and checks if it contains a specific escape sequence (\n
or \t). Demonstrate the importance of indentation in the program.

6. Question

Problem statement

An analyst needs a quick way to categorize numbers based on their sign.

Your Task:
Create a ternary operator-based function that categorizes a number as "Positive," "Negative," or
"Zero."

7. Question

Problem statement

A teacher wants to demonstrate the power of Python comprehensions to students.

Your Task:
Generate a list of squares for numbers 1–10 using a list comprehension. Demonstrate how to
extract even squares from the list.

8. Question

Problem statement
A student wants to learn efficient generation of large datasets using generators.

Your Task:
Write a generator function to yield the first N Fibonacci numbers. Use it to print Fibonacci
numbers up to 100.

9. Question

Problem statement

A data scientist wants to analyze sorting performance for large datasets.

Your Task:
Measure the time taken to sort a list of 1,000,000 random integers using the timeit module.

10. Question

Problem statement

A programmer wants to understand how Python handles copying in memory.

Your Task:
Illustrate the difference between shallow and deep copy using lists.

11. Question

Problem statement

A developer validates user input in a registration system.

Your Task:
Write a program that creates a custom exception InvalidEmailError. Validate an email
address using regular expressions, and raise the custom exception if the email is invalid.

12. Question

Problem statement

A data engineer processes streaming data for analytics.


Your Task:
Create a coroutine to process a stream of numbers in real time. It should calculate the running
average of the numbers provided.

13. Question

Problem statement

A data scientist optimizes calculations for large datasets.

Your Task:
Write a Python program to use the multiprocessing module to calculate the sum of squares
of numbers in a large dataset by splitting the dataset into chunks.

14. Question

Problem statement

A developer creates a plugin system for a Python application.

Your Task:
Write a program to dynamically load and execute a Python module based on user input.
Validate the module's existence before importing.

15. Question

Problem statement

A system administrator tracks file access and ensures proper closure.

Your Task:
Create a custom context manager to handle file operations safely. Ensure that it logs each file
access in a separate log file.

Common questions

Powered by AI

In Python, shallow copies create a new collection object and populate it with references to the items found in the original, meaning that changes to mutable items in the original can affect the copy. Deep copies, however, construct a new collection object and recursively add copies of the items found, ensuring that modifications in the original do not affect the copied object. This distinction impacts program behavior significantly in terms of data integrity, where deep copies protect against unintentional data alterations, but at the cost of higher memory and computational resource usage .

The multiprocessing module in Python offers significant benefits when calculating the sum of squares over a large dataset by allowing parallel execution. This leads to a reduction in computation time as multiple processes can run concurrently, each handling a portion of the dataset. Leveraging multiple CPU cores helps prevent bottlenecks associated with single-threaded execution, resulting in improved efficiency and scalability, especially pertinent for processing extensive data collections in data-heavy applications .

Using OpenCV to create images with random colors in Python provides powerful image processing capabilities directly from a script. It allows precise control over image properties and efficient storage in various formats. However, the complexity and size of the OpenCV library may be overkill for simple tasks, and it can lead to higher memory usage and complexity in cases where basic functionality is sufficient. The necessity of understanding matrix operations can also introduce a steeper learning curve .

Python's id() function helps beginners understand variable storage in memory by returning the memory address (identity) associated with a specific object. When a variable is assigned a value, id() can be used to show where this variable is stored in memory, highlight changes when reassigned, and demonstrate object references, providing a practical illustration of how Python manages memory behind variable assignments .

A custom context manager in Python can be constructed using the `__enter__` and `__exit__` methods within a class or the `@contextmanager` decorator in functions. When used for file operations, it ensures that files are properly opened and closed by handling exceptions and automatically calling the close method post-operation. A context manager can also log file access operations by combining these methods with logging functions, which helps in tracking and debugging file handling processes effectively .

To validate an email address with a custom exception in Python, you should first define a custom exception class, such as InvalidEmailError, inheriting from the base Exception class. Next, use a regular expression inside a function to validate email input against standard email patterns. If the email fails to match, raise the InvalidEmailError to indicate the input is invalid. This approach ensures that email validation is both extensible and decoupled from standard exception handling, making error management clearer and more maintainable .

Dynamic typing in Python allows the voting eligibility checker program to accept user input as various data types without declaring the type beforehand. The program can accept an age input as a string, and then use the type() function to determine the data type at runtime, ensuring it dynamically handles conversions if necessary .

A coroutine for calculating a running average in Python can be implemented by defining a function with the `yield` keyword to pause and resume execution. This coroutine would initialize with the starting variables, such as a sum and count, and update these each time a new number is sent into the coroutine. By yielding results after each input, users receive immediate feedback. This approach is effective for handling streaming data because it minimizes state storage and allows for real-time computation making it suitable for time-critical analytics .

List comprehensions in Python are advantageous because they provide a concise way to construct lists without needing multiple lines of loop statements. At a glance, students can see how a functional style of programming can lead to clearer and more maintainable code. They also show efficiency gains when generating lists, such as the one containing squares of numbers, by reducing the syntax to a single line, which can make the code easier to read and debug .

The timeit module in Python is practically used for assessing the efficiency of different algorithms or code snippets, particularly sorting in large datasets. By providing a method to precisely measure execution time, developers can compare sorting algorithms under realistic conditions, optimize performance, and minimize execution time. This aids in selecting the most efficient solution tailored to specific data characteristics, which is crucial for performance-critical applications where time complexity directly impacts user experience or system resource allocation .

You might also like