Python (ME-506) - Important Questions
-
Unit 37 Marks High Priority
Explain different ways of passing arguments to a Python function. Discuss positional arguments, keyword arguments, default arguments, variable-length arguments using *args and **kwargs, and keyword-only arguments. Illustrate each case with a concise code example.
Core concept: parameter passing mechanisms and function definition styles which are frequently examined as short theory plus code questions.
-
Unit 310 Marks High Priority
Write two Python functions to compute the factorial of a non-negative integer $n$: one using recursion and one using an iterative loop. Compare their time and space complexity and state which version would be preferred for large $n$ and why.
Standard practical & theory: recursion vs iteration and complexity analysis are commonly asked to test understanding of function design.
-
Unit 314 Marks High Priority
Explain the concepts of closure and decorator in Python. Write a decorator called timeit that measures and prints the execution time of the decorated function. Demonstrate its use on a sample function.
Core advanced topic: decorators and closures are high-yield items for long-answer questions requiring code and explanation.
-
Unit 310 Marks High Priority
What is the difference between an iterator and a generator in Python? Implement a generator function that yields the Fibonacci sequence up to the $n$-th term. Discuss the memory advantage of using a generator compared to returning a list.
Important practical concept: generators and iterator protocol are tested for both implementation and memory-efficiency discussion.
-
Unit 37 Marks High Priority
Describe how to create a Python package. Explain the role of init.py, and demonstrate how to structure a package with two modules so that one module can import the other. Explain absolute versus relative imports with examples.
Module/package organization and import mechanics are core syllabus topics and frequently appear in examination questions.
-
Unit 310 Marks High Priority
Write Python code to perform the following tasks: (a) Open a text file and read it line by line, printing each line with its line number. (b) Write a list of dictionaries to a file in JSON format. (c) Explain how binary file I/O differs from text I/O and give an example of writing and reading binary data.
File I/O is fundamental and commonly tested: reading/writing text & binary files and working with JSON are practical tasks.
-
Unit 37 Marks Medium Priority
Explain Python's exception handling mechanism using try, except, else, and finally. Define a custom exception class called InvalidAgeError and write a function validate_age(age) that raises this exception when the age is negative or unreasonably large.
Exception handling and custom exceptions test both syntax and design for robust programs.
-
Unit 37 Marks Medium Priority
Compare JSON serialization and pickle in Python. Give a code example that demonstrates how to serialize and deserialize a custom Python object to JSON by providing a custom encoder/decoder or by converting the object to and from a dictionary.
Serialization topics (JSON vs pickle) are practical and often asked to assess data interchange knowledge.
-
Unit 310 Marks Medium Priority
Explain the use of the re module functions compile, search, match, and findall. Write a regular expression to validate a simple email address and use it to extract the username and domain parts for the input string.
Regular expressions are a standard applied topic; pattern design and extraction are typical exam problems.
-
Unit 37 Marks Medium Priority
Explain lambda expressions in Python. Using lambda, map, filter, and reduce (from functools), write code to compute the sum of squares of even numbers from a given list of integers. Also show an equivalent solution using list comprehensions.
Higher-order functions and functional constructs are commonly tested as short coding questions or conceptual comparisons.
-
Unit 37 Marks Medium Priority
Write a unit test using the unittest framework for a function is_prime(n) that returns True if $n$ is prime. Also explain how doctest can be used to test small functions and show a simple doctest example inside a function's docstring.
Testing and documentation are often included in question papers as applied short-answer items.
-
Unit 37 Marks Low Priority
Describe how to parse command-line arguments in Python using the argparse module. Write a short script that accepts a filename and a verbosity flag (--verbose) and prints appropriate messages based on the flag.
Command-line argument parsing is a practical skill sometimes asked in viva or short coding questions.
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