Files Flashcards
What does the FileWriter constructor do when provided with a filename as a parameter in Java?
a) Opens the file if it exists
b) Creates the file if it does not exist
c) Opens and creates the file if it does not exist
d) Both a and b
d) Both a and b
Which exception is typically handled when using the FileWriter class in Java?
a) NullPointerException
b) IOException
c) FileNotFoundException
d) ClassCastException
b) IOException
What method is used to write a line of text into a file in Java using FileWriter?
a) writeLine()
b) append()
c) write()
d) println()
c) write()
What must be done after writing data to a file using FileWriter to ensure all data is saved?
a) Call the close() method
b) Call the save() method
c) Call the commit() method
d) Call the end() method
a) Call the close() method
Why do we use BufferedWriter over FileWriter when writing large amounts of data?
a) It offers faster disk writes
b) It uses caching to optimize writing
c) It automatically compresses the file
d) It provides automatic file locking
b) It uses caching to optimize writing
What is the output if the following code is run?
BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt")); writer.write("Java, Oracle, 1995\r\n"); writer.write("Python, PSF, 1991\r\n"); writer.close();
a) The file will contain Java and Python release dates
b) The file will be empty
c) The file will contain only Python release dates
d) An IOException will be thrown
a) The file will contain Java and Python release dates
Which method should be called to force data in the file buffer to be written to the disk before closing the file?
a) close()
b) write()
c) flush()
d) persist()
c) flush()
When reading files in Java, why is it preferable to use BufferedReader over FileReader?
a) BufferedReader can handle larger files more efficiently
b) BufferedReader reads files in binary mode
c) BufferedReader automatically compresses file contents
d) BufferedReader reads from network streams only
a) BufferedReader can handle larger files more efficiently
What is the purpose of the readLine() method in BufferedReader?
a) Reads the entire file at once
b) Reads a single line of the file
c) Reads the first 10 lines of the file
d) Reads the file into a list of strings
b) Reads a single line of the file
What will happen if BufferedReader.readLine() reaches the end of the file?
a) It will return an empty string
b) It will throw an IOException
c) It will return null
d) It will stop reading silently
c) It will return null
In the following code, what does the split() method do?
String[] gameColumns = gameLine.split(",");
a) Splits the string into words
b) Splits the string into an array based on the comma delimiter
c) Removes commas from the string
d) Converts the string to uppercase
b) Splits the string into an array based on the comma delimiter
How do we convert a string like ‘1981’ into an integer in Java?
a) Integer.convert()
b) Integer.parseInt()
c) String.toInt()
d) Integer.parse()
b) Integer.parseInt()
What problem is caused when trying to parse the string “ year” using Integer.parseInt()?
a) It returns 0
b) It causes a NumberFormatException
c) It trims the string automatically
d) It works fine without errors
b) It causes a NumberFormatException
How can we fix leading spaces before numbers in strings when parsing to integers?
a) Use Integer.trim()
b) Use Integer.convert()
c) Use the trim() method on the string
d) Use the split() method
c) Use the trim() method on the string
What happens if you do not handle the header row properly when reading a CSV file?
a) The header row will be ignored automatically
b) The header row may cause a NumberFormatException
c) The header row will be skipped without any issues
d) The program will output the header as data
b) The header row may cause a NumberFormatException