Home Login

Basic Java Interview Questions

Categories: EDUCATION

Q.1. What is singleton class in Java and how can we make a class singleton?

Ans. Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

 

Q.2. What is the difference between Array list and vector in Java?

Ans. 

ArrayList:

(i) Array List is fast as it’s non-synchronized. 

(ii) Array List is not synchronized.          

(iii) If an element is inserted into the Array List, it increases its Array size by 50%.

(iv) Array List does not define the increment size.

(v) Array List can only use Iterator for traversing an Array List.

 

Vector:

(i) Vector is synchronized.

(ii) Vector is slow as it is thread safe.

(iii) Vector defaults to doubling size of its array.

(iv) Vector defines the increment size. 

(v) Vector can use both Enumeration and Iterator for traversing.

 

Q.3. What is the difference between equals() and == in Java?

Ans. Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

 

 

Q.4. When can you use the super keyword?

Ans. In Java, the super keyword is a reference variable that refers to an immediate parent class object.

When you create a subclass instance, you’re also creating an instance of the parent class, which is referenced to by the super reference variable.

 

The uses of the Java super Keyword are- 

(i) To refer to an immediate parent class instance variable, use super.

(ii) The keyword super can be used to call the method of an immediate parent class.

(iii) Super() can be used to call the constructor of the immediate parent class.

 

Q.5. What makes a HashSet different from a TreeSet?

Ans.

HashSet:

(i) It is implemented through a hash table.

(ii) It permits the null object.

(iii) It is faster than TreeSet especially for search, insert, and delete operations.        

(iv) It does not maintain elements in an ordered way.

(v) It uses equals() method to compare two objects.          

            

TreeSet:

(i) TreeSet implements SortedSet Interface that uses trees for storing data.

(ii) It does not allow the null object.

(iii) It is slower than HashSet for these operations.

(iv) The elements are maintained in a sorted order.

(v) It uses compareTo() method for comparing two objects.

(vi) It does not permit a heterogenous object.          It permits a heterogenous object.

 

 

Q.6. What are the differences between HashMap and HashTable in Java?

Ans.

HashMap:

(i) It is non synchronized. It cannot be shared between many threads without proper synchronization code.

(ii) It permits one null key and multiple null values.

(iii) is a new class introduced in JDK 1.2.

(iv) It is faster.

(v) It is traversed through the iterator.

(vi)  It uses fail fast iterator.

(vii) It inherits AbstractMap class.

 

Hashtable:

(i) It is synchronized. It is thread-safe and can be shared with many threads.

(ii) It does not permit any null key or value.

(iii) It was present in earlier versions of java as well.

(iv) It is slower.

(v) It is traversed through Enumerator and Iterator.

(vi) It uses an enumerator which is not fail fast.

(vii) It inherits Dictionary class.

Top articles
Signs of Schizophrenia: Is Someone You Know at Risk? Published at:- Top 5 Benefits of Earning Your Degree from an Online University Published at:- How to Stay Motivated and Succeed in Your Online University Studies Published at:- Future of Online Learning: Community College Edition Published at:- Climate Crisis Chronicles: Man-Made Pollution and the Heatwaves Published at:- Basic Java Interview Questions Published at:- NEET (Zoology MCQs) Published at:- Awards, Honours and Persons in News (MCQs) Published at:- Summits and Conferences (MCQs) Published at:- SPORTS (MCQs) Published at:-
|| Others || Bike || All world || Covid 19 || Bikes || Politicians || Bank Recruitment || IPL 2022 || Jio || Mobile recharge || Election 2022 || Politics news || Cricket 2022 || Budget Special || NITI Aayog || CAR || DAILY NEWS || MOBILE / TAB / WATCH / TECHNOLOGY || APIs || ABOUT LUCKNOW || SPORTS || FILM INDUSTRY || FOREIGN COUNTRIES || SCIENCE NEWS || Technology || NEW SCHEMES || CELEBRITIES || ENTERTAINMENT || NEIGHBOUR COUNTRIES || NEWS OF INDIA || FOREIGN OR INDIAN NEWS || US NEWS || UK NEWS || Historical news || Software Engineering || GAMING NEWS || Software || Cricket || Cricket || Other || Education ||
closeConn($conn); ?>