J2EE Technical Programming Interview Questions Part 3
Question 1.) Difference between Enumeration and Iteration?
Ans: Both will help you to travel into collection bag, but Enumeration is a legacy classes, Iterator have introduced in Collection framework. In enumeration we can modify the collection objects but throw Iterator it is possible. Enumeration can be used for Collection objects as well as Iterator can be used for legacy classes. Enumeration acts as Read-only interface, where as using Iterator we can manipulate the objects also like adding and removing the objects. So Enumeration is used whenever we want to make Collection objects as Read-only.
Question 2.) What’s the difference between Transient and Volatile?
Ans: Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. These variables are not serialized. Transient instance fields are neither saved nor restored by the standard serialization. You have to handle restoring them yourself.
Volatile: Volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program. For example a Variable might be read from Cache and not update the content if it has been changed by another thread. Specifying a variable as volatile tells the JVM that any threads using that variable are not allowed to cache that value at all. Making the Variable Volatile will ensure that the compiler will get the content of the variable every time it is used and not cache its content. If not used Carefully this modifier might introduce bugs.
Question 3.) Is JVM platform dependent?
Ans: Although java is platform independent but still JVM IS platform dependent one of the feature called byte code makes JVM platform dependent. Byte code is an intermediate machine code of compiled source code. The byte code can run on all machines, however the JVM must be installed in each machine.
Question 4.) What are wrapper classes? Why do we need wrapper classes?
Ans: Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are Boolean, Byte, Character, Double, Float, Integer, Long, and Short.
We can create instances of these classes hence we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.
Question 5.) What are Checked and Unchecked Exception?
Ans: A checked exception is generally known as Compiletime Exception. Checked exception forces client programmers to deal with the possibility that the exception will be thrown hence the programmer has to handle these types of exceptions. e.g., IOException thrown by java.io.FileInputStream’s read( ) method.
Where as Unchecked exceptions are Runtime Exception. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. e.g., StringIndexOutOfBounds Exception thrown by String’s charAt () method· Checked exceptions must be caught at compile time but Runtime exceptions do not need to be.
Question 6.) What is deadlock and how it can be avoid?
Ans: Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource, which is held by the other waiting thread. In Java, this resource is usually the object lock obtained by the synchronized keyword.
Question 7.) What is the difference between interface and abstract class?
Ans: Difference between interface and abstract:
All the methods declared inside interface should be abstract, and there is no need to use the key word “abstract” for those method, but in case of abstract class at least one method should be abstract, most importantly u have to use the “abstract” key word for that method, besides that it may contain concrete methods. Abstract class must be extended abstract () methods generally contain only declaration part without definition part.
Question 8.) Why are there no global variables in Java?
Ans: Global variables are considered bad form for a variety of reasons:
Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables).
State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.
When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.
Question 9.) What are the steps in the JDBC connection?
Ans: For making a JDBC connection we need to go through the following steps:
Step 1: Register the database driver by using:
Class.forName (\” driver class for that specific database\”);
Step 2 : Create a database connection using:
Connection con = DriverManager.getConnection (url, username, password);
Step 3: Create a query using:
Statement stmt = Connection.Statement (\”select * from TABLE NAME\”);
Step 4: Exceute the query:
Question 10.) What is an enumeration?
Ans: An enumeration is an interface which containing methods for accessing the underlying data structure. It is a construct which collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection.