Sie sind auf Seite 1von 9

UNIT 5

FILES, MODULES, PACKAGES


Files and exception: text files, reading and writing files, format operator; command
line arguments, errors and exceptions, handling exceptions, modules, packages;
Illustrative programs: word count, copy file.

1 1. 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”, “w”)
d) infile = open(file = “c:\\scores.txt”, “w+”)
2 To open a file c:\scores.txt for appending data, we use ____________
a) outfile = open(“c:\\scores.txt”, “a”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “r”)

3 Which of the following statements are true?


a) When you open a file for reading, if the file does not exist, an error occurs
b) When you open a file for writing, if the file does not exist, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with the
new file
d) All of the mentioned
4 To read the entire remaining contents of the file as a string from a file object infile, we use
____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

5 The readlines() method returns ____________


a) str
b) a list of lines
c) a list of single characters
d) a list of integers

6 2. Is it possible to create a text file in python?


a. Yes
b. No
c. Machine dependent
d. All of the mentioned

7 What will be the output of the following code?


try:
   f = open("testfile", "r")
   f.write("This is the test file for exception handling!!")
except IOError:
   print ("Error: could not find a file or read data")
else:
   print ("content is written in the file successfully")

a. This is the test file for exception handling!!


b. Error: could not find a file or read data
c. content is written in the file successfully
d. IO Error
8 Which of the following attributes are not related to a file object?
a. closed
b. mode
c. name
d. rename

9 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

10 How do you delete a file?


a. del(fp)
b. fp.delete()
c. os.remove(‘file’)
d. os.delete(‘file’)

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


a. rr
b. r
c. r+
d. w+

12 How do you get the name of a file from a file object (fp)?
a. fp.name
b. fp.file(name)
c. self.__name__(fp)
d. fp.__name__()

13 What is the output of the code shown below?


re.split(r'\s+', 'Chrome is better than explorer', maxspilt=3)
a. [‘Chrome’, ‘is’, ‘better’, ‘than’, ‘explorer’]
b. [‘Chrome’, ‘is better’, ‘than explorer’]
c. (‘Chrome’, ‘is’, ‘better’, ‘than explorer’)
d. ‘Chrome is better’ ‘than explorer’

14 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

15 What is the use of tell() method in python?


a. tells you the current position within the file
b. tells you the end position within the file
c. tells you the file is opened or not
d. none of the mentioned

16 What is the output of the following print() function


print('%d %d %.f' % (11, '22', 11.22))
a. 11 22 11.22
b. TypeError
c. 11 ‘22’ 11.22
d. None of the above

17 In Python3, Whatever you enter as input, the input() function converts it into a string


a. True
b. False
18 When a module is imported, its contents:
a. are executed once (implicitly)
b. are executed as many times as they are imported
c. are ignored
d. may be executed (explicitly)
19 . Knowing that a function named fun() resides in a module named mod, choose the proper
way to import it:
a. import fun from mod
b. import fun
c. from mod import fun
d. from fun import mod

20 What will be the output of the following snippet?   


price = 49
txt = "The price is {} dollars"
print(txt.format(price))

a. The price is 49 dollars


b. Error
c. {}
d. None of the Above

21 What will be the output of the following snippet?   


try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")

a. Hello
b. Something went wrong
c. Nothing went wrong
d. Hello/ Nothing went wrong

22 What will be the output of the following snippet?   


try:
print(x)
except:
print("An exception occurred")

a. x
b. An exception occurred
c. Error
d. None of the Above
23 What will be the output of the following snippet?   
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

a. Something went wrong/ The 'try except' is finished


b. Something went wrong
c. The 'try except' is finished
d. None of the Above

24 The read(n) method returns ____________


a) To read total data
b) a list of lines
c) To read ‘n’ characters from the file
d) a list of integers

25 The read(n) method returns ____________


a) To read total data
b) a list of lines
c) To read ‘n’ characters from the file
d) a list of integers

26 The write() method returns ____________


a) str
b) a list of lines
c) a list of single characters
d) a list of integers

27 The writelines() method returns ____________


