Chapter 2 coverage:
1. What Java package is commonly used for file input and output operations?
- A) `[Link]`
- B) `[Link]`
- C) `[Link]`
- D) `[Link]`
2. Which keyword is used to handle exceptions in Java?
- A) `try`
- B) `catch`
- C) `throw`
- D) `finally`
3. Which method is used to read data from a text file in Java?
- A) `readLine()`
- B) `nextLine()`
- C) `read()`
- D) `readText()`
4. What class is commonly used for scanning input in Java?
- A) `Scanner`
- B) `FileReader`
- C) `BufferedReader`
- D) `InputStreamReader`
5. In Java, what keyword is used to indicate that a method can potentially throw an exception?
- A) `throw`
- B) `catch`
- C) `throws`
- D) `try`
6. True or False: The `FileNotFoundException` is a checked exception in Java.
7. True or False: The `print()` method in the `PrintStream` class can be used to write formatted data to a file.
8. True or False: In Java, the `FileWriter` class is used to write data to a file.
9. Which method is used to write data to a text file in Java?
- A) `write()`
- B) `print()`
- C) `append()`
- D) `save()`
10. Which class is used to represent a file in Java?
- A) `File`
- B) `FileReader`
- C) `FileWriter`
- D) `FilePath`
11. What does the `nextInt()` method of the `Scanner` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next character from the input source
- D) Reads the next double value from the input source
12. In Java, what is used to separate different fields in a text file?
- A) Commas (,)
- B) Colons (:)
- C) Semicolons (;)
- D) Pipes (|)
13. What does the `next()` method of the `Scanner` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next token from the input source
- D) Reads the next double value from the input source
14. Which method is used to close a file in Java after reading or writing operations?
- A) `closeFile()`
- B) `end()`
- C) `close()`
- D) `finish()`
15. What does the `hasNext()` method of the `Scanner` class do in Java?
- A) Checks if there is another line of text available in the input source
- B) Checks if there is another integer value available in the input source
- C) Checks if there is another token available in the input source
- D) Checks if there is another double value available in the input source
16. Which of the following statements is true about checked exceptions in Java?
- A) They must be caught or declared using a `throws` clause.
- B) They are optional to handle in the code.
- C) They are subclasses of the `RuntimeException` class.
- D) They are never thrown by the Java runtime system.
17. Which method is used to print formatted data to the console in Java?
- A) `println()`
- B) `printf()`
- C) `print()`
- D) `write()`
18. What does the `nextDouble()` method of the `Scanner` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next token from the input source
- D) Reads the next double value from the input source
19. Which of the following statements is true about the `FileNotFoundException` in Java?
- A) It is a checked exception that must be caught or declared.
- B) It is an unchecked exception.
- C) It is a subclass of the `RuntimeException` class.
- D) It is never thrown by the Java runtime system.
20. True or False: In Java, the `FileReader` class is used to write data to a file.
21. What does the `nextLine()` method of the `Scanner` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next token from the input source
- D) Reads the next double value from the input source
22. Which class is used to write data to a file in Java?
- A) `File`
- B) `FileReader`
- C) `FileWriter`
- D) `FilePath`
23. What does the `hasNextLine()` method of the `Scanner` class do in Java?
- A) Checks if there is another line of text available in the input source
- B) Checks if there is another integer value available in the input source
- C) Checks if there is another token available in the input source
- D) Checks if there is another double value available in the input source
24. Which method is used to write formatted data to a file in Java?
- A) `print()`
- B) `printf()`
- C) `write()`
- D) `println()`
25. True or False: In Java, the `Scanner` class is used to represent a file.
26. Which of the following is NOT a valid method of the `File` class in Java?
- A) `createNewFile()`
- B) `deleteFile()`
- C) `exists()`
- D) `renameTo()`
27. Which method is used to read data from a file in Java?
- A) `read()`
- B) `readLine()`
- C) `readText()`
- D) `nextLine()`
28. True or False: The `FileInputStream` class in Java is used for reading data from a file.
29. What does the `nextInt()` method of the `Scanner` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next character from the input source
- D) Reads the next double value
30. Which method is used to write data to a binary file in Java?
- A) `write()`
- B) `print()`
- C) `append()`
- D) `writeBytes()`
31. What does the `hasNextInt()` method of the `Scanner` class do in Java?
- A) Checks if there is another line of text available in the input source
- B) Checks if there is another integer value available in the input source
- C) Checks if there is another token available in the input source
- D) Checks if there is another double value available in the input source
32. True or False: The `BufferedReader` class is used for efficient reading of characters from a character-input stream.
33. Which class is used to read data from a binary file in Java?
- A) `File`
- B) `FileReader`
- C) `BufferedReader`
- D) `FileInputStream`
34. What does the `readLine()` method of the `BufferedReader` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next token from the input source
- D) Reads the next double value from the input source
35. Which method is used to read a single character from a file in Java?
- A) `readChar()`
- B) `read()`
- C) `readCharacter()`
- D) `nextChar()`
36. True or False: In Java, the `FileOutputStream` class is used to represent a file.
37. What does the `write()` method of the `FileWriter` class do in Java?
- A) Writes formatted data to the file
- B) Writes a single character to the file
- C) Writes binary data to the file
- D) Writes a line of text to the file
38. Which of the following is NOT a method of the `FileWriter` class in Java?
- A) `append()`
- B) `write()`
- C) `flush()`
- D) `close()`
39. True or False: The `DataInputStream` class in Java is used for reading primitive data types from a binary stream.
40. What does the `read()` method of the `FileInputStream` class do in Java?
- A) Reads the next line of text from the input source
- B) Reads the next integer value from the input source
- C) Reads the next byte of data from the input source
- D) Reads the next double value from the input source
Debugging Problem: File Reading Error
Problem Description:
The following Java program is intended to read data from a text file named "[Link]" and display each line of the file.
However, there are some errors in the code preventing it from functioning correctly.
```java
import [Link];
import [Link];
import [Link];
public class FileReaderDebug {
public static void main(String[] args) {
File file = new File("[Link]");
try {
Scanner scanner = new Scanner(file);
while ([Link]()) {
String line = [Link]();
[Link](line);
}
[Link]();
} catch (FileNotFoundException e) {
[Link]("File not found: " + [Link]());
}
}
}
```
Instructions for Students:
1. Identify and fix the error(s) preventing the program from reading and displaying the contents of the "[Link]" file.
2. Run the corrected program and verify that it displays each line of the file correctly.
3. Provide a brief explanation of the error(s) you found and the solution(s) you implemented to fix them.
Sample Input ([Link]):
```
This is line 1
This is line 2
This is line 3
```
Expected Output:
```
This is line 1
This is line 2
This is line 3
```
Additional Notes:
- Ensure that the "[Link]" file exists in the same directory as the Java program.
- Consider how file paths are specified in Java when locating the input file.
- Pay attention to exception handling to catch any potential errors during file processing.