Font size
Worksheets22BCA6A-Quiz2-Java-File Handling
Total questions: 56
Worksheet time: 29mins
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
