0% found this document useful (0 votes)
53 views11 pages

PPS Unit Test-2 Model Answersheet 2022

Uploaded by

tg467900
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)
53 views11 pages

PPS Unit Test-2 Model Answersheet 2022

Uploaded by

tg467900
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
You are on page 1/ 11

Programming and Problem Solving FE 2019 Computer Engineering

Mumbai Education Trust’s


INSTITUTE OF ENGINEERING, BKC, NASHIK.
F.E. (Computer Engineering)
PROGRAMMING AND PROBLEM SOLVING (2019 Pattern)
UNIT TEST – II MODEL ANSWERSHEET 2022
Time : 1 Hour [Max. Marks : 30]
Instructions to the candidates:
1) Answer Q.1 or Q.2, Q.3 or Q.4
2) Neat diagrams must be drawn wherever necessary.
3) Assume suitable data if necessary.
4) Figures to the right indicate full marks. Date : 06/08/2022
Q. 1 a) Define and List programming paradigm. Explain any one. 4-M
 The programming paradigm is the way of writing computer programs. There are four
programming paradigms and they are as follows.
1. Monolithic programming paradigm
2. Structured-oriented programming paradigm
3. Procedural-oriented programming paradigm
4. Object-oriented programming paradigm

The Monolithic programming paradigm is the oldest. It has the following


characteristics. It is also known as the imperative programming paradigm.
1. In this programming paradigm, the whole program is written in a single block.
2. We use the goto statement to jump from one statement to another statement.
3. It uses all data as global data which leads to data insecurity.
4. There are no flow control statements like if, switch, for, and while statements.
5. There is no concept of data types.
6. An example of a Monolithic programming paradigm is Assembly language.

b) Explain Class and Object with an example. 5-M


 Class :The class can be defined as a collection of objects. It is a logical entity that
has some specific attributes and methods. For example: if you have an employee
lass, then it should contain an attribute and method, i.e. an email id, name, age,
salary, etc.

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering
Syntax :
class ClassName:
<statement-1>
.
<statement-N>
Example :
# demonstrate defining
# a class
class met:
x=5
In the above example, we have created a class named met using the class keyword.

 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

c) Define Constructor and Explain Types of constructor with example. 6-M



1. A constructor is a unique function that gets called automatically when an object is
created of a class.

2. The main purpose of a constructor is to initialize or assign values to the data


members of that class.
3. It cannot return any value other than none

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering
Syntax of Python Constructor
def __init__(self):
#initialisations
__init__ is one of the reserved functions in Python. In Object Oriented Programming,
it is known as a constructor.

Types of Constructor in Python


1. Parameterized Constructor
2. Non-Parameterized Constructor
3. Default Constructor

1. Parameterized Constructor in Python


• When the constructor accepts arguments along with self,it is known as
parameterized constructor.
• These arguments can be used inside the class to assign the values to the data
members.
1. An object of the class Family is created. It has a variable known as members.
2. When the object is created, a parameter (here it is 10) is passed as arguments.
3. This parameter (10 as in above example) is taken up by the constructor as the object
is created.
4. The number 10 is assigned to the variable count which is further assigned to
self.members.
5. The self.members can be used within the class to print the data.

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering
Example :

2. Non-Parameterized Constructor in Python


When the constructor doesn't accept any arguments from the object and has only one
argument self in the constructor, it is known as a non-parameterized constructor.
Explaination :
In the above example, we initialise our favourite fruit as Apple inside the class. But we
want to change it as soon as the object gets created.
This can be done by re-assigning the value to the constructor. This type of constructor
doesn't take any other argument other than self. It can be used to print and check whether
our value got changed.
Example :

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

# initializer with instance attributes


def __init__(self, name, cost):
self.name = name
self.cost = cost
print("***DISPLAYING CAR DETAILS***")
c1 = Car('MARUTI', 120000) # object created
c2 = Car('SUV', 200000)
print(c1.name,c1.cost) # print information
print(c2.name,c2.cost)

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

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering
1. Get Current Directory
We can get the present working directory using the getcwd() method of the os module.
This method returns the current working directory in the form of a string. We can also use
the getcwdb() method to get it as bytes object.
Example :
Print( os.getcwd())

b) What is Dictionary(Method)? Explain any 2 function. 5-M


 Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
Dictionaries are written with curly brackets { }, and have keys and values:
Example : Create and print a dictionary:
thisdict = {
"name": "anand",
"college": "METIOE",
"passout": 2011
}
print(thisdict)
O/p = {'name': 'anand', 'college': 'METIOE', 'passout': 2011}

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.

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering
2. Adding Items :

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

# Reading from file


Content = file.read()

for i in Content:
if i=='\t':
tab_count += 1

elif i=='\n':
line_count += 1

print("This is the number of tabs in the file", tab_count)


print("This is the number of lines in the file", line_count)

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering

Input.txt
I am Mr. Anand Gharu
Computer Department
MET IOE nashik

Output: This is the number of tabs in the file 1


This is the number of lines in the file 3

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.

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu


Programming and Problem Solving FE 2019 Computer Engineering
b) Write python prog that reads data from one file and write into another file. 5-M
 f1 = open(“Input.txt”, “r”)
f2 = open(“Output.txt”, “w”)

for line in f1:


f2.write(line)
Input.txt
I am Mr. Anand Gharu
Output.txt
I am Mr. Anand Gharu

c) Explain different file operations in details with examples.(Any two). 6-M


 Most importantly there are 4 types of operations that can be handled by Python on
files:
1. Open
2. Read
3. Write
4. Close

Other operations include:


1. Rename
2. Delete

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()

*************************** BEST OF LUCK *******************************

MET’S Institute of Engineering, BKC, Nashik Prepared by : Prof. Anand N. Gharu

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy