wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

JAVA Files

Total questions: 127

Worksheet time: 1hrs 14mins

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

57.

What is Java serialization?

a)

It is the process of converting an object into a string of characters

b)

It is the process of converting an object into a stream of bytes

c)

It is the process of converting an object into a stream of integers

d)

It is the process of converting a stream of bytes into an object

58.

How is Java serialization achieved?

a)

By using the java.io.Serialization class

b)

By calling the serialize() method

c)

By implementing the java.io.Serializable interface

d)

By setting the serialization flag to true

59.

What is the purpose of using Fileinputstream in Java?

a)

To delete a file

b)

To create a new file

c)

To read data from a file

d)

To write data to a file

60.

How do you create a Fileinputstream object in Java?

a)

FileInputStream input = new FileInputStream(file.txt);

b)

FileInputStream input = new FileInputStream(file);

c)

FileInputStream input = new FileInputStream("file.txt");

d)

FileInputStream input = new FileInputStream();

61.

What is the purpose of using Fileoutputstream in Java?

a)

To create a new file

b)

To write data to a file

c)

To read data from a file

d)

To delete a file

62.

How do you create a Fileoutputstream object in Java?

a)

FileOutputStream.open("filename.txt")

b)

FileOutputStream.write("filename.txt")

c)

FileOutputStream.create("filename.txt")

d)

new FileOutputStream("filename.txt")

63.

How do you create a new file using the File class in Java?

a)

file.addNewFile()

b)

file.createFile()

c)

file.makeNewFile()

d)

file.createNewFile()

64.

What is the difference between FileWriter and Fileoutputstream in Java?

a)

FileWriter is used to read bytes from a file, while FileOutputStream is used to write characters to a file.

b)

FileWriter and FileOutputStream are not related to file handling in Java.

c)

FileWriter is used to write characters to a file, while FileOutputStream is used to write bytes to a file.

d)

FileWriter and FileOutputStream are used interchangeably in Java.

65.

How do you read data from a file using Fileinputstream in Java?

a)

Create a FileInputStream object and use its read() method to read the data from the file.

b)

Open the file in a text editor and manually copy the data

c)

Use a PrintWriter object to read the data from the file

d)

Use a Scanner object to read the data from the file

66.

How do you write data to a file using Fileoutputstream in Java?

a)

Use BufferedWriter to write data to the file

b)

Use PrintWriter to write data to the file

c)

Create an instance of FileOutputStream and use its write() method to write data to the file.

d)

Use FileReader to write data to the file

67.

What is the significance of using serialization in Java?

a)

It allows objects to be converted into a string for storage

b)

It helps in sorting arrays in Java

c)

It is used for creating random numbers in Java

d)

It allows objects to be converted into a byte stream for storage or transmission

68.

What are the advantages of using Fileinputstream and Fileoutputstream in Java?

a)

They are not compatible with Java serialization

b)

They provide high-level access to the file system, making file handling more complicated

c)

They provide low-level access to the file system, allowing for efficient and flexible file handling.

d)

They are slower and less efficient than other file handling methods

69.

How do you handle exceptions while working with Fileinputstream and Fileoutputstream in Java?

a)

Use try-catch blocks to catch and handle IOException

b)

Use if-else statements to handle IOException

c)

Throw a new custom exception instead of catching IOException

d)

Ignore the exception and continue with the program

70.

What are the different modes of opening a file using Fileoutputstream in Java?

a)

Delete mode

b)

1. Create mode 2. Append mode

c)

Update mode

d)

Read mode

71.

How do you serialize and deserialize an object in Java?

a)

Use the Serializable class to write the object to a database

b)

Use ObjectOutputStream to write the object to a network socket

c)

Use ObjectOutput to write the object to a text file

d)

Implement the Serializable interface and use ObjectOutputStream to write the object to a file, then use ObjectInputStream to read the object from the file.

72.
Which of these method of FileReader class is used to read characters from a file?
a)
read()
b)
scanf()
c)
get()
d)
getInteger()
73.
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
74.
Which of these class is not a member class of java.io package?
a)
String
b)
StringReader
c)
Writer
d)
File
75.
Which of these class is not related to input and output stream in terms of functioning?
a)
File
b)
Write
c)
InputStream
d)
Reader
76.
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
77.
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
78.
public char[] readPassword() blongs to whcih class
a)
Scanner Class
b)
Console class
c)
InputStreamReader class
d)
DataInputStream
79.
Which method is used to close the Stream?
a)
close();
b)
flush();
c)
null();
d)
final();
80.
 Which exception is thrown by read() method?