a) str
b) a list of lines
c) a list of single characters
d) a list of integers
28 Which of the following statements are true?
a. with open(“test.txt”) as f:
b. with open as f =(“test.txt”):
c. Error
d. None of the Above
29 Which of the following statements are true?
f.write(“I am studying %dst year in dept of %s at %s college” %(1, ‘S&H’, ‘KSRIET’))

a. I am studying 1st year in dept of IT at KSRIET college


b. I am studying 1st year in dept of S&H at KSRIET college
c. I am studying 1st year in dept of S&H at KSRIET
d. I am studying 1st year in dept of CSE at KSRIET college

30 How do you create a new directory?


a. os.chdir(“Dir_Name”)
b. os.mkdir(“Dir_Name”)
c. os.remove(‘Dir_Name’)
d. os.delete(‘Dir_Name’)

31 How do you change the current working directory?


a. os.chdir(“Dir_Name”)
b. os.mkdir(“Dir_Name”)
c. os.remove(‘Dir_Name’)
d. os.delete(‘Dir_Name’)

32 Which module is used in command line arguments?


a. sys
b. math
c. pygame
d. None of the Above
33 what is the command for executing largest among 3 numbers in command line arguments?
a. python.exe / largest.py / 20 30 40
b. python.exe / largest.py / 20 30
c. python.exe / largest.py
d. None of the Above

34 Find the error:


a=int(input(“Enter the Number :”))

Enter the Number : q


a. KeyError
b. ValueError
c. TypeError

d. FileNotFoundError
35 Find the error:
>>> a=[1,2,3,4,5]
>>> print(a[7])

a. KeyError
b. ValueError
c. TypeError
d. IndexError

36 Find the error:


>>> a={1:’one’,5:’five’,’a’:’apple’,4:44}
>>> a[3]

a. KeyError
b. ValueError
c. TypeError
d. IndexError
37 How do you change the file position to an offset value at the start?
a. fp.seek(offset, 0)
b. fp.seek(offset, 1)
c. fp.seek(offset, 2)
d. none of the mentioned

38 How do you change the file position to an offset value at current position ?
a. fp.seek(offset, 0)
b. fp.seek(offset, 1)
c. fp.seek(offset, 2)
d. none of the mentioned

39 Find the error:


>>> print(a

a. KeyError
b. ValueError
c. TypeError
d. EOFError
40 Find the error:
for i in range(1,6):
print(i)

a. KeyError
b. ValueError
c. IndentationError
d. EOFError
41 What is the command for package downloading in python.org?

a. pip install pygame


b. pop install pygame
c. python.exe pip install
d. python.exe pip install module name
42 which one of the following is true to import a module with renaming?
a. import math as f
b. as f import math
c. from pi import math as f
d. None of the Above

43 Find the error:


print(“hello”)
print(10/0)
print(“hi”)

a. KeyError
b. ValueError
c. ZeroDevisionError
d. EOFError

44 If you try to open a file that doesn’t exist, you get an_________________

a. KeyError
b. ValueError
c. IOError
d. EOFError
45 import sys
a = argv[1].split()
dict = {}
for i in a:
if i in dict: dict[i]=dict[i]+1
else:
dict[i] = 1
print(dict) print(len(a))

Output
C:\Python34>python .exe word.py
"python is awesome lets program in python"
Find the output for word count:
a. {'lets': 1, 'awesome': 1, 'in': 1, 'python': 2, 'program': 1, 'is': 1}
b. {'lets': 1, 'awesome': 1, 'in': 2, 'python': 2, 'program': 1, 'is': 1}
c. {'lets': 2, 'awesome': 1, 'in': 1, 'python': 2, 'program': 1, 'is': 1}
d.Error
46 To open a file c:\scores.txt for read and write, we use ____________
a) outfile = open(“c:\\scores.txt”, “r+”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)
47 To open a file c:\scores.txt for write and read, we use ____________
a) outfile = open(“c:\\scores.txt”, “w+”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “r”)
48 To open a file c:\scores.txt for write, we use ____________
a) outfile = open(“c:\\scores.txt”, “w+”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “r”)
49 If you don’t have permission to access a file. you get _____________
a. KeyError
b. ValueError
c. PermissionError
d. EOFError
50 A package is a folder containing one or more Python modules. One of the modules in a
package must be called__________________
a. main.py
b. _init_.py
c. package.py
d. init.py

Das könnte Ihnen auch gefallen