PPS Unit Test-2 Model Answersheet 2022
PPS Unit Test-2 Model Answersheet 2022
Object :
An object (instance) is an instantiation of a class. When class is defined, only the
description for the object is defined. Therefore, no memory or storage is allocated.
The example for object of met class can be:
Example :
# defining object
m1 = met( )
Print(M1.x)
In this example, object is created as m1 for class met
OR
MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu
Programming and Problem Solving FE 2019 Computer Engineering
Q.2 a) How Data hiding is done in python? Give example code. 4-M
Abstraction in python is defined as a process of handling complexity by hiding
unnecessary information from the user.
This is one of the core concepts of object-oriented programming (OOP) languages.
That enables the user to implement even more complex logic on top of the provided
abstraction without understanding or even thinking about all the hidden background/back-
end complexity.
Example :
from abc import xyz,
class ClassName(xyz):
Abstraction is used to hide the internal functionality of the function from the users. The
users only interact with the basic implementation of the function, but inner working is
hidden. User is familiar with that "what function does" but they don't know "how it
does."
b) Explain the significance of the init( ) method & Self object with example 5-M
The __init__() Function
All classes have a function called __init__(), which is always executed when the class is
being initiated.
Use the __init__() function to assign values to object properties, or other operations that
are necessary to do when the object is being created.
Example :
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Note: The __init__() function is called automatically every time the class is being used
to create a new object.
MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu
Programming and Problem Solving FE 2019 Computer Engineering
c) Write a python program to create class car with two attributes name
& cost. Create two objects and display information. 6-M
class Car:
name = "" # class attributes
cost = 0
OUTPUT :
***DISPLAYING CAR DETAILS***
MARUTI 120000
SUV 200000
Q.3 a) What is directory? List directory methods and explain any one. 4-M
A directory or folder is a collection of files and subdirectories. Python has the os
module that provides us with many useful methods to work with directories (and files as
well).
Directory Methods :
Display current working directory
Creating Directory
Renaming Directory
Changing Directory
Removing Directory
1. Creating Dictionary : The dictionary can be created by using multiple key-value pairs
enclosed with the curly brackets {}, and each key is separated from its value by the colon
(:).The syntax to define the dictionary is given below.
Syntax :
Dict = {"Name": "Tom", "Age": 22}
In the above dictionary Dict, The keys Name and Age are the string that is an immutable
object.
Adding an item to the dictionary is done by using a new index key and assigning a value to
it:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
O/p = {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
c) Write a python prog. To counts the no. of tabs and new line characters in a file. 6-M
# Opening a file
file = open("input.txt","r")
tab_count = 0
line_count = 0
for i in Content:
if i=='\t':
tab_count += 1
elif i=='\n':
line_count += 1
Input.txt
I am Mr. Anand Gharu
Computer Department
MET IOE nashik
OR
Q.4 a) Why do we need files? Explain relative and absolute path in files. 4-M
Definition : “A file is an essential data item stored in one’s computer. Each file can be
characterized with its filename & file extension.”
A file or a computer file is a chunk of logically related data or information which can be
used by computer programs. Usually a file is kept on a permanent storage media, e.g. a
hard drive disk. A unique name and path is used by human users or in programs or scripts
to access a file for reading and modification purposes.
1. Absolute file paths are notated by a leading forward slash or drive label. For
example, /home/example_user/example_directory or C:/system32/cmd.exe. An
absolute file path describes how to access a given file or directory, starting from the
root of the file system. A file path is also called a pathname.
2. Relative file paths are notated by a lack of a leading forward slash. For example,
example_directory. A relative file path is interpreted from the perspective your
current working directory. If you use a relative file path from the wrong directory,
then the path will refer to a different file than you intend, or it will refer to no file at
all.
1. Open file :
Python has a built-in open() function to open a file. This function returns a file object, also
called a handle, as it is used to read or modify the file accordingly.
Syntax:
file_object = open(file_name, mode)
Here, file_name is the name of the file or the location of the file that you want to open, and
file_name should have the file extension included as well. Which means in test.txt – the
term test is the name of the file and .txt is the extension of the file.
MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu
Programming and Problem Solving FE 2019 Computer Engineering
Example1 :
fo = open(“C:/Documents/Python/test.txt”, “r+”)
In the above example, we are opening the file named ‘test.txt’ present at the location
‘C:/Documents/Python/’ and we are opening the same file in a read-write mode which
gives us more flexibility.
Example2 :
fo = open(“C:/Documents/Python/img.bmp”, “rb+”)
In the above example, we are opening the file named ‘img.bmp’ present at the location
“C:/Documents/Python/”, But, here we are trying to open the binary file..
2. Close file :
In order to close a file, we must first open the file. In python, we have an in-built method
called close() to close the file which is opened.
Whenever you open a file, it is important to close it, especially, with write method.
Because if we don’t call the close function after the write method then whatever data we
have written to a file will not be saved into the file.
Example-1 :
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
my_file.close()