Important IPU 4th Sem Java Questions with Answers
Q: Differentiate between byte streams and character streams.
A: Byte streams (`InputStream`/`OutputStream`) handle raw binary data.
Character streams (`Reader`/`Writer`) handle characters using encoding like UTF-8.
Q: Explain the use of BufferedReader and BufferedWriter with examples.
A: `BufferedReader` and `BufferedWriter` provide efficient reading/writing using internal buffers.
Example: `BufferedReader br = new BufferedReader(new FileReader("file.txt"));`
Q: What is object serialization in Java? How is it implemented?
A: Serialization is converting an object to a byte stream. Implement by `implements Serializable` and
use `ObjectOutputStream`/`ObjectInputStream`.
Q: Write a program to copy contents from one file to another using streams.
A: Use FileInputStream and FileOutputStream to read and write bytes in a loop.
Q: Discuss the role of RandomAccessFile in Java I/O.
A: `RandomAccessFile` allows non-sequential read/write by seeking positions in a file.
Q: Outline the steps to connect a Java application to a database using JDBC.
A: 1. Load driver
2. Create connection
3. Create statement
4. Execute query
5. Process result
6. Close connection.
Q: Differentiate between Statement and PreparedStatement.
A: `Statement` is used for static queries. `PreparedStatement` is used for dynamic queries and
provides better performance and security.
Q: Write a JDBC program to insert a record into a database table.
A: Use `PreparedStatement` with `executeUpdate()` to insert values safely into DB.
Q: Explain the process of handling SQL exceptions in JDBC.
A: Use try-catch block and `SQLException.getMessage()` or `printStackTrace()`.
Q: What are the advantages of using JDBC over ODBC?
A: JDBC is platform-independent, pure Java, and works better with Java apps. ODBC is native and
platform-dependent.
Q: Differentiate between TCP and UDP protocols.
A: TCP is reliable and connection-oriented. UDP is faster but connectionless and unreliable.
Q: Write a simple client-server program using sockets in Java.
A: Use `ServerSocket` on server side and `Socket` on client side. Use streams for communication.
Q: Explain the role of ServerSocket and Socket classes.
A: `ServerSocket` waits for client connections. `Socket` represents the client-server connection.
Q: Discuss how data is transmitted over a network using Java sockets.
A: Through InputStream and OutputStream attached to a Socket.
Q: What are the differences between extending the Thread class and implementing the
Runnable interface?
A: `Runnable` allows multiple inheritance. `Thread` class gives more control. Both can be used to
create threads.
Q: Explain thread synchronization and its importance.
A: Synchronization ensures only one thread accesses a critical section at a time to prevent data
inconsistency.
Q: Write a program demonstrating inter-thread communication.
A: Use `wait()`, `notify()`, and `synchronized` block to let threads communicate.
Q: Discuss the lifecycle of a thread in Java.
A: States: New -> Runnable -> Running -> Blocked/Waiting -> Terminated.
Q: Explain the architecture of Java RMI.
A: RMI uses stub, skeleton, registry, and remote interfaces for object-based communication over
network.
Q: List and describe the steps to create a simple RMI application.
A: 1. Define remote interface
2. Implement interface
3. Register remote object
4. Client looks up and invokes method.
Q: Differentiate between Remote and Serializable interfaces.
A: `Remote` allows an object to be accessed remotely. `Serializable` allows it to be sent over
network as a byte stream.
Q: What are the security considerations in RMI?
A: Set a proper `SecurityManager`, use policy files, and validate remote object inputs.
Q: What is JNI and why is it used?
A: JNI allows Java to call native code (e.g., C/C++). Used for performance and system-level
operations.
Q: Describe the steps to integrate native C code with Java using JNI.
A: 1. Declare native method
2. Generate header
3. Implement in C
4. Compile and load library.
Q: Discuss the advantages and disadvantages of using JNI.
A: Pros: Speed, native access. Cons: Platform dependency, complexity, harder debugging.
Q: Differentiate between ArrayList and LinkedList.
A: `ArrayList` is faster for indexing. `LinkedList` is better for frequent insertions/deletions.
Q: Explain the characteristics of HashMap and Hashtable.
A: `HashMap` is non-synchronized and faster. `Hashtable` is synchronized and thread-safe.
Q: Write a program to demonstrate the use of Iterator and ListIterator.
A: Create a List and use `Iterator` for forward-only and `ListIterator` for bi-directional traversal.
Q: What is the difference between Set and List interfaces?
A: `Set` does not allow duplicates. `List` allows duplicates and maintains insertion order.
Q: Discuss the thread-safety of various collection classes.
A: `Vector`, `Hashtable` are thread-safe. `ArrayList`, `HashMap` are not. Use
`Collections.synchronizedList()` if needed.