Assignment 2
Assignment 2
def change(a):
b=[x*2 for x in a]
print(b)
def change(a):
b=[x*x for x in a]
print(b)
s=[1,2,3]
change(s)
a) [2,4,6]
b) [1,4,9]
c) [2,4,6]
[1,4,9]
d) There is a name clash
# Do something
except:
# Do something
finally:
# Do something
a) no, there is no such thing as finally
d) yes
a) Assertion Error
b) 10 8
c) No output
d) 108
5] What happens if the file is not found in the code shown below?
a=False
while not a:
try:
f_n=input("Enter file name")
i_f=open(f_n,'r')
except:
print("Input file not found")
a) No error
b) Assertion error
c) Input output error
d) Name error
6] What will be the output of the following Python code?
class test:
def __init__(self,a="Hello World"):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
a) The program has an error because constructor can’t have default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have parameters
10] How do you change the file position to an offset value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned
11] What is the output of the following?
i =1
whileFalse:
if i%2==0:
break
print(i)
i +=2
a) 1
b) 1 3 5 7 …
c) 1 2 3 4 …
d) none of the mentioned
12] Write a Python program to Count the Number of Words in a Text File
13] Write a Python program to count the number of lines in a text file.
14] Write a Python program to print the even numbers from a given list.
15] Write a Python function to check whether a number is in a given range.
16] Write a Python program to reverse a string.
17] Write a Python function to create and print a list where the values are square
of numbers between 1 and 10