Top 150+ Java Interview Questions and Answers

    58 Votes

66. What does rejex package consists of?

The java.util.regex package primarily consists of three classes: Pattern, Matcher, and PatternSyntaxException. Let us see what each class does. To define a pattern for regex engine, Pattern class is used. An object of Pattern class is a compiled version of a regular expression. No constructors are provided by the Pattern class.
In order to create a pattern, a static method(Compile(expression)) of the pattern class is invoked, which accept a regular expression as the argument. The subsequent object returned by the pattern class is used for identifying a match.
Pattern s = Pattern.compile(".p">);// Checking single character
Matcher m = p.matcher("apple");

Job of the matcher object is to perform the match operation against an input, after interpreting the pattern. Like the Pattern class, Matcher objects are created by invoking the "matcher" method of a Pattern object. A PatternSyntaxException is used to throw an exception, if there is a syntax error in a regular expression pattern.

67. What is StringBuffer class is used for?
  • StringBuffer is used to create and manipulate strings, which undergoes frequent modifications.
  • If we define a StringBuffer without specifying the data it can hold, it will alocate 16 bytes. Advantage of the striger buffer is that, if more data is added to buffer than the alloted size, it's capacity will grow dynamically to handle additional characters. The operators + and += can't be used for string concatenation.
  • It has fewer utility methods (e.g. substring, trim, ....)
68. What is the difference between StringBuffer and StringBuilder class?

The StringBuffer class is not threadsafe whereas StringBuilder class is threadsafe. It means that the methods of the StringBuilder class are synchronized.

69. What is matcher in regex package?

A matcher is created from pattern object, by calling it's matcher method. A matcher is used to perform three different match operations, such as:

  • Matching a input sequence against a given pattern. 
  • Using "lookingAt()" method to match a sequence, from the start till it find a matching pattern. 
  • Using find() method to find all the matching sequences that matches the pattern, just like iterator

70. How to add comments in Java File ?

Comments can be inserted in 3 ways as shown below
// Comment Type1
/* Multi Line Comments
* Comment Line 2 */
/* Documentation Comment
* Comment Line 2 */ 
This comment will be added in the automatic document generation

Java Interview Questions on Collections

71. What are the limitations of array?

Arrays do not grow while applications demand. It has inadequate support for inserting, deleting, sorting, and searching operations.

72. What is collection framework?

Collections Framework provides a generic set of methods and interfaces inorder to handle collections. It can be summarized as

  • Interfaces that characterize common collection types 
  • Abstract Classes which can be used as a starting point for custom collections and which are extended by the JDK implementation classes. 
  • Classes which provide implementations of the Interfaces.
  • Algorithms that provide behaviors commonly required when using collections i.e. search, sort, iterate, etc.

To access each item of a collection, the framework provides Iterator interface. Advantage of the iterator interface is that it gives a well defined standard way to access the elements in a collection, one at a time.

73. What is set interface about?

The Set interface is used to represent a collection which does not contain duplicate elements. The Set interface extends the Collection interface. It stipulates that an instance of Set contains no duplicate elements.

The classes that implement Set must ensure that no duplicate elements can be added to the set. It means that the two elements, Obj1 and Obj2 can't be in a set if Obj1.equals(Obj2) is true.

74. What is HashSet, LinkedHashSet and TreeSet?

HashSet, TreeSet, and LinkedHashSet are the three general purpose implementations of the set interface provided by the Java platform.

  • HashSet: This is an unsorted, unordered Set. This may be chosen when order of the elements are not important.
  • LinkedHashSet: This is ordered. The elements are linked to one another(Double-Linked). This will maintain the list in the order in which they were inserted.
  • TreeSet: This is a sorted set. The elements in this will be in ascending order by default. You can also define a custom order using a Comparator passed as a parameter to the constructor.

75. Which one to choose and when among HashSet, LinkedHashSet and TreeSet?

  • HashSet is a good choice for representing sets if you don't care about the order of elements in a set. But if ordering is important, then LinkedHashSet or TreeSet are better choices. However, LinkedHashSet or TreeSet come with an additional speed and space cost.
  • Iteration over a LinkedHashSet is generally faster than iteration over a HashSet. 
  • Tree-based data structures get slower as the number of elements get larger. 
  • HashSet and LinkedHashSet do not represent their elements in sorted order.
  • TreeSet provides additional features such as finding the first and last elements in a set.
Page 7 of 14
Planning to do an MBA?
A quick all-in-one MBA entry manual for MBA Aspirants. Book covers
  • Possible MBA Tests & Exam Preparation
  • Tips to choose right MBA Program
  • Essay, Resume & Letter of Recommendation
  • MBA Interview Preparation
  • MBA Financial Planning
Price - 6.99$

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