Files Flashcards

1
Q

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

A

d) Both a and b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which exception is typically handled when using the FileWriter class in Java?

a) NullPointerException
b) IOException
c) FileNotFoundException
d) ClassCastException

A

b) IOException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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()

A

c) write()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

a) Call the close() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

b) It uses caching to optimize writing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

a) The file will contain Java and Python release dates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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()

A

c) flush()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

a) BufferedReader can handle larger files more efficiently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

b) Reads a single line of the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

c) It will return null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

b) Splits the string into an array based on the comma delimiter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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()

A

b) Integer.parseInt()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

b) It causes a NumberFormatException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

c) Use the trim() method on the string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

b) The header row may cause a NumberFormatException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What condition should be checked to skip reading the header row in a CSV file?

a) if (headerRead == false)
b) if (header == null)
c) if (headerRead == true)
d) if (headerRead != true)

A

a) if (headerRead == false)

17
Q

What will the following code print to the console?

if (year == 1981) {
  System.out.println(gameLine);
}

a) Only games released in 1981
b) All games from the file
c) All games except those from 1981
d) Games released in 1980

A

a) Only games released in 1981

18
Q

Which of the following correctly handles both the header row and spaces in the year column?

a) Use split(), skip the header, and use trim() on year
b) Parse the entire file as a string
c) Only use readLine()
d) No need to handle the header or spaces

A

a) Use split(), skip the header, and use trim() on year

19
Q

What does the trim() method do in Java?

a) Converts a string to lowercase
b) Removes leading and trailing spaces
c) Converts a string to uppercase
d) Splits a string into words

A

b) Removes leading and trailing spaces

20
Q

What is the correct way to open a file for reading and writing in Java while ensuring the file is closed properly?

a) Use FileWriter and BufferedReader, but do not close
b) Always use try/catch blocks and call close() in both reading and writing
c) Open with FileWriter, ignore closing
d) Use FileReader and FileWriter together

A

b) Always use try/catch blocks and call close() in both reading and writing