wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

java Files & Serialize

Total questions: 92

Worksheet time: 57mins

Name
Class
Date
1.

Which class in Java is used for file handling?

a)

BufferedReader

b)

File

c)

FileReader

d)

Scanner

2.

Which class in Java is used for file handling?

a)

BufferedReader

b)

File

c)

FileReader

d)

Scanner

3.

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

a)

void

b)

false

c)

null

d)

true

4.

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

a)

write()

b)

isWritable()

c)

canWrite()

d)

canRead()

5.

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

a)

FileAlreadyExistsException

b)

NullPointerException

c)

IOException

d)

FileNotFoundException

6.

Which method is used to delete a file in Java?

a)

erase()

b)

delete()

c)

remove()

d)

clear()

7.

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

a)

float

b)

int

c)

long

d)

double

8.

Which method returns the absolute pathname of a file?

a)

getAbsolutePath()

b)

getPath()

c)

getFullPath()

d)

getDirectory()

9.

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

a)

FileReader

b)

Scanner

c)

FileWriter

d)

BufferedReader

10.

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

a)

close()

b)

terminate()

c)

finish()

d)

end()

11.

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

a)

BufferedWriter

b)

FileReader

c)

FileWriter

d)

Scanner

12.

What method is used to check if a file exists?

a)

isExisting()

b)

exists()

c)

isPresent()

d)

isAvailable()

13.

Which method returns the name of the file?

a)

getTitle()

b)

getLabel()

c)

getFileName()

d)

getName()

14.

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

15.

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

a)

canRead()

b)

isReadable()

c)

read()

d)

readable()

16.

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

a)

-1

b)

0

c)

null

d)

undefined

17.

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

18.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

19.

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

a)

try

b)

catch

c)

finally

d)

throw

20.

try block may or may not contains catch blocks

a)

True

b)

False

21.

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

22.

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

23.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

24.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

25.

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

a)

try

b)

finally

c)

thrown

d)

catch

26.

java.lang.NullPointerException is a

a)

Error

b)

runtime exception

c)

Compile time exception

d)

None

27.

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

28.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

29.

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

30.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

31.

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

a)

3

b)

4

c)

6

d)

2

32.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

33.

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}

34.

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.

35.

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.

36.

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

37.

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

38.

FileNotFoundException

a)

Is a subclass/extends IOException

b)

Is a Compile time exception

c)

Found in java.io package

d)

All

39.

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

40.

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

41.

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

42.

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

43.
Which of these method of FileReader class is used to read characters from a file?
a)
read()
b)
scanf()
c)
get()
d)
getInteger()
44.
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
45.
Which of these class is not a member class of java.io package?
a)
String
b)
StringReader
c)
Writer
d)
File
46.
Which of these class is not related to input and output stream in terms of functioning?
a)
File
b)
Write
c)
InputStream
d)
Reader
47.
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
48.
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
49.
public char[] readPassword() blongs to whcih class
a)
Scanner Class
b)
Console class
c)
InputStreamReader class
d)
DataInputStream
50.
Which method is used to close the Stream?
a)
close();
b)
flush();
c)
null();
d)
final();
51.
 Which exception is thrown by read() method?
a)
SystemInputException
b)
IOException
c)
SystemException
d)
InterruptedException
52.
Which of these class contains the methods print() & println()?
a)
PrintStream
b)
BUfferedOutputStream
c)
System.out
d)
System
53.

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

a)

a) try

b)

b) finally

c)

c) throw

d)

d) catch

54.

2. Class at the top of exception class hierarchy?

a)

ArithmeticException

b)

Throwable

c)

Object

d)

Exception

55.

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

a)

java.util

b)

java.io

c)

java.lang

d)

java.file

56.

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

a)

catch

b)

throw

c)

throws

d)

finally

57.

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

a)

NumberFormatException

b)

ArithmeticException

c)

NullPointerException

d)

None of these

58.

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

59.

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

60.

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

61.

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

62.

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

63.

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

64.

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

65.

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.

66.

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

67.

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

68.

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

69.

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

70.

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

71.

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

72.

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.

73.
Which version of Java introduced the concept of Generics?
a)
1.3
b)
1.4
c)
1.5
d)
1.6
74.
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
75.
Which of the following is type-safe?
a)
ArrayList
b)
Arrays
c)
HashSet
d)
LinkedList
76.
To which of these collections is type-casting required during retrieval?
a)
Arrays
b)
ArrayList before Java 1.5
c)
Generic ArrayList
d)
HashSet
77.
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
78.
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
79.
When defining `class Account<T>`, what does `T` represent?
a)
Tuple
b)
Type
c)
Text
d)
Template
80.
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
81.
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
82.
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
83.
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
84.
Generics are enforced at which time?
a)
Run-time
b)
Compile-time
c)
Both run-time and compile-time
d)
Neither
85.
Given `class Test<T>`, what can `T` be replaced with?
a)
Only classes
b)
Classes and interfaces
c)
Only interfaces
d)
Primitives and classes
86.
If you have `ArrayList<?> l`, which statement is valid?
a)
l.add("String");
b)
l.add(10);
c)
l.add(null);
d)
l.clear();
87.
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
88.
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)
<?>
89.
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){}`
90.
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
91.
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>
92.
The main problem with collections prior to Java 1.5 was:
a)
Performance
b)
Memory Usage
c)
Type-safety
d)
Complexity