Top 150+ Java Interview Questions and Answers

    58 Votes

89. What are the new features added in Java 1.5?

Generic classes
  • New approach to loops – Enhanced for loop
  • Static imports
  • Arrays, string builder, queues and overriding return type
Annotation
  • Output formatting – format() and printf()
  • Autoboxing of primitive types
  • Enumerated types
  • Variable length of arguments

 

90. What is Generic classes?
Generic classes is one of the best features of Java 5. Since it is backward compatible with Java 1.4, old programs will work on latest Java. Le us have a look at the features ad use of generic Classes. it's quite common that Java throws ClassCastException at runtime, while working with collection classes. To avoid this nasty surprise, Java 5 had introduced compile-time type checking.
If we are using generic classes, we don't want to typecast while retrieving the items from the collections.
91. What is the big advantage of Generics?
Big advantage: collections are now type safe. For example, you can create a Stack that holds only Strings as follows:
Stack<String> names = new Stack<String>();
You can write methods that require a type-safe collection as follows:
void printNames(Stack<String> names) {
String nextName = names.pop(); // no casting needed!
names.push("Hello"); // works just the same as before
names.push(Color.RED); // compile-time error!

 

92. What is StringBuilder class?

Compared to StringBuffer, StringBuilder is an improved version to do similar tasks. It allows quick concatenation of strings. It is not safe for use by multiple threads at the same time since the methods are not synchronized.

Why use StringBuilder?

Creating new Strings in a loop can be inefficient because of the huge number of new objects that gets created and discarded.

StringBuilder.append(<type> <value>)
StringBuilder.insert(<type> <value>)

93. What is Queue interface?

  • Queue extends the Collection interface. The main objective of the queue is to hold elements that are to be processed later. Elements in a queue are typically ordered by First-In-First-Out(FIFO) order.
  • Main Methods provided by the queue are "remove()" and "pull()" method. Both the elements return the first element in the queue. if a queue is empty and if we invoke remove() method, Java throws an exception. In case of calling pull() method on an empty queue, null is returned.

94. Describe covariant return types with an example?

It allows the user to have a method in an inherited class with same signature as in the parent’s class but differing only in return types. It makes programs more readable and solves casting problems.

class Shape
{
public Shape transform(int x, int y)
}
class Rectangle extends Shape
{
public Rectangle transform(int x, int y)
}

95. What is boxing and unboxing ?

Converting a primitive data type like int, float, etc. to it's corresponding wrapper class object(Integer,Float etc.) is called boxing or autoboxing. Example of boxing,

int i = 0;
Integer O = new Integer(i);

Unboxing is the exact opposite of boxing where wrapper class object is converted into primitive data type. Example of unboxing,

i = O.intValue();

96. How autoboxing was done before java 5 and in java 5 version?

Before 5.0

ArrayList<Integer> list = new ArrayList<Integer>();list.add(0, new Integer(42)); int total = (list.get(0)).intValue();

From 5.0 version:

ArrayList<Integer> list = new ArrayList<Integer>();list.add(0, 42);int total = list.get(0);

97. How java distinguishes between primitive types and objects?

Java distinguishes between primitive types and Objects as follows.

  • Primitive types, i.e., int, double are compact, support arithmetic operators. 
  • Object classes(Integer, Double) have more methods. For example, Integer.toString()

You need a “wrapper” to use Object methods:

Integer ii = new Integer( i ); 
ii.hashCode()

Similarly, you need to “unwrap” an Object to do primitive operation

int j = ii.intValue() * 7;

Java 1.5 makes this automatic:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, 42);
int total = list.get(0);

98. What is Enum?

An enumeration(enum) are a list of constants to represent a predefined list of values. Before Java 5, enums are declared as follows.

public final int SPRING = 0;
public final int SUMMER = 1;
public final int FALL = 2;
public final int WINTER = 3;

This is a nuisance, and is error prone as well. Here’s the new way of doing it,

enum Season { winter, spring, summer, fall }

Enums are classes which extends java.lang.Enum.Enums. This interface is final and it can’t be subclassed. It provides only one Constructor and it is protected. It implements java.lang.Comparable: compareTo() and implements Serializable.

Java Interview Questions on Exceptions

99. What is an exception?

An exception can be defined as an event which disrupts the program execution flow.

Page 9 of 14

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers