Files and Exception Handling - streams and files, multi-file programs, exception and event handling - Question Bank

1. Which C++ stream manipulator is used to move the file's write/read position to a specific location?
A) endl
B) setw
C) seekp/seekg
D) flush
2. What does the 'x' mode signify when opening a file in Python?
A) Read mode
B) Write mode (overwrites existing content)
C) Append mode
D) Exclusive creation mode (fails if the file already exists)
3. In the context of multi-file programs, what is a common way to link or compile different source files together?
A) Using a single compiler command with all source files listed.
B) Creating separate executable files for each source file.
C) Manually copying code between files.
D) Using a linker tool after compilation.
4. Which Java exception is a general superclass for all errors and exceptions that are not runtime exceptions?
A) Throwable
B) Error
C) Exception
D) RuntimeException
5. What is the main purpose of the `raise` statement in Python?
A) To create a new thread.
B) To explicitly trigger an exception.
C) To increase the priority of a process.
D) To close a file handle.
6. In C++, what does the `seekg()` function typically do?
A) Writes data to the file.
B) Sets the position of the input stream pointer.
C) Reads data from the file.
D) Closes the file stream.
7. Which Java class is used for reading primitive data types and strings from binary files?
A) FileReader
B) BufferedReader
C) DataInputStream
D) ObjectInputStream
8. What is the difference between `os.path.exists()` and `os.path.isfile()` in Python?
A) `exists()` checks for both files and directories; `isfile()` checks only for files.
B) `isfile()` checks for both files and directories; `exists()` checks only for files.
C) They perform the exact same function.
D) `exists()` checks for directories only; `isfile()` checks for files only.
9. In Java, what is the purpose of the `Serializable` interface?
A) It allows objects to be converted into a byte stream for storage or transmission.
B) It defines methods for network communication.
C) It enables event handling for GUI components.
D) It marks a class as thread-safe.
10. What is the role of the `catch` block in Java?
A) To define the code that might throw an exception.
B) To execute code that should always run.
C) To handle a specific type of exception that is thrown in the `try` block.
D) To re-throw an exception.
11. Which Python exception is raised when you try to write to a file that has been opened in read-only mode?
A) PermissionError
B) IOError
C) UnsupportedOperation
D) ValueError
12. What is the standard way to handle potential errors when performing file operations in C++?
A) Using `goto` statements to jump over errors.
B) Checking the state of the stream object after each operation (e.g., `fail()`, `bad()`).
C) Ignoring all errors and hoping for the best.
D) Relying solely on the operating system to report errors.
13. Which Java exception is a superclass for most other I/O related exceptions?
A) Exception
B) Runtime Exception
C) IOException
D) SystemException
14. In Python, what does the 'rb' mode signify when opening a file?
A) Read mode, binary
B) Read mode, byte-oriented
C) Read mode, buffered
D) Read mode, raw bytes
15. What is the purpose of the `try-with-resources` statement in Java (introduced in Java 7)?
A) To handle checked exceptions more easily.
B) To ensure that resources like streams are automatically closed after use.
C) To define custom exception types.
D) To allow multiple threads to access a file concurrently.
16. Which of the following is a common scenario where multi-file programming is essential?
A) Writing a simple 'Hello, World!' program.
B) Developing a large application with distinct modules like UI, data access, and business logic.
C) Creating a single, self-contained script.
D) Performing basic arithmetic operations.
17. What is the primary advantage of using `BufferedReader` over `FileReader` for reading large text files in Java?
A) `BufferedReader` is simpler to use.
B) `BufferedReader` reads data in larger chunks, improving performance by reducing the number of system calls.
C) `FileReader` automatically handles character encoding.
D) `BufferedReader` can only read binary data.
18. Which Python function is used to check if a file exists?
A) os.path.exists()
B) file.exists()
C) os.isfile()
D) path.check()
19. In C++, what is the purpose of the `flush()` method on a file stream?
A) To close the file stream.
B) To ensure that any buffered output is written immediately to the file.
C) To read data from the file.
D) To set the file pointer to the beginning of the file.
20. What is a 'daemon thread' in Java?
A) A thread that performs background tasks and does not prevent the JVM from exiting.
B) A thread that is responsible for handling user interface events.
C) A thread that is automatically created by the JVM.
D) A thread that is used for inter-thread communication.
21. Which Java class is used for writing character data to files, often with buffering for efficiency?
A) FileOutputStream
B) FileWriter
C) PrintWriter
D) ObjectOutputStream
22. What is the primary role of the `finally` clause in Python's `try...except...finally` structure?
A) To catch specific exceptions.
B) To execute code that should run regardless of whether an exception occurred.
C) To define the code that might raise an exception.
D) To re-raise a caught exception.
23. In C++, what happens if you try to open a file that does not exist using `ofstream` in its default mode?
A) The program will terminate with an error.
B) An exception of type `std::ios_base::failure` will be thrown (if exceptions are enabled).
C) The `ofstream` object will be in a bad state, and subsequent operations will fail.
D) The file will be created automatically.
24. What does the `EOFException` in Java typically indicate?
A) An error during file creation.
B) An attempt to read past the end of a file during deserialization.
C) A problem with network connectivity.
D) An invalid input format.
25. Which of the following is NOT a common mode for opening files in Python?
A) 'r'
B) 'w'
C) 'x'
D) 's'
26. What is the standard extension for Java archive files, often used to package multiple class files and resources for distribution?
A) .jar
B) .war
C) .ear
D) .zip
27. What is the difference between a checked and an unchecked exception in Java?
A) Checked exceptions must be declared or caught; unchecked exceptions do not need to be.
B) Unchecked exceptions are always fatal; checked exceptions are recoverable.
C) Checked exceptions occur at compile time; unchecked exceptions occur at runtime.
D) There is no functional difference; they are just different names.
28. In Python, what is the purpose of the `seek()` method when working with files?
A) To read the entire file content.
B) To write data to the file.
C) To change the current position within the file.
D) To close the file.
29. Which C++ stream object is used for both reading from and writing to files?
A) ifstream
B) ofstream
C) fstream
D) iostream
30. What does the `throws` keyword in a Java method signature indicate?
A) That the method will definitely throw an exception.
B) That the method might throw one or more specified checked exceptions.
C) That the method is guaranteed not to throw any exceptions.
D) That the method catches a specific exception.
31. In Java, which method is typically used to read a line of text from a `BufferedReader`?
A) read()
B) readLine()
C) next()
D) getLine()
32. What is the primary function of an 'event handler'?
A) To generate random numbers
B) To define the program's main loop
C) To respond to specific events that occur during program execution
D) To manage memory allocation
33. Which Python exception is raised when a file or directory is requested but does not exist?
A) TypeError
B) ValueError
C) FileNotFoundError
D) IOError
34. What is a 'stream' in the context of file handling?
A) A fixed-size data structure
B) A sequence of data that can be read from or written to
C) A type of exception
D) A compiled program module
35. In C++, what is the role of the `std::ios::binary` flag when opening a file?
A) It enables text mode translation.
B) It specifies that the file should be treated as a sequence of raw bytes, without character translations.
C) It opens the file for both reading and writing.
D) It appends data to the end of the file.
36. Which exception class in Java represents an error related to file operations, such as a file not being found?
A) NullPointerException
B) ArrayIndexOutOfBoundsException
C) FileNotFoundException
D) IOException
37. What is the primary benefit of using multi-file programs?
A) To increase the complexity of the code
B) To improve code organization, modularity, and reusability
C) To reduce the need for comments
D) To make debugging impossible
38. In Python, using the `with open(...) as f:` construct, when is the file automatically closed?
A) Immediately after the `open()` call.
B) When the program terminates.
C) Automatically when the `with` block is exited, even if errors occur.
D) Only when `f.close()` is explicitly called within the block.
39. Which Java class is fundamental for handling file input and output operations?
A) System
B) File
C) Scanner
D) BufferedReader
40. What is an 'event' in the context of programming?
A) A type of loop structure
B) A signal that something has happened, requiring a response
C) A variable declaration
D) A function call
41. What does the 'a' mode signify when opening a file in Python?
A) Read mode
B) Write mode (overwrites existing content)
C) Append mode (adds content to the end)
D) Exclusive creation mode
42. Which of the following C++ stream objects is used for reading from files?
A) ofstream
B) fstream
C) ifstream
D) iostream
43. What is the purpose of the `finally` block in Java's exception handling?
A) It contains the code that might throw an exception.
B) It contains the code to handle the exception.
C) It contains code that will always execute, regardless of whether an exception was thrown or caught.
D) It is used to define custom exception types.
44. In C++, what is the standard library that provides file stream classes like `ifstream` and `ofstream`?
A) <iostream>
B) <fstream>
C) <sstream>
D) <cstdio>
45. Which keyword is used to explicitly throw an exception in Java?
A) throw
B) throws
C) catch
D) finally
46. What is the purpose of a `try-catch` block in exception handling?
A) To define a new exception type
B) To execute code regardless of whether an exception occurs
C) To anticipate and handle potential runtime errors
D) To create a loop for error checking
47. What does the 'w' mode signify when opening a file in Python?
A) Read mode
B) Write mode (overwrites existing content)
C) Append mode
D) Binary mode
48. In Python, what is the primary function used to open a file?
A) open()
B) read()
C) write()
D) close()
49. Which of the following is a common stream type used for reading data from a file in Java?
A) OutputStream
B) InputStream
C) PrintStream
D) BufferedWriter