Pps Solved 2019 November
Pps Solved 2019 November
Pps Solved 2019 November
Q1)
a) What is a function? Explain code reuse. Explain with an example Docstring. [6]
Code reuse refers to the practice of using existing code for new purposes, rather than
rewriting it from scratch.
```python
def greet(name):
"""
"""
print(greet.__doc__)
```
A lambda function is a small anonymous function defined using the lambda keyword. It can take any
number of arguments but can only have one expression. They are often used when you need a
simple function for a short period of time.
Example:
``` PYTHON
# R EGULAR FUNCTION
DEF ADD ( X , Y ):
RETURN X +Y
```
c) Write a Python program using a function to find the greatest of three numbers by passing
numbers as arguments. [6]
```python
"""
"""
return num1
return num2
else:
return num3
```
OR
Q2)
a) Differentiate between Local & Global variables. Write a Python program to demonstrate the
difference between local and global variables. [6]
Local variables are defined within a function and can only be accessed inside that function.
Global variables are defined outside of any function and can be accessed throughout the
program.
```python
# Global variable
global_var = 10
def function():
# Local variable
local_var = 20
# Trying to access local variable outside the function will raise an error
function()
```
```python
print(message, name)
```
c) Write a Python program using a function to find whether a number is odd or even. [6]
Here's a Python program to find whether a number is odd or even using a function:
```python
def check_odd_even(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
number = 7
result = check_odd_even(number)
i) Rindex
ii) Zfill
iii) Split
Examples:
```python
s = "Hello, World!"
print(s.rindex('o')) # Output: 8
```
--------------------------------------------------------------------------------------------------------------------------------------
b) Write a Python program to display tables from 1 to 10 using formatting characters. [6]
```python
print()
```
c) What will be the output of the following statement S = “Welcome to Python”. [5]
iii) print(s[4:]) - Outputs "ome to Python", slicing from index 4 to the end.
iv) print(s[1:-1]) - Outputs "elcome to Pytho", slicing from index 1 to the second last character.
v) print("Come" not in str) - Outputs True, as "Come" is not present in the string.
OR
Q4)
Examples:
```python
# Join
# Enumerate
# Strip
print(" Hello ".strip()) # Output: Hello
```
b) Write a Python program to find whether a given character is present in a string or not. In case it
is present, print the index at which it is present. Do not use built-in string methods. [6]
```python
if c == char:
return index
return -1
char = "o"
if index != -1:
else:
```
c) Write a Python program to check whether a given string starts with a specified character. [5]
if string[0] == char:
return True
else:
return False
char = "H"
if starts_with(string, char):
else:
```
--------------------------------------------------------------------------------------------------------------------------------------
Q5)
a) Define programming paradigm. List programming paradigms. Explain any one. [6]
1. Imperative
2. Declarative
3. Procedural
4. Functional
5. Object-oriented
Inheritance is a key concept in object-oriented programming that allows a class (subclass) to inherit
properties and behavior from another class (superclass). It promotes code reuse by enabling the
subclass to inherit the attributes and methods of the superclass without needing to redefine them.
For example, consider a superclass Shape with attributes and methods common to all shapes, such
as area() and perimeter(). Now, subclasses like Circle, Rectangle, and Triangle can inherit these
attributes and methods from Shape and only need to define their unique characteristics. This
reduces redundancy and promotes efficient code maintenance.
c) Write a Python program that uses a class to store exam numbers and marks of four subjects. Use
a list to store the marks of four subjects. [6]
```python
class Exam:
self.exam_number = exam_number
self.marks = marks
exam_number = "E001"
```
In this program, we define a class `Exam` with attributes `exam_number` and `marks`. We create an
object `exam1` of the `Exam` class, passing exam number and marks as arguments. We use a list to
store the marks for four subjects. Finally, we print the exam number and marks for four subjects.
OR
Q6)
- Data Abstraction: Data abstraction is the process of hiding the implementation details and showing
only the essential features of an object to the outside world. It helps in reducing programming
complexity and focusing on relevant details.
- Encapsulation: Encapsulation is the bundling of data (attributes) and methods (functions) that
operate on the data into a single unit (class). It prevents direct access to the data from outside the
class and ensures data integrity by controlling access through methods.
ii) Polymorphism:
Polymorphism is the ability of an object to take on multiple forms or behave differently in different
contexts. It allows objects of different classes to be treated as objects of a common superclass. There
are two types of polymorphism: compile-time polymorphism (method overloading) and runtime
polymorphism (method overriding).
b) With the help of an example, explain the significance of the `__init__()` method. [6]
The `__init__()` method is a special method in Python classes that is called automatically when a
new object of the class is created. It is used to initialize the attributes of the object with initial values.
The significance of the `__init__()` method lies in its ability to ensure that objects are properly
initialized when they are created, providing a convenient way to set initial values for object
attributes.
Example:
```python
class Car:
self.name = name
self.cost = cost
# Displaying information
```
This program defines a class `Car` with an `__init__` method to initialize the attributes `name` and
`cost`. It also has a method `display_info` to display the information of a car object. Two car objects,
`car1` and `car2`, are created using this class, and their information is displayed using the
`display_info` method.
c) Write a Python program to create a class Car with two attributes name & cost. Create two
objects and display information. [6]
```python
class Car:
self.name = name
self.cost = cost
def display_info(self):
car1.display_info()
print()
car2.display_info()
```
This program defines a class `Car` with an `__init__` method to initialize the attributes `name` and
`cost`. It also has a method `display_info` to display the information of a car object. Two car objects,
`car1` and `car2`, are created using this class, and their information is displayed using the
`display_info` method.
Q7
a) Write a Python program that reads data from one file and writes into another file line by line. [6]
```python
output_file.write(line)
```
Explanation:
- This code uses nested `with` statements to open two files simultaneously: 'input.txt' for reading
and 'output.txt' for writing.
- The `with` statement ensures that the files are properly closed after their respective suites finish
executing, even if an exception occurs.
- Inside the loop, each line from the input file is read and then written to the output file, effectively
copying the contents of the input file to the output file.
b) What is a directory? List any four directory methods and explain any two of them. [6]
Explanation:
- A directory is a container used to store files and other directories. It organizes files in a hierarchical
structure.
- Explanation of `os.listdir(path)`: This method returns a list of names of entries in the directory
specified by the `path` argument. It does not include the special entries '.' (current directory) and '..'
(parent directory). This method is useful for listing the contents of a directory.
- Explanation of `os.chdir(path)`: This method changes the current working directory to the specified
`path`. It allows you to navigate between different directories in the file system. This method is
helpful when you need to work with files and directories located in different locations.
c) Why do we need files? Explain relative and absolute paths in files. [5]
Explanation:
- Files are necessary for storing and organizing data persistently on a computer's storage devices.
They allow data to be saved, retrieved, and manipulated by programs. Files are essential for tasks
such as data storage, data processing, and communication between programs.
- Relative Path: A relative path specifies the location of a file or directory relative to the current
working directory. It does not include the root directory. For example, `my_folder/my_file.txt`.
- Absolute Path: An absolute path specifies the complete location of a file or directory from the root
directory of the file system. It includes the root directory. For example,
`/home/user/my_folder/my_file.txt`.
OR
Q8)
a) Write a Python program that counts the number of tabs and newline characters in a file. [6]
```python
tab_count = 0
newline_count = 0
tab_count += line.count('\t')
newline_count += line.count('\n')
```
b) Write a Python program to display the current directory, create a directory, and remove the
created directory. [6]
```python
import os
current_dir = os.getcwd()
new_dir = 'new_directory'
os.mkdir(new_dir)
os.rmdir(new_dir)
```
c) Differentiate between text and binary files. Explain any 4 access modes used in Python. [5]
- Text Files: Text files are human-readable files that store data as a sequence of characters. They
contain textual data encoded in formats like ASCII or UTF-8. Examples include .txt, .csv, and .html
files.
- Binary Files: Binary files contain data in a format that is not human-readable. They store data in a
sequence of bytes and can represent any type of data, including images, videos, executables, etc.
2. 'w' (Write): Opens a file for writing. If the file exists, it will be truncated. If it does not exist, a new
file will be created.
3. 'a' (Append): Opens a file for appending. New data will be written at the end of the file. If the file
does not exist, it will be created.
4. 'b' (Binary): Opens a file in binary mode. This mode is used when working with binary files to
prevent any newline character translation.