[go: up one dir, main page]

0% found this document useful (0 votes)
41 views9 pages

PPS Unit 5

Pps

Uploaded by

rohitgagare99
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)
41 views9 pages

PPS Unit 5

Pps

Uploaded by

rohitgagare99
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/ 9

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 – I
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 : 30/06/2022

MODEL ANSWERSHEET-2022

Q. 1 a) What is function? Explain with its Syntax and Example? 4-M


 Definition : “A function is a block of code which only runs when it is called”.
 You can pass data, known as parameters(Argument), into a function. 1-Mark
 A function can return data as a result.
 It is also called method or procedure

Syntax : 1-Mark
def my_function(parameters):
function_block
return expression

Example : 2-Mark
#function definition
def hello_world():
print("hello world")
# function calling
hello_world()

Output:
hello world

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


Programming and Problem Solving FE 2019 Computer Engineering
b) Define Argument in Function? Explain its Types in Function with e.g. 5-M
 Definition : The arguments are types of information which can be passed into the
function. The arguments are specified in the parentheses. We can pass any number of
arguments, but they must be separate them with a comma. 1-Mark

Types of Argument :
1. Default Arguments
2. Keyword Arguments
3. Positional Arguments
4. Arbitrary Positional Arguments
5. Arbitrary Keyword Arguments

1. Default Argument : 2-Mark


Python allows function parameters to have default values. This is useful in case a
matching argument is not passed in the function call statement.
Example :
def sayhello(name = "World"):
print("Hello,", name)
sayhello()
sayhello(“METIOE")
The output looks like this:
Hello, World

2. Keyword parameter Argument : 2-Mark


Python offers a way to write any argument in any order if you name the arguments
when calling the function. This allows us to have complete control and flexibility
over the values sent as arguments for the corresponding parameters.
Example :
def multiply(a, b):
return a*b

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


Programming and Problem Solving FE 2019 Computer Engineering
print(multiply(a = 10, b = 5))
print(multiply(b = 20, a = 9))
The output is:
50
180

c) Differentiate between Local & Global variable. Write a python program to


demonstrate difference between local and global variable. 6-M
 4-Mark
Sr. Sr.
Local Variable Global Variable
No. No.
Local variables are those which are Global variables are those which are
1 1
defined inside a function. not defined inside any function.
2 It has local scope 2 It has a global scope
Local variables are accessible only The global variables are accessible
3 inside the function in which it was 3 throughout the program and inside
initialized. every function.
Local variables are those which are Global variables are those which are
4 4
initialized inside a function. initialized outside a function.
Local scope is defined as Local Global scope is defined as Global
5 5
Variable Variable

Example : 2-Mark
def my_func():
x = 10 #Local Variable
print("Value inside function:",x)
x = 20 #Global Variable
my_func()
print("Value outside function:",x)

Output :
Value inside function: 10
Value outside function: 20

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


Programming and Problem Solving FE 2019 Computer Engineering
OR
Q.2 a) Explain Lambda Function with example. 4-M
 Definition : In Python, an anonymous function is a function that is defined without
a name. 1-Mark

• While normal functions are defined using the def keyword in Python, anonymous
functions are defined using the lambda keyword.
• Hence, anonymous functions are also called lambda functions. 1-Mark

Syntax
lambda arguments : expression

Example : 2-Mark
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
Output : 15

b) Write python program using function to find whether number is odd/even.5-M


 Program : 5-Mark
num=int(input("Enter a number for check odd or even: "))
def find_Evenodd(num):

if(num%2==0):
print(num," Is an even")
else:
print(num," is an odd")

find_Evenodd(num);//function call

Output :
Enter a number for check odd or even: 349
349 is an odd

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


Programming and Problem Solving FE 2019 Computer Engineering
c) Explain DocString with e.g.& Compare Between Comment and DocString.6-M
 “Docstring is a short form of documentation strings.” 2-Mark
• A docstring in Python is a way to provide an explanation along with functions,
modules, and classes.
• They are documentation strings that are used as comments.
• Docstrings always start with a capital letter and end with a full stop. It can be a
single line or a multiline comment.
Example :

4-Mark

