wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

22BCA6A-Quiz2-Java-File Handling

Total questions: 56

Worksheet time: 29mins

Name
Class
Date
1.

Which class in Java is used for file handling?

a)

BufferedReader

b)

File

c)

FileReader

d)

Scanner

2.

What does the createNewFile() method return if the file is successfully created?

a)

void

b)

false

c)

null

d)

true

3.

Which method is used to check if a file is writable?

a)

write()

b)

isWritable()

c)

canWrite()

d)

canRead()

4.

What exception does the createNewFile() method throw if an error occurs?

a)

FileAlreadyExistsException

b)

NullPointerException

c)

IOException

d)

FileNotFoundException

5.

Which method is used to delete a file in Java?

a)

erase()

b)

delete()

c)

remove()

d)

clear()

6.

What is the return type of the length() method in the File class?

a)

float

b)

int

c)

long

d)

double

7.

Which method returns the absolute pathname of a file?

a)

getAbsolutePath()

b)

getPath()

c)

getFullPath()

d)

getDirectory()

8.

Which class is used to write text to a file in Java?

a)

FileReader

b)

Scanner

c)

FileWriter

d)

BufferedReader

9.

What method should be called to close a FileWriter after writing?

a)

close()

b)

terminate()

c)

finish()

d)

end()

10.

Which class is used to read the contents of a text file in Java?

a)

BufferedWriter

b)

FileReader

c)

FileWriter

d)

Scanner

11.

What method is used to check if a file exists?

a)

isExisting()

b)

exists()

c)

isPresent()

d)

isAvailable()

12.

Which method returns the name of the file?

a)

getTitle()

b)

getLabel()

c)

getFileName()

d)

getName()

13.

What is the purpose of the mkdir() method?

a)

To rename a file

b)

To create a directory

c)

To delete a directory

d)

To create a file

14.

Which method is used to test if a file is readable?

a)

canRead()

b)

isReadable()

c)

read()

d)

readable()

15.

What is the output of the length() method if the file is empty?

a)

-1

b)

0

c)

null

d)

undefined

16.

What is the advantage of Exception Handling

a)

To avoid abnormal termination of a program

b)

To find out errors

c)

To Debug program

d)

None of these

17.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

18.

Which of the following key word is optional in Exception handling program

a)

try

b)

catch

c)

finally

d)

throw

19.

try block may or may not contains catch blocks

a)

True

b)

False

20.

What is the purpose of 'throw' keyword

a)

To Raise an exception explicityly

b)

To Raise an exception implicityly

c)

To create user defined exceptions

d)

To create custom exceptions

21.

What is an exception

a)

Error occurred during the execution of a program

b)

Bug occurred during the compile time of a program

c)

Simply an Error

d)

It is related to input values

22.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

23.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

24.

Which of these keywords is not a part of exception handling?

a)

try

b)

finally

c)

thrown

d)

catch

25.

java.lang.NullPointerException is a

a)

Error

b)

runtime exception

c)

Compile time exception

d)

None

26.

Which of these class is highest in hierarchy in java

a)

java.lang.Exception

b)

java.lang.Error

c)

java.lang.Throwable

d)

java.lang.Object

27.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

28.

Choose an exception if attempt to divide number by zero


int number = 89 / 0;

System.out.println("The answer is " + number);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundException

29.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

30.

Which line or lines should we get inside a try block to avoid errors?

a)

3

b)

4

c)

6

d)

2

31.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

32.

What will this program print?

a)

Nothing, there is an error

b)

There was a ValueError, please try again

c)

num1num2num1^{num2}

d)

num2num1num2^{num1}

33.

public class Foo

{

public static void main(String[] args)

{

try

{

return;

}

finally

{

System.out.println( "Finally" );

}

}

}

a)

Finally

b)

Compilation fails.

c)

The code runs with no output

d)

An exception is thrown at runtime.

34.

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");

a)

finished

b)

runtime error.

c)

Compilation fails

d)

Exception.

35.

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 */

}

}

a)

ABCD

b)

Compilation fails.

c)

C is printed before exiting with an error message.

d)

BC is printed before exiting with an error message

36.

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

}

}

a)

BD

b)

BDE

c)

BD

d)

DE

37.

FileNotFoundException

a)

Is a subclass/extends IOException

b)

Is a Compile time exception

c)

Found in java.io package

d)

All

38.

class Main {

public static void main(String args[]) {

try {

throw 10;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

a)

Got the Exception 10

b)

Got the Exception 0

c)

Compiler Error

d)

none

39.

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)

A

b)

B

c)

AC

d)

BC

40.

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.

a)

NumberFormatException

b)

NullPointerException

c)

ArrayIndexOutOfBoundsException

d)

InvalidStringException

41.

class IT

{

static public void main(String...args)

{

String s=null;

System.out.println("length is:"+s.length());

}

}

a)

It throws NullPointerException

b)

it throws IOException

c)

It throws StringExeption

d)

output is 0

42.
Which of these method of FileReader class is used to read characters from a file?
a)
read()
b)
scanf()
c)
get()
d)
getInteger()
43.
Which of these packages contain classes and interfaces used for input & output operations of a program?
a)
java.util
b)
java.lang
c)
java.io
d)
all of the mentioned
44.
Which of these class is not a member class of java.io package?
a)
String
b)
StringReader
c)
Writer
d)
File
45.
Which of these class is not related to input and output stream in terms of functioning?
a)
File
b)
Write
c)
InputStream
d)
Reader
46.
Which stream used to read data from a source, it may be a file, an array, peripheral device or socket?
a)
InputStream
b)
OutputStream
c)
Input/OutputStream
d)
None of the above
47.
Which is used for writing data to a file in file handling?
a)
FileInputStream
b)
FileOutputStream
c)
Both A & B
d)
None of the above
48.
public char[] readPassword() blongs to whcih class
a)
Scanner Class
b)
Console class
c)
InputStreamReader class
d)
DataInputStream
49.
Which method is used to close the Stream?
a)
close();
b)
flush();
c)
null();
d)
final();
50.
 Which exception is thrown by read() method?
a)
SystemInputException
b)
IOException
c)
SystemException
d)
InterruptedException
51.
Which of these class contains the methods print() & println()?
a)
PrintStream
b)
BUfferedOutputStream
c)
System.out
d)
System
52.

1. Which of these keywords must be used to monitor for exceptions?

a)

a) try

b)

b) finally

c)

c) throw

d)

d) catch

53.

2. Class at the top of exception class hierarchy?

a)

ArithmeticException

b)

Throwable

c)

Object

d)

Exception

54.

3. In which of the following package, Exception class exist?

a)

java.util

b)

java.io

c)

java.lang

d)

java.file

55.

4. Exception generated by try block is caught in ______ block?

a)

catch

b)

throw

c)

throws

d)

finally

56.

5. Which exception is thrown when divide by zero occurs?

a)

NumberFormatException

b)

ArithmeticException

c)

NullPointerException

d)

None of these