PYQ1
PYQ1
Duration: 3 Hours
2 Explain with example the usage of any three methods from math module. 3
5 Write a Python program to find the frequency of each word in a string using 3
dictionary.
10 Write Python code to plot histogram of marks of students stored in a list L, with 3
proper title, xlabel and ylabel using matplotlib library.
Module 1
11 a) Describe the waterfall model of software development process with a neat figure. 9
b)Write a Python script to find nCr .Use math module to find the factorial
12 a)Area of the circle given center point (x1,y1) and one point on the perimeter (x2,y2). 7
Module 2
13 a) Write a Python program to print all prime numbers less than 1000. 7
Module 3
15 a) Write a Python program to read list of positive integers into a list and separate the 6
prime and composite numbers into two different list.
16 a)Write a program to remove all duplicate elements from a list.( do not keep any copy 8
of the repeating element)
Module 4
b)Implement a class Point which represent a point (x,y) in Cartesian coordinate. Use a
constructor to initialize a point object and a member function to display the object
values. Overload + operator to add two point object.
10
b)Distinguish between function overloading and function over riding with examples
Module 5
19 a)Illustrate numpy arrays with example. How indexing, slicing and sorting is done with 6
examples
b)Write a python program to read numbers from a file named num.txt.Write all 8
positive numbers from num.txt to a file named positive.txt and all negative numbers to
a file negative.txt.
20 a) Explain how the matrix operations are done using numpy arrays 6
d)Display rno,name and total marks of all the students in the descending order of total
marks
Scheme of
Valuation/Answer Key
Scheme of evaluation
(marks in brackets) and
answers of problems/key
PART A
1 print("test") 3
print("x=%d"%x)
print("{} {}".format(x,y))
3 Extract digits 1
4 Function advantage 1
Example 2
Creating a new function can make a program smaller by eliminating repetitive code.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
[ return value(s)/expression]
5 Logic 2
Correctness 1
d=dict()
for w in S:
d[w]=d.get(w,0)+1
print("word count")
print(d)
Definition difference
Mutability
indexing
7 Class defn 1
Constructor 1
Member function 1
8 Explanation 2
Example 1
Function os.getcwd(), returns the Current Working Directory(CWD) of the file used to
execute the code, can vary from system to system.
os.listdir()
The method walk() generates the file names in a directory tree by walking the tree
either top-down or bottom-up
10 Use of matplotlib 2
L=[10,20,30,30,30,40,50,70]
plt.xlabel('mark')
plt.ylabel('frequency')
plt.title('Histogram')
plt.hist(l)
plt.show()
PART B
Answer any one full question from each module. Each question carries 14 marks
Module 1
11 a) Explanation- 6+2
Fig 1
There is much more to programming than writing lines of code. Computer Engineers
refer to the process of planning and organizing a program as Software
Development. There are several approaches to software development. One version is
known as the waterfall model.
2.Analysis: Determines what the program will do. This is viewed as the process of
clarifying the specification for the problem.
4.Implementation: The programmers write the program. This step is also called coding
phase.
5.Integration: Large programs have many parts.In the integration phase, these parts
are brought together into a smoothly functioning system as a whole.
6. Maintenance: Programs usually have a long life, a span of 5-15 years is common.
During this time, requirements change, errors are detected and minor or major
modifications are made.
Computation of factorial
b) identifiers
variables
keywords 3
Module 2
b)logic
14 a) Logic 5
b) demonstration + example
These functions are called anonymous because they are not declared in the standard
manner by using the def keyword. You can use the lambda keyword to create small
anonymous functions. 5+2
Lambda functions can have any number of arguments but only one expression. The
expression is evaluated and returned. Lambda functions can be used wherever
function objects are required.
double = lambda x: x * 2
print(double(5))
Module 3
15 a)logic 4
b)
>>> A={3,2,1,4}
>>> A
{1, 2, 3, 4, 5}
>>> B=A.copy()
>>> B
{1, 2, 3, 4, 5}
>>> B.clear()
>>> B
set()
>>> A
{1, 2, 3, 4, 5}
>>> A.discard(5)
>>> A
{1, 2, 3, 4}
>>> A.update({5,6})
>>> A
{1, 2, 3, 4, 5, 6}
>>> B={1,2}
>>> B.issubset(A)
True
>>> A.union(B)
{1, 2, 3, 4, 5, 6}
>>> A.intersection(B)
{1, 2}
>>> A.difference(B)
{3, 4, 5, 6}
16 a)logic + syntax + correctness 4+2+2
b)
today = date.today()
# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
# mm/dd/y
d3 = today.strftime("%m/%d/%y")
d4 = today.strftime("%b-%d-%Y")
outputs
d1 = 12/09/2020
d3 = 09/12/20
d4 = Sep-12-2020
Module 4
b)class defn
constructor
overloaded + operator
2
display function
2
18 a) Definition 2
Handling exception 4
Example 2
What is Exception?
An exception is an event, which occurs during the execution of a program that disrupts
the normal flow of the program's instructions. In general, when a Python script
encounters a situation that it cannot cope with, it raises an exception. An exception is
a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception
immediately otherwise it terminates and quits.
Handling an exception
If you have some suspicious code that may raise an exception, you can defend your
program by placing the suspicious code in a try: block. After the try: block, include an
except: statement, followed by a block of code which handles the problem as elegantly
as possible.
Syntax
try:
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
......................
else:
For example, we might prompt the user for the name of a file and then try to open it.
If the file doesn’t exist, we don’t want the program to crash; we want to handle the
exception:
else:
f.close()
As previously mentioned, the portion that can cause an exception is placed inside the
try block.
b) Explanation
Example overriding
2
2
Module 5
19 a)
Indexing/slicing 2
Sorting 2
import numpy as np
Indexing/slicing
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A[2])
[7 8 9]
print(A[2,2])
9
print(A[2][2])
print(A[1:,1:])
[[5 6]
[8 9]]
print(A[:2,1:])
[[2 3]
[5 6]]
b) reading file
b) import pandas as pd
df = pd.read_csv("student.csv") 1*8
print(df.head(10))
print(df.sort_values(by='name')[['rno','name']])
print(df)
print(df.sort_values(by='total',ascending=False)[['rno','name','total']])
print(df['m1'].mean(),df['m1'].var())
print(df['m2'].min(),df['m2'].max())
plt.plot(df['name'],df['m3'])
df.to_csv('studentnew.csv')