Top 10 Interview Questions on Core Java for 2-3 Years of Experience

Are you looking for the top 10 interview questions on Core Java for those with 2-3 years of experience? If so, you’ve come to the right place! In this article, we’ll provide you with a comprehensive list of questions that are commonly asked in interviews for Core Java positions.

We’ll also provide you with some tips and advice on how to answer each question. With this information, you’ll be well-prepared to ace your interview and land the job of your dreams. So, let’s get started!

Top 10 Interview Questions on Core Java for 2-3 Years of Experience

1. What is the difference between an Interface and an Abstract Class?

  1. An interface is a collection of abstract methods, while an abstract class is a class that contains both abstract and concrete methods.
  2. An interface can only contain abstract methods, while an abstract class can contain both abstract and concrete methods
  3. An interface can extend multiple interfaces, while an abstract class can only extend one abstract class.
  4.  An interface cannot contain any implementation code, while an abstract class can contain implementation code.
  5. An interface cannot contain any instance variables, while an abstract class can contain instance variables.

2. What is the purpose of garbage collection in Java, and when is it used?

  1. The purpose of garbage collection in Java is to reclaim memory that is no longer being used by an application. This is done by identifying and removing objects that are no longer referenced by the application.
  2. Garbage collection is used when an application has created objects that are no longer needed. The garbage collector will identify these objects and reclaim the memory they occupy. This helps to ensure that applications do not use up too much memory and can continue to run efficiently.

3. What is the difference between a constructor and a method?

  1. A constructor is a special type of method that is used to initialize an object when it is created, while a method is a function that is associated with an object and can be used to perform an action or calculate a value.
  2. A constructor is called automatically when an object is created, while a method must be explicitly called.
  3. A constructor does not have a return type, while a method must have a return type.
  4. A constructor can be overloaded, while a method cannot be overloaded.
  5. A constructor can be used to set the initial values of the object’s fields, while a method can be used to perform operations on the object’s fields.

4. What is the difference between a static and a non-static method?

  1. A static method is a method that can be called without creating an instance of the class, while a non-static method must be called on an instance of the class.
  2. A static method can access only static variables, while a non-static method can access both static and instance variables.
  3. A static method can be called directly from the class, while a non-static method must be called from an instance of the class.
  4. A static method can be overloaded, while a non-static method cannot be overloaded.
  5. A static method can be used to perform operations that do not require any data from the instance, while a non-static method can be used to perform operations that require data from the instance.

5. What is the difference between a public and a private method?

  1. Public methods are accessible from outside the class in which they are defined, while private methods are only accessible from within the class.
  2. Public methods can be called from any other class, while private methods can only be called from within the same class.
  3. Public methods can be overridden in subclasses, while private methods cannot be overridden.
  4. Public methods can be used to share data between classes, while private methods are used to keep data within the class.

6. What is the difference between a final, finally and finalize?

Final: A final keyword is used in Java to declare a constant variable, a method that cannot be overridden, or a class that cannot be extended.

Finally: Finally is a block of code is always executed after a try/catch block has been completed and before the code following the try/catch block. The finally block is used to make sure that critical code is always executed, even if an exception is thrown.

Finalize: Finalize is a method in Java that is called by the garbage collector when an object is about to be destroyed. The method allows the object to perform any cleanup operations before it is destroyed.

7. What is the difference between a String, a StringBuffer, and a StringBuilder?

String:

  1. Strings are immutable, meaning that they cannot be changed once they are created.
  2. Strings are thread-safe, meaning that they are safe to use in a multi-threaded environment.
  3. Strings are stored in the String pool, meaning that they are stored in a pool of memory and can be reused.

StringBuffer:

  1. StringBuffer is mutable, meaning that it can be changed once they are created.
  2. StringBuffer is thread-safe, meaning that they are safe to use in a multi-threaded environment.
  3. StringBuffer is stored in the heap, meaning that they are stored in a pool of memory and can be reused.

StringBuilder:

  • StringBuilder is mutable, meaning that it can be changed once they are created.
  • StringBuilder is not thread-safe, meaning

8. What is the difference between an Array and an ArrayList?

  1. An array is a fixed-size data structure, meaning that it cannot change its size once it is created. An ArrayList is a variable-size data structure, meaning that it can grow or shrink in size as needed.
  2. An array can store only one type of data, while an ArrayList can store multiple types of data.
  3. An array is faster than an ArrayList because it does not need to resize itself when new elements are added.
  4. An array is more memory efficient than an ArrayList because it does not need to store extra information about its size.
  5. An array is static, while an ArrayList is dynamic.

9. What is the difference between a HashMap and a Hashtable?

  1. HashMap is not synchronized whereas Hashtable is synchronized. 2. HashMap allows null key and null values whereas Hashtable doesn’t allow null key and null values.
  2. HashMap is unordered and Hashtable is ordered.
  3. HashMap is faster than Hashtable as it is not synchronized.
  4. HashMap is introduced in JDK 1.2 and Hashtable is introduced in JDK 1.0.

10. What is the difference between a Comparable and a Comparator?

  1. Comparable is an interface that is used to compare two objects of the same type. It uses the compareTo() method to compare two objects.
  2. Comparator is an interface that is used to compare two objects of different types. It uses the compare() method to compare two objects.
  3. Comparable is used to define the natural ordering of objects, while Comparator is used to define the custom ordering of objects.
  4. Comparable is implemented by the class whose objects are to be compared, while Comparator is implemented by a separate class.
  5. Comparable is a part of java.lang package, while Comparator is a part of java.util package.

Conclusion

Overall, the top 10 interview questions on core Java for 2-3 years of experience are designed to test a candidate’s knowledge and understanding of the language. They should be able to demonstrate their ability to write code, debug, and explain the fundamentals of Java. With the right preparation and practice, any candidate should be able to confidently answer these questions and demonstrate their proficiency in the language.

Recommended:

Leave A Reply

Your email address will not be published.