menu

Python Input and Output


1. Which of the following is used to write binary data to a file in Python?

write()

writelines()

append()

D. None of the above


2. Which method is used to check if a file exists in Python?

exists()

isfile()

isdir()

D. both A and B


3. Which method is used to read the entire contents of a file as a string in Python?

read()

readline()

readlines()

D. write()


4. Which function is used to read a JSON file in Python?

json.read()

json.load()

json.write()

D. json.dump()


5. How do you open a file for writing in Python?

file = open("filename.txt", "w")

file = open("filename.txt", "r")

file = open("filename.txt", "a")

D. file = open("filename.txt", "x")


6. Which function is used to change the working directory in Python?

os.chdir()

os.getcwd()

os.mkdir()

D. os.listdir()


7. Which method is used to read all the lines of a file into a list in Python?

read()

readline()

readlines()

D. write()


8. Which of the following is used to write data to a file in Python?

write()

read()

close()

D. open()


9. How do you open a file for appending in Python?

file = open("filename.txt", "a")

file = open("filename.txt", "w")

file = open("filename.txt", "r")

D. file = open("filename.txt", "x")


10. Which function is used to get the current working directory in Python?

os.getcwd()

os.listdir()

os.mkdir()

D. os.path()