Programming in Python MCA Paper - Dec 2020
- Subject Code: - PGCA 1951
- Subject Name: - Programming in Python
- Date of Examination: - December 2020
- Class: - MCA 1st
Instructions to Candidates
- Section A is Compulsory consisting of TEN questions carrying TWO marks each.
- Section B & C have FOUR questions each.
- Attempt any FIVE questions from SECTION B & C carrying TEN marks each.
- Select at least TWO questions from SECTION B & C
- What is namespace in Python?
- What are various python modules? List some commonly used built-in modules in python.
- What are the inbuilt functions for data type conversion in python?
- Differentiate between Python Arrays and lists.
- Why do we use ‘Self’ in python?
- Define the use of python iterators.
- Where do we use help () and dir () functions in python languages?
- How can files be deleted in Python?
- How do we handle exceptions in Python?
- How does break continue and pass work?
- How do you install Python on Windows and set path variable? What is the process of compilation and linking in python?
- What are different decision-making statements in Python? Write a program to produce Fibonacci series in Python?
- How do you define tuples in python language? What is a dictionary and how can you implemented it in Python?
- Where do you need to use multiple assignments? Discuss different inbuilt I/O functions of Python. Brief about non-associative operations.
- Elaborate Inheritance in python with an example. How are classes created in Python?
- What is the need for exception handling in a programming language? How can a user define exceptions in a python program? Write a program to elaborate.
- What is a Python module? What do file-related modules in Python do? Can you name some file-related modules in Python? Write a sample program using Python module to display calendar.
- What is functional programming? List a few methods to implement to functionally oriented programming in python? Write down a program in python to read and write a text file.
Answers:
Q1. What is namespace in Python?
Ans: - A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary. Let’s go through an example, a directory-file system structure in computers. Needless to say, that one can have multiple directories having a file with the same name inside every directory. But one can get directed to the file, one wishes, just by specifying the absolute path to the file.
Real-time example, the role of a namespace is like a surname. One might not find a single “Alice” in the class there might be multiple “Alice” but when you particularly ask for “Alice Lee” or “Alice Clark” (with a surname), there will be only one (time being don’t think of both first name and surname are same for multiple students).
Q2. What are various python modules? List some commonly used built-in modules in python.
Ans: - Any text file with the .py extension containing Python code is basically a module. Different Python objects such as functions, classes, variables, constants, etc., defined in one module can be made available to an interpreter session or another Python script by using the import statement. Functions defined in built-in modules need to be imported before use. On similar lines, a custom module may have one or more user-defined Python objects in it. These objects can be imported in the interpreter session or another script.
Q3. What are the inbuilt functions for data type conversion in python?
- int(a, base): This function converts any data type to integer. ‘Base’ specifies the base in which string is if the data type is a string.
- float(): This function is used to convert any data type to a floating-point number
- ord() : This function is used to convert a character to integer.
- hex() : This function is to convert integer to hexadecimal string.
- oct() : This function is to convert integer to octal string.
- tuple() : This function is used to convert to a tuple.
- set() : This function returns the type after converting to set.
- list() : This function is used to convert any data type to a list type.
- dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
- str() : Used to convert integer into a string.
- complex(real,imag) : This function converts real numbers to complex(real,imag) number.
- chr(number): This function converts number to its corresponding ASCII character.
Q4. Differentiate between Python Arrays and lists.
Q5. Why do we use ‘Self’ in python?
- Example: -
- Output: -
Q6. Define the use of python iterators.
Ans: - An iterator in Python is an object that contains a countable number of elements that can be iterated upon. In simpler words, we can say that Iterators are objects that allow you to traverse through all the elements of a collection and return one element at a time. More specifically, we say that an iterator is an object that implements the iterator protocol. We are going to discuss this protocol in the later section
- __iter(iterable)__ method that is called for the initialization of an iterator. This returns an iterator object
- next ( __next__ in Python 3) The next method returns the next value for the iterable. When we use a for loop to traverse any iterable object, internally it uses the iter() method to get an iterator object which further uses next() method to iterate over. This method raises a StopIteration to signal the end of the iteration.
Q7. Where do we use help () and dir () functions in python languages?
- help(): - In Python, help() is a super useful built-in function that can be used to return the Python documentation of a particular object, method, attributes, etc.
- example
- output
- dir(): - In python, dir() shows a list of attributes for the object passed in as argument , without an argument it returns the list of names in the current local namespace (similar to locals().keys() ) .
- example
- output
Q8. How can files be deleted in Python?
- Example
Q9. How do we handle exceptions in Python?
Ans: - An exception is an error which happens at the time of execution of a program.
The cause of an exception is often external to the program itself. For example, an incorrect input, a malfunctioning IO device etc. Because the program abruptly terminates on encountering an exception, it may cause damage to system resources, such as files. Hence, the exceptions should be properly handled so that an abrupt termination of the program is prevented.
Python uses try and except keywords to handle exceptions. Both keywords are followed by indented blocks.
The try: block contains one or more statements which are likely to encounter an exception. If the statements in this block are executed without an exception, the subsequent except: block is skipped.
If the exception does occur, the program flow is transferred to the except: block. The statements in the except: block are meant to handle the cause of the exception appropriately. For example, returning an appropriate error message.
Q10. How does break continue and pass work?
- The break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. In other words, we can say that break is used to abort the current execution of the program and the control go to the next line after the loop. The break is commonly used in the cases where we need to break the loop for a given condition.
- The continue statement in Python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and starts with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition. The continue statement in Python is used to bring the program control to the beginning of the loop.
- The pass statement is a null operation since nothing happens when it is executed. It is used in the cases where a statement is syntactically needed but we don't want to use any executable statement at its place.
Section B
Q1. How do you install Python on Windows and set path variable? What is the process of compilation and linking in python?
Q2. What are different decision-making statements in Python? Write a program to produce Fibonacci series in Python?
- WAP to produce Fibonacci series in Python
- Output: -
Q3. How do you define tuples in python language? What is a dictionary and how can you implemented it in Python?
Ans: -Q4. Where do you need to use multiple assignments? Discuss different inbuilt I/O functions of Python. Brief about non-associative operations.
- Python allows you to assign a single value to several variables simultaneously. For example: a = b = c = 1 Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location.
- You can also assign multiple objects to multiple variables. For example: a, b, c = 1, 2, “john” Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value “john” is assigned to the variable c.
Section C
Q1. Elaborate Inheritance in python with an example. How are classes created in Python?
- Code reusability- we do not have to write the same code again and again, we can just inherit the properties we need in a child class.
- It represents a real world relationship between parent class and child class.
- It is transitive in nature. If a child class inherits properties from a parent class, then all other sub-classes of the child class will also inherit the properties of the parent class.
- Single Inheritance - When a child class inherits only a single parent class.
- Multiple Inheritance - When a child class inherits from more than one parent class.
- Multilevel Inheritance - When a child class becomes a parent class for another child class.
- Hierarchical Inheritance - Hierarchical inheritance involves multiple inheritance from the same base or parent class.
- Hybrid Inheritance - Hybrid inheritance involves multiple inheritance taking place in a single program.
Q2. What is the need for exception handling in a programming language? How can a user define exceptions in a python program? Write a program to elaborate.
Q3. What is a Python module? What do file-related modules in Python do? Can you name some file-related modules in Python? Write a sample program using Python module to display calendar.
Program using Python module to display calendar
- Output: -
Q4. What is functional programming? List a few methods to implement to functionally oriented programming in python? Write down a program in python to read and write a text file.
- High level: You’re describing the result you want rather than explicitly specifying the steps required to get there. Single statements tend to be concise but pack a lot of punch.
- Transparent: The behavior of a pure function depends only on its inputs and outputs, without intermediary values. That eliminates the possibility of side effects, which facilitates debugging.
- Parallelizable: Routines that don’t cause side effects can more easily run in parallel with one another.
- Map - The map function allows us to apply a function to every element in an iterable object.
- Filter - The filter function tests every element in an iterable object with a function that returns either True or False, only keeping those which evaluates to True.
- Lambda functions: In Python, anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions.
- Immutability: Immutability is a functional programming paradigm can be used for debugging as it will throw an error where the variable is being changed not where the value is changed. Python too supports some immutable data types like string, tuple, numeric, etc.
Comments
Post a Comment