WorksheetsJava Serialization and File Handling
Total questions: 15
Worksheet time: 8mins
What is Java serialization?
It is the process of converting an object into a string of characters
It is the process of converting an object into a stream of bytes
It is the process of converting an object into a stream of integers
It is the process of converting a stream of bytes into an object
How is Java serialization achieved?
By using the java.io.Serialization class
By calling the serialize() method
By implementing the java.io.Serializable interface
By setting the serialization flag to true
What is the purpose of using Fileinputstream in Java?
To delete a file
To create a new file
To read data from a file
To write data to a file
How do you create a Fileinputstream object in Java?
FileInputStream input = new FileInputStream(file.txt);
FileInputStream input = new FileInputStream(file);
FileInputStream input = new FileInputStream("file.txt");
FileInputStream input = new FileInputStream();
What is the purpose of using Fileoutputstream in Java?
To create a new file
To write data to a file
To read data from a file
To delete a file
How do you create a Fileoutputstream object in Java?
FileOutputStream.open("filename.txt")
FileOutputStream.write("filename.txt")
FileOutputStream.create("filename.txt")
new FileOutputStream("filename.txt")
How do you create a new file using the File class in Java?
file.addNewFile()
file.createFile()
file.makeNewFile()
file.createNewFile()
What is the difference between FileWriter and Fileoutputstream in Java?
FileWriter is used to read bytes from a file, while FileOutputStream is used to write characters to a file.
FileWriter and FileOutputStream are not related to file handling in Java.
FileWriter is used to write characters to a file, while FileOutputStream is used to write bytes to a file.
FileWriter and FileOutputStream are used interchangeably in Java.
How do you read data from a file using Fileinputstream in Java?
Create a FileInputStream object and use its read() method to read the data from the file.
Open the file in a text editor and manually copy the data
Use a PrintWriter object to read the data from the file
Use a Scanner object to read the data from the file
How do you write data to a file using Fileoutputstream in Java?
Use BufferedWriter to write data to the file
Use PrintWriter to write data to the file
Create an instance of FileOutputStream and use its write() method to write data to the file.
Use FileReader to write data to the file
What is the significance of using serialization in Java?
It allows objects to be converted into a string for storage
It helps in sorting arrays in Java
It is used for creating random numbers in Java
It allows objects to be converted into a byte stream for storage or transmission
What are the advantages of using Fileinputstream and Fileoutputstream in Java?
They are not compatible with Java serialization
They provide high-level access to the file system, making file handling more complicated
They provide low-level access to the file system, allowing for efficient and flexible file handling.
They are slower and less efficient than other file handling methods
How do you handle exceptions while working with Fileinputstream and Fileoutputstream in Java?
Use try-catch blocks to catch and handle IOException
Use if-else statements to handle IOException
Throw a new custom exception instead of catching IOException
Ignore the exception and continue with the program
What are the different modes of opening a file using Fileoutputstream in Java?
Delete mode
1. Create mode 2. Append mode
Update mode
Read mode
How do you serialize and deserialize an object in Java?
Use the Serializable class to write the object to a database
Use ObjectOutputStream to write the object to a network socket
Use ObjectOutput to write the object to a text file
Implement the Serializable interface and use ObjectOutputStream to write the object to a file, then use ObjectInputStream to read the object from the file.
