Important MCQ of Exception and File Handling in Python
Important MCQ of Exception and File Handling in Python is very important unit of Python . Here we will discuss some important MCQ questions related to Exception and File handling in Python Computer Science. These Questions are very important for CUET UG Computer Science Exam for class 12 students and also for various other competitive exams such as SSC , Haryana CET , UPSC CSE , Bank exams etc.
Important MCQ of Exception and File Handling in Python: Syllabus for Unit 1 of Exception Handling in Python is:
Exception and File Handling in Python Exception Handling: syntax errors, exceptions, need of exception handling, user-defined exceptions, raising exceptions, handling exceptions, catching exceptions, Try – except – else clause, Try – finally clause, recovering and continuing with finally, built-in exception classes. File Handling: text file and binary file, file types, open and close files, reading and writing text files, reading and writing binary files using pickle module, file access modes.
Here are some MCQs for Exception and File Handling in Python:
Ques1. Which block lets you test a block of code for errors?
A. try
B. except
C. finally
D. None of the above
Answer : A
Explanation: The try block lets you test a block of code for errors.
Ques 2. How many except statements can a try-except block have?
a) zero
b) one
c) more than one
d) more than zero
Answer: d
Explanation: There has to be at least one except statement.
Ques 3. Can one block of except statements handle multiple exception?
a) yes, like except TypeError, SyntaxError [,…]
b) yes, like except [TypeError, SyntaxError]
c) no
d) none of the mentioned
Answer: a
Explanation: Each type of exception can be specified directly. There is no need to put it in a list.
Ques 4. What happens when ‘1’ == 1 is executed?
a) we get a True
b) we get a False
c) an TypeError occurs
d) a ValueError occurs
Answer: b
Explanation: It simply evaluates to False and does not raise any exception.
Ques 5. ___________ is a type of errors produce in python programs.
a. Compile time error
b. Run time error
c. Both (a) and (b)
d. None of these
Answer: c. Both (a) and (b)
Ques 6 : When an error occurs during the execution of a program , an exception is said to have been __________
a. Created
b. Asserted
c. Triggered
d. Raised
Answer: d. Raised
Ques 7 . It is raised when local or global variable is not defined
a. NameError
b. ValueError
c. TypeError
d. ZeroDivisonError
Answer: a. NameError
Ques 8 : When you accept string instead of integer value, ______ is raised.
a. TypeError
b. ValueError
c. NameError
d. SyntaxError
Answer: b. ValueError
Ques 9 : Consider the given code and fill in the blank space with appropriate exception object name.
try:
X= int(input(“enter a number”))
R=X*Y
except_______:
Print(“Please check Y is not defined”)
else:
Print(“Result=”, R)
a. TypeError
b. NameError
c. IOerror
d. IndexError
Answer: b. NameError
Ques 10 : What will happen if finally and except blocks are not positioned properly
a. Program will execute , but will not display anything
b. Program will throw exception when executed
c. Program will not be complied
d. Program will execute successfully
Answer: c. Program will not be complied
Ques 11. Which exception raised when a calculation exceeds maximum limit for a numeric type?
A. StandardError
B. ArithmeticError
C. OverflowError
D. Floating PointError
Ans : C
Explanation: OverflowError : Raised when a calculation exceeds maximum limit for a numeric type
Ques 12. What will be the output of the following Python code?
int(‘65.43’)
a) ImportError
b) ValueError
c) TypeError
d) NameError
Answer: b
Explanation: The snippet of code shown above results in a value error. This is because there is an invalid literal for int() with base 10: ’65.43’.
Ques 13. Syntax errors are also known as parsing errors.
a) True
b) False
Answer: a
Explanation: Syntax errors are known as parsing errors. Syntax errors are raised when there is a deviation from the rules of a language. Hence the statement is true.
Ques 14. An exception is ____________
a) an object
b) a special function
c) a standard module
d) a module
Answer: a
Explanation: An exception is an object that is raised by a function signaling that an unexpected situation has occurred, that the function itself cannot handle.
Ques 15. How can you handle exceptions globally in Python?
a. Using the `global_exception_handler()` function
b. By modifying the global exception settings in the `sys` module
c. By using the `global` keyword
d. There is no way to handle exceptions globally
Answer: B. By modifying the global exception settings in the `sys` module
Ques 16 What is the maximum possible length of an identifier?
a. 16
b. 32
c. 64
d. None of these above
Answer: (d) None of these above
Explanation: The maximum possible length of an identifier is not defined in the python language. It can be of any number.
Ques 17. Who developed the Python language?
a. Zim Den
b. Guido van Rossum
c. Niene Stom
d Wick van Rossum
Answer: (b) Guido van Rossum
Explanation: Python language was developed by Guido van Rossum in the Netherlands.
Ques 18: Which function open file in Python?
a. open()
b. new()
c. Open()
d. None of the above
Answer: a. open()
Ques 19: Which statement will return one line from a file(File object is ‘f’)?
a. f.readline()
b. f.readlines()
c. f.read()
d. f.line()
Answer: a. f.readline()
Ques 20. Which of the following will read entire content of file(file object ‘f’)?
a. f,reads()
b. f.read()
c. f.read(all)
d. f.read( * )
Answer: b. f.read()
Ques 21. The first and second arguments of fopen() are
A. A character string containing the name of the file & the second argument is the mode
B. A character string containing the name of the user & the second argument is the mode
C. A character string containing file pointer & the second argument is the mode
D. None of the mentioned
Ans : A
Ques 22. If there is any error while opening a file, fopen will return?
A. Nothing
B. EOF
C. NULL
D. Depends on compiler
Answer: c. Null
Ques 23. What is the output of this program?
#include <stdio.h>
int main(){
FILE *fp;
char *str;
fp=fopen(“demo.txt”,”r”);// demo.txt :you are a good programmer
while(fgets(str,6,fp)!=NULL)
puts(str);
fclose(fp);
return 0;
}
A. you are a good programmer
B. e a good programmer
C. you ar
D. you are
Ans : B
Explanation: It will print only six five character
Ques 24: Which of the following is not an appropriate file access mode?
a. < a >
b. < r+ >
c. < w >
d. < wr+ >
Answer: d. < wr+ >
Ques 25: Which of the following is not true regarding ‘w’ file open mode?
a. open files in write mode
b. it replaces old content with new one in existing file
c. if file does not exists, than produces ‘ FileNotFoundError ‘
d. All are true
Answer: c. if file does not exists, than produces ‘ FileNotFoundError ‘
Ques 26: The difference between r+ and w+ is:
a. No difference as both are used to read and write content
b. Depends upon type of file
c. In r+ mode cursor is initially placed at the beginning of the file and for w+ cursor is placed at the end
d. In r+ cursor is initially placed at the end of file and for w+ cursor is placed at the beginning
Answer: a. No difference as both are used to read and write content
One thought on “Important MCQ of Exception and File Handling in Python”