Core Java interview questions

Java interview questions for 2 years experience

Table of Contents

Core In this post, we are going to see Java interview questions for experienced.

These are the most asked interview questions for freshers(0-3 years experienced). This question list will help you to crack java interview. I have already shared detailed answer over here before, you might find it helpful as well.

I would like to apologize for putting so many links in this article but it is better to understand answer in good depth,so I have put links to details answers over here.

1.   Can we override static method in java?

No, you cannot override static method in java. You can only hide them. You can read the detailed answer over here.

2.   Can you overload main method in java?

Yes, you can overload main method in java.You can find the detailed answer over here.

3. Can we override private methods in java?

You can not override private methods in java as it is visible to that class only.

4. What is the base class for all the classes?

java.lang.Object is base class for the objects.

5.   Can you list down some of important method from object class?

Important methods of object classes are:

  • hashcode : It returns hash value of the object
  • equals : It compares the object references
  • wait : It causes current thread to wait until notify or notifyAll is not called
  • notify : Wakes up single thread which is waiting for lock
  • notifyAll: Wakes up all threads which is waiting for lock
  • toString : Provides String representation of the object
  • clone : This method is used to clone the object
  • finalize: This method is called when object is being garbage collected.

6.   Which two methods should you override while putting the custom object as Key in HashMap?

You need to override hashcode and equals method in custom class while putting objects of custom class in HashMap.

7.  What is the difference between HashMap and HashSet in java?

You can find details answer over here.

8. Can we have abstract class without having any abstract method in it?

Yes, you can have abstract class without having any abstract method.

9. Have you heard about transient variable? When will you use it?

Transient variables are used Serialization. If you don’t want to make variable serializable, you can make it transient variable.

10. Can you call start method twice in java?

No, you can not call Start method twice. It will throw illegal state exception. You can read detailed answer over here.

11. Do you know why String is immutable in java?

You can fin the detailed answer over here.

12. Do you know how to make a class immutable? Can you provide steps for it?

You can find detailed answer over here.

13. Can we have static method in the interface?

Yes, we can have static method in the interface from Java 8.

14. Can you declare constructor final?

No, You can not declare constructor final.

15. What is the difference between StringBuffer and StringBuilder?

Parameter
StringBuffer
StringBuilder
Thread-safe
StringBuffer is thread safe. Two threads can not call methods of StringBuffer simultaneously.
StringBuilder is not thread safe, so two threads can call methods of StringBuilder simultaneously.
Performance
It is less performance efficient as it is thread-safe
It is more performance efficient as it is not thread-safe.

16. What is Java ClassPath?

ClassPath is environment variable which java virtual machine (JVM) uses to locate all classes which is used by the program.
For example: jre/lib/rt.jar has all java classes and you also need to include jar files or class file which is being used by program.

17. You have a list of Custom objects? How can you sort them?

You need to use Comparable or Comparator interface to sort list of custom objects.

18. What is volatile in java?

If you mark any variable volatile then this variable will be read from main memory rather than CPU cache so each thread will have updated value in the variable.

19. What are two different ways to call garbage collector?

System.gc() OR Runtime.getRuntime().gc().

20. What is marker interface in java? Can you provide some examples of marker interface?

Marker interfaces are those interfaces which do not have any method in it.

Examples of marker interfaces are : Serializable and Cloneable.

21. How many objects will be created below:

Three objects will be created here, two in heap memory and one in String constant pool.

22. Can you differentiate between Checked Exception and Unchecked exception?

You can find detailed answer over here.

23. What is the difference between ArrayList and LinkedList? How will you decide which one you need to use?

You can find details answer over here.

24. What is difference between wait and [sleep in java](https://java2blog.com/java-thread-sleep-example/ “sleep in java”)?

You can find details answer over here.

25. You have started three threads from main threads.You need to make sure main thread complete last. How will you do it?

You can use thread’s join method to achieve this scenario.
Without using join method:

When you run above program, you might get below output:

Main thread ends here
T2 in run method
T1 in run method
T3 in run method

With the help of join method:

When you run above program, you will get below output

T2 in run method
T3 in run method
T1 in run method
Main thread ends here

As you can see, main thread complete last in this scenario.
You can also use CountDownLatch and CyclicBarrier too to achieve same scenario.

That’s all about java interview questions.
You may also like:

Was this post helpful?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *