1. What are the three standard streams in Java?
The three standard streams in Java are
System.in,
System.out, and
System.err.
System.in is used for input, System.out is used for standard output, and System.err is used to display error messages.
2. Write a simple program that uses synchronized method.
A synchronized method ensures that only one thread can access it at a time to prevent data inconsistency. Example:
class Test { synchronized void display() { System.out.println("Synchronized Method"); } }
3. What exception is commonly thrown when opening a non-existent file?
When attempting to open a non-existent file, Java commonly throws
FileNotFoundException,
which is a checked exception under the IOException class.
4. What is 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 transferred over a network. It is achieved by implementing the Serializable interface.
5. Define a thread in Java.
A thread in Java is a lightweight subprocess that allows concurrent execution of two or more parts of a program for maximum utilization of CPU resources.
6. What is thread priority?
Thread priority is a value assigned to a thread to indicate the importance of its execution. It ranges from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), with 5 as the default (NORM_PRIORITY).
7. What is the use of File class?
The File class in Java is used to create, delete, rename, and obtain information about files and directories in the file system.
8. Differentiate between Runnable interface and Thread class.
Runnable is an interface that must be implemented to define a thread’s task, while Thread is a class that can be extended to create a thread. Implementing Runnable allows multiple inheritance, whereas extending Thread does not.
9. What are the two ways to create a thread in Java?
A thread can be created by extending the Thread class or by implementing the Runnable interface and passing its object to a Thread instance.
10. 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();
11. List the methods used for inter-thread communication.
The methods used for inter-thread communication are
wait(),
notify(), and
notifyAll(),
which are defined in the Object class.
12. State one difference between FileReader and FileInputStream.
FileReader is used to read character streams (text files), whereas FileInputStream is used to read byte streams (binary files).
0 Comments