a)
SystemInputException
b)
IOException
c)
SystemException
d)
InterruptedException
81.
Which of these class contains the methods print() & println()?
a)
PrintStream
b)
BUfferedOutputStream
c)
System.out
d)
System
82.

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

83.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

84.

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

a)

try

b)

catch

c)

finally

d)

throw

85.

try block may or may not contains catch blocks

a)

True

b)

False

86.

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

87.

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

88.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

89.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

90.

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

a)

try

b)

finally

c)

thrown

d)

catch

91.

java.lang.NullPointerException is a

a)

Error

b)

runtime exception

c)

Compile time exception

d)

None

92.

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

93.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

94.

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

95.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

96.

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

a)

3

b)

4

c)

6

d)

2

97.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

98.

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}

99.

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.

100.

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.

101.

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

102.

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

103.

FileNotFoundException

a)

Is a subclass/extends IOException

b)

Is a Compile time exception

c)

Found in java.io package

d)

All

104.

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

105.

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

106.

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

107.

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

108.
Which version of Java introduced the concept of Generics?
a)
1.3
b)
1.4
c)
1.5
d)
1.6
109.
The primary purpose of Generics in Java is to:
a)
Improve performance
b)
Introduce new data structures
c)
Provide Type-Safety
d)
Support multi-threading
110.
Which of the following is type-safe?
a)
ArrayList
b)
Arrays
c)
HashSet
d)
LinkedList
111.
To which of these collections is type-casting required during retrieval?
a)
Arrays
b)
ArrayList before Java 1.5
c)
Generic ArrayList
d)
HashSet
112.
Why were Generics introduced in Java 1.5?
a)
To provide type-safety to collections
b)
To improve collection performance
c)
To support new data structures
d)
To phase out arrays
113.
In a generic `ArrayList<String>`, what type of objects can you add?
a)
Any Object type
b)
Only Strings
c)
Integer and Strings
d)
None
114.
When defining `class Account<T>`, what does `T` represent?
a)
Tuple
b)
Type
c)
Text
d)
Template
115.
Bounded types in Generics are used to:
a)
Increase collection size
b)
Limit the type of objects you can add to a collection
c)
Specify the exact size of a collection
d)
Improve collection performance
116.
The syntax `ArrayList<? extends Number>` specifies:
a)
ArrayList that can hold Strings
b)
ArrayList that can hold Numbers and subclasses of Number
c)
ArrayList that holds any object type
d)
ArrayList that holds exclusively integers
117.
What does the wildcard character (`?`) in Generics denote?
a)
A specific type
b)
Multiple types
c)
A type that extends another
d)
An unknown type
118.
In `ArrayList<? super Integer>`, you can add:
a)
Any object type
b)
Only Integers
c)
Integers and subclasses of Integer
d)
Integers and superclasses of Integer
119.
Generics are enforced at which time?
a)
Run-time
b)
Compile-time
c)
Both run-time and compile-time
d)
Neither
120.
Given `class Test<T>`, what can `T` be replaced with?
a)
Only classes
b)
Classes and interfaces
c)
Only interfaces
d)
Primitives and classes
121.
If you have `ArrayList<?> l`, which statement is valid?
a)
l.add("String");
b)
l.add(10);
c)
l.add(null);
d)
l.clear();
122.
What is the result of compiling `class Test<T extends Runnable&Number>`?
a)
Successful compile
b)
Compile-time error due to wrong order
c)
Compile-time error due to multiple bounds
d)
Compile-time error due to invalid bounds
123.
If we want to add objects of either a particular type or its super classes in an ArrayList, which wildcard should we use?
a)
extends
b)
super
c)
?
d)
<?>
124.
Which of the following methods is validly declared?
a)
`public T void methodOne(T t){}`
b)
`public <T> void methodOne1(T t){}`
c)
`public T methodOne(T t){}`
d)
`public methodOne(T t){}`
125.
The erasure in Generics refers to:
a)
A runtime error
b)
Removing all generic type information
c)
A compile-time error
d)
A new kind of collection
126.
When defining `ArrayList<? super String>`, which of the following types can it represent?
a)
ArrayList<Object>
b)
ArrayList<StringBuilder>
c)
ArrayList<String>
d)
ArrayList<CharSequence>
127.
The main problem with collections prior to Java 1.5 was:
a)
Performance
b)
Memory Usage
c)
Type-safety
d)
Complexity