0% found this document useful (0 votes)
26 views

Assignment 2

The document contains questions about Python programming concepts like functions, classes, exceptions, files and strings. It asks to write code snippets to demonstrate understanding of these concepts.

Uploaded by

Elavazha Gan
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)
26 views

Assignment 2

The document contains questions about Python programming concepts like functions, classes, exceptions, files and strings. It asks to write code snippets to demonstrate understanding of these concepts.

Uploaded by

Elavazha Gan
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/ 5

ASSIGNMENT 2

1] What will be the output of the following Python code?

def change(a):

b=[x*2 for x in a]

print(b)

def change(a):

b=[x*x for x in a]

print(b)

from mod1 import change

from mod2 import change

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

2] Is the following code valid?


try:

# Do something

except:

# Do something

finally:

# Do something
a) no, there is no such thing as finally

b) no, finally cannot be used with except

c) no, finally must come before except

d) yes

3] What is the output of the following code?


def foo():
try:
return 1
finally:
return 2
k =foo()
print(k)
a) 1
b) 2
c) 3
d) error, there is more than one return statement in a single try-finally block

4] What is the output of the code shown below?


x=10
y=8

assert x>y,'X too small'

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

7] What will be the output of the following Python code?


class test:
def __init__(self):
self.variable = 'Old'
self.Change(self.variable)
def Change(self, var):
var = 'New'
obj=test()
print(obj.variable)
a) Error because function change can’t be called in the __init__ function
b) ‘New’ is printed
c) ‘Old’ is printed
d) Nothing is printed

8] What is the use of seek() method in files?


a) sets the file’s current position at the offset
b) sets the file’s previous position at the offset
c) sets the file’s current position within the file
d) none of the mentioned

9] Which of the following is not a valid mode to open a file?


a) ab
b) rw
c) r+
d) w+

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

You might also like