Multiple choice quiz about Python - Part 3
Today's topic Quantrimang wants to challenge you is about File and exception handling in Python. Let's try the following 15 questions!
-
Question 1: What does opening file with mode 'a' mean?
-
Opening files in read-only mode.
-
Open the file in recording mode.
-
Open the recording mode file at the end of the file.
-
Open the file to read and write.
-
Question 2: What does the code below mean?
-
Open the test.txt file that is allowed to read and write to the file.
-
Open test.txt file and only read file.
-
Open the test.txt file and be allowed to overwrite the file
-
Open the file test.txt and be allowed to write to the file.
-
Question 3: Which code automatically closes the file when an exception occurs?
-
with open("test.txt", encoding = 'utf-8') as f:
-
try:
f = open("test.txt",encoding = 'utf-8')
finall
f.close()
-
There is no exact answer.
-
-
Question 4: Which assertion is correct about the code below?
f = open('test.txt', 'r', encoding = 'utf-8')
f.read()
-
This program reads the content of test.txt file.
-
If test.txt is downstream, the read () function will return the new line's start symbol 'n'.
-
You can pass an integer type parameter to read ().
-
All of the above answers are correct.
-
Question 5: What does the code below mean?
-
Print out the current working directory.
-
Print out all folders (not files) inside the given folder.
-
Print out all folders and files inside the given folder.
-
-
Question 6: Which of the following statements is correct?
-
Exception is an error that arises when executing the program (runtime error).
-
The syntax error is also an exception.
-
Exceptions are used by Python to remove a block of code from the program.
-
All of the above answers are correct.
-
Question 7: What happens if you try to open a file that doesn't exist?
-
Python automatically creates a new file under the name you are calling.
-
Nothing happens because the file does not exist.
-
-
-
Question 8: What is the result of the code below?
number = 5.0
try:
r = 10/number
print(r)
except:
print("Oops! Error occurred.")
-
-
-
2.0 Oops! Error occurred.
-
-
Question 9: What does the following code do?
try:
# đoạn code có thể gây ra lỗi
pass
except (TypeError, ZeroDivisionError):
print("Python Quiz")
-
Print 'Python Quiz' if an exception occurs (no matter what the exception is).
-
Print out 'Python Quiz' if no exception occurs.
-
Print 'Python Quiz' if one of the exceptions TypeError and ZeroDivisionError occurs.
-
Only print 'Python Quiz' when both exceptions TypeError and ZeroDivisionError happen
-
Question 10: Which exception occurs when it detects an error does not belong to any other category?
-
Question 11: Which result is the output of the code below?
def myfunc():
try:
print('Monday')
finally:
print('Tuesday')
myfunc()
-
-
-
-
-
Question 12: Which result is the output of the code below?
try:
print("throw")
except:
print("except")
finally:
print("finally")
-
-
-
-
-
Question 13: Which assertion is correct about the program below?
class A:
def __init__(self):
self.a = 1
self.__b = 1
def getY(self):
return self.__b
obj = A()
obj.a = 45
print(obj.a)
-
The program has an error because '__b' is a private property, not accessible from outside the class.
-
The program runs normally and the printed result is 1.
-
The program has an error because 'a' is a private property, not accessible from outside the class.
-
The program runs normally and the printed result is 45.
-
Question 14: Which assertion is correct about the program below?
class A:
def __init__(self):
self.x = 1
self.__y = 1
def getY(self):
return self.__y
a = A()
ax = 45
print(ax)
-
The program has an error because 'x' is a private property, not accessible from outside the class.
-
The program runs normally and the printed result is 1.
-
The program has an error because '__y' is a private property, not accessible from outside the class.
-
The program runs normally and the printed result is 45.
-
Question 15: What does opening file with mode 'wb' mean?
-
-
Open the file to read and write.
-
Open the file to record for binary format.
-
Open the file to read and write to binary.