Practice sheet CS – Exception handling, File Handling
1
Multiple Choice Questions (MCQs)
- What type of errors are detected when the rules of a programming language are not followed?
- a) Runtime errors
- b) Syntax errors
- c) Logical errors
- d) Semantic errors
- What does Python display when a syntax error is encountered?
- a) Error code and line number
- b) Name of the error and a small description
- c) Only the line number
- d) Only the error code
- When does the execution of a program start?
- a) Immediately after writing the code
- b) After the syntax error is rectified
- c) During the writing of the code
- d) Before any error is encountered
- What is an exception in Python?
- a) A syntax error
- b) A Python object that represents an error
- c) A type of variable
- d) A special function
- How are syntax errors handled in Python?
- a) As runtime errors
- b) As logical errors
- c) As exceptions
- d) As semantic errors
- What should a programmer do when an exception occurs?
- a) Ignore the exception
- b) Rectify the error
- c) Handle the exception to prevent abrupt termination
- d) Restart the program
- Which of the following is a built-in exception in Python?
- a) DivisionByZeroError
- b) ValueError
- c) IndexOutOfBoundError
- d) FileNotFoundError
- What does the Python interpreter do when an error is encountered in a program?
- a) Ignores the error
- b) Raises or throws an exception
- c) Terminates the program
- d) Continues execution
- What are Exception Handlers?
- a) Codes that execute when a program starts
- b) Codes that execute when an exception is raised
- c) Codes that handle logical errors
- d) Codes that handle syntax errors
- What involves interrupting the normal flow of program execution and jumping to the exception handler?
- a) Handling an exception
- b) Raising an exception
- c) Debugging an error
- d) Compiling the code
- Which statements are used to raise exceptions in Python?
- a) try and except
- b) raise and assert
- c) if and else
- d) for and while
- What is the additional code written to handle exceptions known as?
- a) Exception code
- b) Error handler
- c) Exception handler
- d) Debugging code
- Where is an exception said to be caught?
- a) In the if block
- b) In the for block
- c) In the try block
- d) In the while block
- Where is an exception handled in Python?
- a) In the catch block
- b) In the except block
- c) In the else block
- d) In the finally block
- What is a file in Python?
- a) A named location in primary storage
- b) A named location on a secondary storage media
- c) A named location in the CPU
- d) A named location in the RAM
- What does a text file contain?
- a) Only numerical information
- b) Only binary information
- c) Only textual information
- d) Only hexadecimal information
- How is each line of a text file stored?
- a) As a sequence of ASCII equivalent of the characters
- b) As a sequence of binary data
- c) As a sequence of hexadecimal values
- d) As a sequence of decimal values
- What method is used to open a file in Python?
- a) read()
- b) write()
- c) open()
- d) close()
- What does the readlines() method return?
- a) A single line from the file
- b) All lines from the file as a list of strings
- c) A specified number of bytes from the file
- d) The current position of the file object
- Which method is used to close a file in Python?
- a) close()
- b) open()
- c) write()
- d) read()
Fill in the Blanks
Words to be filled: programming, description, syntax, error, exceptions, abruptly, displayed, SyntaxError, ValueError, throws, raised, handler, assert, instructions, handler, try, except, permanently, special ,character ,handle ,position
- Syntax errors or parsing errors are detected when we have not followed the rules of the particular _______ language.
- When a syntax error is encountered, Python displays the name of the error and a small _______ about the error.
- The execution of the program will start only after the _______ error is rectified.
- An exception is a Python object that represents an _______.
- Syntax errors are also handled as _______.
- The exception needs to be handled by the programmer so that the program does not terminate _______.
- When an exception occurs during execution, the error message written in that exception is _______.
- Some commonly occurring built-in exceptions are _______ and _______.
- When an error is encountered, Python interpreter raises or _______ an exception.
- Exception Handlers are the codes designed to execute when a specific exception is _______.
- Raising an exception involves interrupting the normal flow of the program execution and jumping to the exception _______.
- Raise and _______ statements are used to raise exceptions.
- The process of exception handling involves writing additional code to give proper messages or _______ to the user.
- This additional code is known as an exception _______.
- An exception is caught in the _______ block and handled in the _______ block.
- A file is a named location on a secondary storage media where data are _______ stored.
- A text file contains only textual information consisting of alphabets, numbers, and other _______ symbols.
- Each byte of a text file represents a _______.
- The open() method returns a file object called file _______.
- The seek() method is used to position the file object at a particular _______ in a file.
Short Answer Questions
- Explain the difference between syntax errors and exceptions in Python.
- What is the purpose of the try and except blocks in Python?
- Describe the use of the open() method in file handling.
- How does the readlines() method function in Python?
- What happens when the close() method is called on a file in Python?
- What does the assert statement do in Python?
- How is a binary file different from a text file in Python?
- Why is exception handling important in programming?
- What is the role of the EOL (End of Line) character in a text file?
- How does the seek() method function in file handling?
Long Answer Questions
- Discuss the process of exception handling in Python, including the roles of raise and assert statements.
- Explain the different methods used in Python for reading from and writing to a file, providing examples for each.
- Describe how built-in exceptions like SyntaxError, ValueError, and IOError are handled in Python.
- Write a Python program that demonstrates file handling by creating a text file, writing data to it, reading the data back, and handling any exceptions that may occur.