Comments Docstrings
A hash symbol (#) is used to mention the They are written between double or triple
initiation of a comment quotes
These are special strings that are used for
These are basically statements that are used for
providing documentation in Python
describing what a particular line of code means
programs

There are only single-line comments. Multi-line


There are single line and multiple line
comments can be inserted in programs using
docstrings
multiple single-line comments

Comments are ignored by interpreters and Compilers and interpreters execute


compilers in Python docstrings

Comments are not visible after the program has You can see the docstrings using the
been executed __doc__ attribute

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


Programming and Problem Solving FE 2019 Computer Engineering

Q.3 a) Why String are Immutable? Justify? 4-M


 In python, the string data types are immutable. Which means a string value cannot
be updated/Changed. We can verify this by trying to update a part of the string which
will led us to an error. 2-Mark
Program :
# Can not reassign 2-Mark
t= "Metioenashik"
print(type(t))
t[0] ="N"When we run the above program,

we get the following output −


t[0] = “N"
TypeError: 'str' object does not support item assignment

b) Explain Membership Operator in Python with example. 5-M


 Definition : “Membership operators are used to test if a sequence is presented in
an object” 1-Mark
Python offers two membership operators to check or validate the membership
of a value. It tests for membership in a sequence, such as strings, lists, or tuples.

1. in Operator : 2-Mark
To check if a certain phrase or character is present in a string, we can use the
keyword in.
Example :
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
Output : True

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


Programming and Problem Solving FE 2019 Computer Engineering
2. not in Operator : 2-Mark
To check if a certain phrase or character is NOT present in a string, we can use the
keyword not in.
Example
Check if "expensive" is NOT present in the following text:
txt = "The best things in life are free!"
print("expensive" not in txt)
Output : True

c) Write a python prog. to display tables from 1 to 10 using formatting


character. 6-M
 Program : 4-Mark
# Multiplication table of 1 to 10

for i in range(1,11):
print("\n\nMULTIPLICATION TABLE FOR %d\n" %(i))
for j in range(1,11):
print("%-5d X %5d = %5d" % (i, j, i*j))

Formatting Operator : 2-Mark


Python allows us to use the format specifiers used in C's printf statement. The format
specifiers in Python are treated in the same way as they are treated in C. However,
Python provides an additional operator %, which is used as an interface between the
format specifiers and their values. In other words, we can say that it binds the format
specifiers to the values.
OR

Q.4 a) Explain String with its operation(Concatenation, Multiplication) 4-M


 Definition : “A string is a sequence of characters.”
A character is simply a symbol. For example, the English language has 26 characters.

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


Programming and Problem Solving FE 2019 Computer Engineering
1. Concatenation : 2-Mark
Joining of two or more strings into a single one is called concatenation.
The + operator does this in Python. Simply writing two string literals together also
concatenates them.
The * operator can be used to repeat the string for a given number of times.

2. Multiplication :
To repeat a string in Python, We use the asterisk operator ” * ” The asterisk. is used
to repeat a string n (number) of times. Which is given by the integer value ” n ” and
creates a new string value. It uses two parameters for an operation: the integer value
and the other is String.

Example : 2-Mark
# Python String Operations
str1 = 'Hello'
str2 ='World!'
# using +
print('str1 + str2 = ', str1 + str2) //concatenation
# using *
print('str1 * 3 =', str1 * 3) //Multiplication

Output :
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello

b) What will be the output of following statement S = “Welcome to Python”. 5-M


i) Print (s[1:9])
ii) Print (s[ :6])
iii) Print (s[4: ])
iv) Print (s[1: –1])
v) Print (“Come” not in str)

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


Programming and Problem Solving FE 2019 Computer Engineering
 Output :
i) elcome t 1-Mark
ii) Welcom 1-Mark
iii) ome to python 1-Mark
iv) elcome to python 1-Mark
v) True 1-Mark
c) Explain following String Methods with example.
i) Join ii) Find iii) Swapcase 6-M
 1. Join : The join() string method returns a string by joining all the elements of an
iterable (list, string, tuple), separated by a string separator. 2-Mark
Example :
text = ['Python', 'is', 'a', 'fun', 'programming', 'language']

# join elements of text with space


print(' '.join(text))

# Output: Python is a fun programming language

2. Find : The find() method returns the index of first occurrence of the
substring (if found). If not found, it returns -1. 2-Mark
Example :
message = 'Python is a fun programming language'

# check the index of 'fun'


print(message.find('fun'))

# Output: 12

3. Swapcase : The string swapcase() method converts all uppercase characters


to lowercase and vice versa of the given string, and returns it. 2-Mark

Example :
string = "MetIoe"
# prints after swapping all cases
print(string.swapcase())
Output : mETiOE

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

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

You might also like