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

WS - On Python Module & File Handling - With - Soln

Python Functions

Uploaded by

S Alejtebi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

WS - On Python Module & File Handling - With - Soln

Python Functions

Uploaded by

S Alejtebi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Worksheet on Python Module & File Handling

Multiple Choice Questions


1. Which of these definitions correctly describes a module?
a) Denoted by triple quotes for providing the specification of certain program
elements
b) Design and implementation of specific functionality to be incorporated into a
program
c) Defines the specification of how it is to be used
d) Any program that reuses code
2. Which of the following is true about top-down design process?
a) The details of a program design are addressed before the overall design
b) Only the details of the program are addressed
c) The overall design of the program is addressed before the details
d) Only the design of the program is addressed
3. Which of the statements about modules is false?
a) In the “from-import” form of import, identifiers beginning with two underscores are
private and aren’t imported
b) dir() built-in function monitors the items in the namespace of the main module
c) In the “from-import” form of import, all identifiers regardless of whether they are
private or public are imported
d) When a module is loaded, a compiled version of the module with file extension .pyc
is automatically produced
4. To open a file c:\scores.txt for reading, we use
a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)
5. To read two characters from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
6. The readlines() method returns
a) str
b) a list of lines
c) a list of single characters
d) a list of integers
7. Which one of the following is not attributes of file
a) closed
b) softspace
c) rename
d) mode
8. What is the correct syntax of rename() a file?
a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned
9. Which function is used to read all the characters?
a) Read()
b) Readcharacters()
c) Readall()
d) Readchar()
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. Write a Python program to read first n lines of a file.
12. Write a Python program to read a file line by line store it into a variable.
13. Write a python program to find the longest words.
14. Write a Python program to count the frequency of words in a file.
15. Write a Python function to sum all the numbers in a list.
Sample List : (8, 2, 3, 0, 7)
Expected Output : 20
16. Write a Python program to reverse a string.
Sample String : "1234abcd"
Expected Output : "dcba4321"
17. Write a Python function that accepts a string and calculate the number of upper case
letters and lower case letters.
Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
18. Write a Python function that takes a list and returns a new list with unique elements
of the first list.
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
19. Write a Python function to check whether a string is a pangram or not.
Note : Pangrams are words or sentences containing every letter of the alphabet at
least once.
For example : "The quick brown fox jumps over the lazy dog"
20. Write a Python program to find the location of Python module sources.
Hints/Solution:-
1. (b) Hint: The term “module” refers to the implementation of specific functionality to be
incorporated into a program.

2. ( C ) In top-down approach) we started with a statement of the overall function that a system
was expected to perform , We then broke that function down into sub functions
3. (C )
4. (a) Hint: C contains file named scores.txt
f=open("C:\scores.txt.txt", "r")
text=f.read()
print(text)
f.close()

5. (a)
6. (b)
7. ( c) Hint:-
f = open('scores.txt','r')
print(f.closed) # gives output False
f.close()
print(f.closed) # gives output True

f.mode # Returns access mode with which file was opened.


f.name #Returns name of the file.
f.softspace #Returns false if space explicitly required with print, true otherwise.
8. (a)
9. (a)
10. (a) Example:-
offset=10
f=open("C:\scores.txt.txt", "r")
text=f.read()
print(text)
f.seek(offset,0)
text=f.read()
print(text)
f.close()

11. # To read first n-lines

12. #To read a file line by line -store it into a variable.

13. #To find the longest words.


f=open("C:\scores.txt.txt", "r")
text=f.read()
lst=text.split()
max_len=0
for word in lst:
if len(word)>max_len:
max_len=len(word)
print("maximum word is",[word for word in lst if len(word)==max_len])
f.close()
-----------------------------------------------------------

14. #To count frequency of words in a file


f=open("C:\scores.txt.txt", "r")
wordcount={}
for word in f.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print(k,v)
f.close()
-----------------------------------------------------------

15. #To reverse a given string


str=input("Enter a string :")
l=len(str)
rev=""
while l!=0:
rev=rev+str[l-1]
l=l-1
print( "String in reverse order is:",rev)
------------------------------------------------------------------------------
16. # To calculate the number of upper case letters and lower case letters.
str=input("Enter a string:")
n1=0
n2=0
for c in str:
if c.isupper():
n1=n1+1
elif c.islower():
n2=n2+1
else:
pass

print ("No. of Upper case characters : ", n1)


print ("No. of Lower case Characters : ", n2)

17.
18. # returns a new list with unique elements of the first list.

19. # To check whether a string is a palindrome or not


20. # to find the location of Python module sources.
import sys
print("\nList of directories in sys module:")
print(sys.path)
print("\nList of directories in os module:")
import os
print(os.path)

You might also like