Font size
Worksheetsjava Files & Serialize
Total questions: 92
Worksheet time: 57mins
Which class in Java is used for file handling?
BufferedReader
File
FileReader
Scanner
Which class in Java is used for file handling?
BufferedReader
File
FileReader
Scanner
What does the createNewFile() method return if the file is successfully created?
void
false
null
true
Which method is used to check if a file is writable?
write()
isWritable()
canWrite()
canRead()
What exception does the createNewFile() method throw if an error occurs?
FileAlreadyExistsException
NullPointerException
IOException
FileNotFoundException
Which method is used to delete a file in Java?
erase()
delete()
remove()
clear()
What is the return type of the length() method in the File class?
float
int
long
double
Which method returns the absolute pathname of a file?
getAbsolutePath()
getPath()
getFullPath()
getDirectory()
Which class is used to write text to a file in Java?
FileReader
Scanner
FileWriter
BufferedReader
What method should be called to close a FileWriter after writing?
close()
terminate()
finish()
end()
Which class is used to read the contents of a text file in Java?
BufferedWriter
FileReader
FileWriter
Scanner
What method is used to check if a file exists?
isExisting()
exists()
isPresent()
isAvailable()
Which method returns the name of the file?
getTitle()
getLabel()
getFileName()
getName()
What is the purpose of the mkdir() method?
To rename a file
To create a directory
To delete a directory
To create a file
Which method is used to test if a file is readable?
canRead()
isReadable()
read()
readable()
What is the output of the length() method if the file is empty?
-1
0
null
undefined
What is the advantage of Exception Handling
To avoid abnormal termination of a program
To find out errors
To Debug program
None of these
What is the parent class of All Exceptions in JAVA
Throw
Exception
Error
Bug
Which of the following key word is optional in Exception handling program
try
catch
finally
throw
try block may or may not contains catch blocks
True
False
What is the purpose of 'throw' keyword
To Raise an exception explicityly
To Raise an exception implicityly
To create user defined exceptions
To create custom exceptions
What is an exception
Error occurred during the execution of a program
Bug occurred during the compile time of a program
Simply an Error
It is related to input values
How to create our own exception
using 'throw' keyword
using 'throws' keyword
using 'finally' keyword
using 'throwable' keyword
Exception classes belongs to following package
import java.io.*
import java.lang.*
import java.util.*
import java.lang.Exception.*
Which of these keywords is not a part of exception handling?
try
finally
thrown
catch
java.lang.NullPointerException is a
Error
runtime exception
Compile time exception
None
Which of these class is highest in hierarchy in java
java.lang.Exception
java.lang.Error
java.lang.Throwable
java.lang.Object
Choose the CORRECT exception
String department="JTMK";
Integer no=Integer.parseInt(department);
ArithmeticException
NullPointerException
NumberFormatException
ArrayIndexOutOfBoundsException
Choose an exception if attempt to divide number by zero
int number = 89 / 0;
System.out.println("The answer is " + number);
ArithmeticException
NullPointerException
NumberFormatException
ArrayIndexOutOfBoundException
What will it print?
1
2
3
30
1
2
30
1
2
3
11
1
2
11
Which line or lines should we get inside a try block to avoid errors?
3
4
6
2
What will this print?
There was an error
1
9
0
What will this program print?
Nothing, there is an error
There was a ValueError, please try again
num1num2
num2num1
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Finally
Compilation fails.
The code runs with no output
An exception is thrown at runtime.
What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
finished
runtime error.
Compilation fails
Exception.
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}
ABCD
Compilation fails.
C is printed before exiting with an error message.
BC is printed before exiting with an error message
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}
BD
BDE
BD
DE
FileNotFoundException
Is a subclass/extends IOException
Is a Compile time exception
Found in java.io package
All
class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
Got the Exception 10
Got the Exception 0
Compiler Error
none
class exception_handling
{ public static void main(String args[])
{ try
{ int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
} catch(ArithmeticException e)
{ System.out.print("B");
}
finally
{ System.out.print("C");
}
}
}
A
B
AC
BC
class IT
{
static public void main(String...args)
{
int k=Integer.parseInt(args[0]);
System.out.println(k);
}
}
if args[0]="gec" then what type of exception is thrown by the above program.
NumberFormatException
NullPointerException
ArrayIndexOutOfBoundsException
InvalidStringException
class IT
{
static public void main(String...args)
{
String s=null;
System.out.println("length is:"+s.length());
}
}
It throws NullPointerException
it throws IOException
It throws StringExeption
output is 0
1. Which of these keywords must be used to monitor for exceptions?
a) try
b) finally
c) throw
d) catch
2. Class at the top of exception class hierarchy?
ArithmeticException
Throwable
Object
Exception
3. In which of the following package, Exception class exist?
java.util
java.io
java.lang
java.file
4. Exception generated by try block is caught in ______ block?
catch
throw
throws
finally
5. Which exception is thrown when divide by zero occurs?
NumberFormatException
ArithmeticException
NullPointerException
None of these
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.
