PES University, Bengaluru UE20CS101
(Established under Karnataka Act No. 16 of 2013)
APRIL 2021: IN SEMESTER ASSESSMENT B Tech 1 SEMESTER
PHYSICS CYCLE
UE20CS101 (4 credit subject) - Python for Computational Problem Solving
Time: 3 Hrs Answer All Questions Max Marks: 100
1 a) Evaluate the following expressions: 5
. i) print( 3 * 2 ** 2 // -2 ) -6
ii) x = 5; y = 7; print(x | y | x) 0101,0111=0111,0101=7
iii) print( ~~~5 * 2) -10
iv) print( [10,11] in [10,11,12,13]) False
v) t = 0; v = 8; print( t or v ) 1000=8
b) Write a program that implements the logic demonstrated in the following image 6
(Accept only YES or NO as input from the user)
c) State whether True or False: 5
i) There is only one best solution for any given computational problem False
ii) The decimal equivalent of 0100110 is 40 False
iii) There is no practical limit to the size of an integer in Python. True
iv) All if statements must include an else header False
v) In Python, a positive sign may be placed in before or after numeric values, to
indicate positive numeric values. False,can't be placed after
d) What is the output? 4
i) ii)
0
i=0 1 i=1
while i < 3: 2 while False:
print(i) 0 if i%2 == 0:
i += 1 break
else: print(i)
print(0) i += 2
iii)
x = 123 Error
for i in x:
print(i)
2 a) Write a program to list the all the words that start with the same alphabet (Use 6
. dictionary type)
Example:
string = "how much wood would a wood chuck chuck if a wood chuck could chuck
wood"
output : {'h': {'how'}, 'm': {'much'}, 'w': {'wood', 'would'}, 'a': {'a'}, 'c': {'could', 'chuck'},
'i': {'if'}}
b) What is the output for the following? 8
i) ii) (2+2+
s = ‘1 2 3 4 5’ s = [ ‘ab’ , ‘cd’ ] 1+1+2
for ch in s.split(‘ ’)[:-1]: for i in s: )
print(ch , end = ‘,’) i = i.upper()
print( i , end = ‘ ’)
iii) s1 = {1,3,5} ; s2 = {5,6,7} ;print(s1 ^ s2)
iv) a = [2,4,3,6,5] ; a.pop(2) ; print(a)
v)
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1],end = ',' )
c) Write a function that returns n Fibonacci numbers 6
(example: for n = 5 , output : 0 1 1 2 3 )
3 a) Write a function that reverses a string using recursion 6
. b) Read the contents of a file and display the lines from the file with line numbers. 6
Output:
1. Betty had some butter,
2. But the butter was bitter,
…
c) Write the output for the following: 2+2
i)
def outer():
5
a = 10 5
def inner():
global a
a=5
print(a)
a = 15
outer()
print(a)
ii) consider the module
test.py:
def increment(n):
return n+1
illustrate different ways to import and use the increment function in a python program
d) Explain with examples the syntax for the following functions in the tkinter module : 4
i) Label() ii) Entry()
4 a) i) Write a program using List comprehension and selection control to check if a given 3+2+1
. number is prime or not.
ii) What is the output?
a)
li=[1, -2, -3, 4, 5]
def fn(x): -2,-3
return x<-1
m=map(fn, li)
print(list(m))
b)
x = [43, 32]
print(len(''.join(list(map(str, x)))))
b) Write a user defined function to mimic filter and use it to remove numbers divisible by 6
3 from a given list of numbers.
numbers = [1,2,3,4,5,6]
output: [1,2,4,5]
c) What is the output of the following code? 4
class myRange:
def __init__(self,n):
self.limit = n
def __iter__(self):
self.i = 0
return self
def __next__(self):
self.i += 1
return self.i-1
r = myRange(5)
t = iter(r)
print(next(t))
print(next(t))
u = iter(r)
print(next(u))
print(next(t))
d) i) Explain with example any 3 commands used in debugging 3+1
ii) Write the command used to open the python debugger from the command
prompt/terminal
5 a) Create a class Queue that implements the working of a queuing system. The class 8
will have attributes such as limit: maximum number of people in the queue (consider
the limit as 10) and people_queue: which is a list on names in a queue.
Implement the following methods:
enter_queue : that adds the name of a person to the end of the queue and also
indicates that the queue is full if there are already 10 people in the queue and
exit_queue: that removes one person from the front of the queue and indicate if the
queue is empty and there are no more people to exit the queue.
b) i) Differentiate between try-except-else and try-except-finally 6
(2+2+
ii) what is the output of the following code? 2)
a) b)
def div(a,b): def example(a):
try: try:
return a/b return a
except: finally:
return ('Div by Zero Error') return ‘No Error’
else: print(example(5))
return "No Error"
print(div(10,2)) 5
No error
c) Given a list k = [1,4,0,5,3] , write a program that calculates the reciprocal of every 6
element in the list and store it in another list called reciprocal. Incorporate exception
handling to handle the division by zero error when calculating the reciprocal of 0
(zero)