1. Define exception in Java.
An exception is an abnormal condition that occurs during program execution which disrupts the normal flow of instructions. In Java, exceptions are objects that are derived from the Throwable class.
2. State the purpose of a try–catch block.
A try–catch block is used to handle runtime errors and prevent program termination. The try block contains risky code, and the catch block handles the exception if it occurs.
3. Demonstrate exception handling using multiple catch clauses with an example.
Multiple catch blocks are used to handle different types of exceptions separately.
Example:
Example:
try
{
int a = 10/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Error");
}
catch(Exception e)
{
System.out.println("General Error");
}
4. Declare a package in a Java source file.
A package is declared using the package keyword at the beginning of the source file.
Example: package mypack;
Example: package mypack;
5. Explain the effect of the protected access modifier with respect to packages.
The protected modifier allows access within the same package and also in subclasses located in different packages.
6. Define a thread in Java.
A thread is a lightweight subprocess that enables concurrent execution of a program. It helps in performing multiple tasks simultaneously.
7. Write a simple program that uses a synchronized method.
class Test
{
synchronized void display()
{
System.out.println("Synchronized Method");
}
}
A synchronized method allows only one thread at a time to access it.
8. State one difference between FileReader and FileInputStream.
FileReader reads character streams, whereas FileInputStream reads byte streams.
9. Name the exception commonly thrown when opening a non-existent file.
The exception commonly thrown is FileNotFoundException.
10. List any two types of exceptions in Java.
Two types of exceptions are Checked exceptions and Unchecked exceptions.
11. Write a Java snippet to handle an ArithmeticException.
try
{
int a = 10/0;
}
catch(ArithmeticException e)
{
System.out.println("Cannot divide by zero");
}
12. State the purpose of the import statement.
The import statement allows a class to use classes and interfaces from other packages without writing their fully qualified names.
13. Differentiate between public and default access modifiers.
The public modifier allows access from anywhere, while the default modifier allows access only within the same package.
14. List any two advantages of using packages.
Packages help in avoiding name conflicts and provide better organization and access control of classes.
15. Differentiate between Runnable interface and Thread class.
The Runnable interface defines the run() method and supports multiple inheritance, while the Thread class is used to create and control threads directly.
16. Write a Java code snippet to create and start a thread.
class MyThread extends Thread
{
public void run()
{
System.out.println("Thread running");
}
}
MyThread t = new MyThread();
t.start();
17. State the use of File class.
The File class is used to create, delete, rename files and directories, and to obtain file information.
18. Define serialization in Java.
Serialization is the process of converting an object into a byte stream so that it can be saved to a file or transmitted over a network.
0 Comments