Practice sheet CS – Exception handling, File Handling

1

Multiple Choice Questions (MCQs)

  1. 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
  2. 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
  3. 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
  4. 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
  5. How are syntax errors handled in Python?
    • a) As runtime errors
    • b) As logical errors
    • c) As exceptions
    • d) As semantic errors
  6. 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
  7. Which of the following is a built-in exception in Python?
    • a) DivisionByZeroError
    • b) ValueError
    • c) IndexOutOfBoundError
    • d) FileNotFoundError
  8. 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
  9. 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
  10. 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
  11. 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
  12. What is the additional code written to handle exceptions known as?
    • a) Exception code
    • b) Error handler
    • c) Exception handler
    • d) Debugging code
  13. 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
  14. 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
  15. 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
  16. What does a text file contain?
    • a) Only numerical information
    • b) Only binary information
    • c) Only textual information
    • d) Only hexadecimal information
  17. 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
  18. What method is used to open a file in Python?
    • a) read()
    • b) write()
    • c) open()
    • d) close()
  19. 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
  20. 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

  1. Syntax errors or parsing errors are detected when we have not followed the rules of the particular _______ language.
  2. When a syntax error is encountered, Python displays the name of the error and a small _______ about the error.
  3. The execution of the program will start only after the _______ error is rectified.
  4. An exception is a Python object that represents an _______.
  5. Syntax errors are also handled as _______.
  6. The exception needs to be handled by the programmer so that the program does not terminate _______.
  7. When an exception occurs during execution, the error message written in that exception is _______.
  8. Some commonly occurring built-in exceptions are _______ and _______.
  9. When an error is encountered, Python interpreter raises or _______ an exception.
  10. Exception Handlers are the codes designed to execute when a specific exception is _______.
  11. Raising an exception involves interrupting the normal flow of the program execution and jumping to the exception _______.
  12. Raise and _______ statements are used to raise exceptions.
  13. The process of exception handling involves writing additional code to give proper messages or _______ to the user.
  14. This additional code is known as an exception _______.
  15. An exception is caught in the _______ block and handled in the _______ block.
  16. A file is a named location on a secondary storage media where data are _______ stored.
  17. A text file contains only textual information consisting of alphabets, numbers, and other _______ symbols.
  18. Each byte of a text file represents a _______.
  19. The open() method returns a file object called file _______.
  20. The seek() method is used to position the file object at a particular _______ in a file.

 

Short Answer Questions

  1. Explain the difference between syntax errors and exceptions in Python.
  2. What is the purpose of the try and except blocks in Python?
  3. Describe the use of the open() method in file handling.
  4. How does the readlines() method function in Python?
  5. What happens when the close() method is called on a file in Python?
  6. What does the assert statement do in Python?
  7. How is a binary file different from a text file in Python?
  8. Why is exception handling important in programming?
  9. What is the role of the EOL (End of Line) character in a text file?
  10. How does the seek() method function in file handling?

Long Answer Questions

  1. Discuss the process of exception handling in Python, including the roles of raise and assert statements.
  2. Explain the different methods used in Python for reading from and writing to a file, providing examples for each.
  3. Describe how built-in exceptions like SyntaxError, ValueError, and IOError are handled in Python.
  4. 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.

We will be happy to hear your thoughts

Leave a reply

Captcha

ScienceShala
Logo
Enable registration in settings - general