0% found this document useful (0 votes)
7 views6 pages

Python Modules and File Handling Quiz

Python Functions

Uploaded by

S Alejtebi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Python Modules and File Handling Quiz

Python Functions

Uploaded by

S Alejtebi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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:\[Link] for reading, we use
a) infile = open(“c:\[Link]”, “r”)
b) infile = open(“c:\\[Link]”, “r”)
c) infile = open(file = “c:\[Link]”, “r”)
d) infile = open(file = “c:\\[Link]”, “r”)
5. To read two characters from a file object infile, we use
a) [Link](2)
b) [Link]()
c) [Link]()
d) [Link]()
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) [Link](offset, 0)
b) [Link](offset, 1)
c) [Link](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 [Link]
f=open("C:\[Link]", "r")
text=[Link]()
print(text)
[Link]()

5. (a)
6. (b)
7. ( c) Hint:-
f = open('[Link]','r')
print([Link]) # gives output False
[Link]()
print([Link]) # gives output True

[Link] # Returns access mode with which file was opened.


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

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:\[Link]", "r")
text=[Link]()
lst=[Link]()
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])
[Link]()
-----------------------------------------------------------

14. #To count frequency of words in a file


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

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 [Link]():
n1=n1+1
elif [Link]():
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([Link])
print("\nList of directories in os module:")
import os
print([Link])

Common questions

Powered by AI

A Python function to count uppercase and lowercase letters could iterate over each character in the string, using 'isupper()' to check for uppercase and 'islower()' for lowercase characters, incrementing respective counters. Finally, the function would return or print the counts .

Double backward slashes are necessary in file paths in Python to escape the backslash character, which is an escape character in strings. Using single backslashes can cause errors or unintended results, as the system might interpret them as escape commands rather than part of the file path .

To read the first 'n' lines of a file in Python, you can open the file in read mode and iterate over the file using a loop that continues 'n' times, fetching each line successively and storing or processing it as needed .

A Python module is defined primarily as the design and implementation of specific functionality to be incorporated into a program. This allows for code reusability and organization by grouping related functionalities together .

In a top-down design process, the overall design of the program is addressed before delving into the details. This approach starts with defining the main functions or overall purpose and then breaks it down into sub-functions, ensuring a coherent architecture from the top level to the specific details .

In the 'from-import' form of importing in Python, identifiers beginning with two underscores are considered private and are not imported. This behavior helps in encapsulating data and restricting access to certain parts of the code .

To create a new list of unique elements from an existing list in Python, you can convert the list to a set to remove duplicates, and then back to a list, or alternatively, use a loop or list comprehension alongside a condition that checks existing elements before adding them to the new list .

You can find the directory of Python module sources by using the 'sys' module's 'path' attribute, which lists all directories Python searches for modules in. Importing 'os' can also assist by providing directory-related functions to navigate and list module paths .

To determine if a string is a pangram, a Python function would typically convert the string to lowercase and then use a set to collect each unique alphabetic character present in the string. By comparing the collected set of characters with the set of all alphabetic letters, the function can verify if all letters are present .

The 'read()' method retrieves all characters in a file as a single string, while 'readlines()' returns a list of lines. 'readlines()' is particularly useful when you need to process individual lines separately, whereas 'read()' is more useful for processing the entire file's content as one block .

You might also like