Table of Contents
- 1.  Can we override static method in java?
- 2.  Can you overload main method in java?
- 3. Can we override private methods in java?
- 4. What is the base class for all the classes?
- 5.  Can you list down some of important method from object class?
- 6.  Which two methods should you override while putting the custom object as Key in HashMap?
- 7. What is the difference between HashMap and HashSet in java?
- 8. Can we have abstract class without having any abstract method in it?
- 9. Have you heard about transient variable? When will you use it?
- 10. Can you call start method twice in java?
- 11. Do you know why String is immutable in java?
- 12. Do you know how to make a class immutable? Can you provide steps for it?
- 13. Can we have static method in the interface?
- 14. Can you declare constructor final?
- 15. What is the difference between StringBuffer and StringBuilder?
- 16. What is Java ClassPath?
- 17. You have a list of Custom objects? How can you sort them?
- 18. What is volatile in java?
- 19. What are two different ways to call garbage collector?
- 20. What is marker interface in java? Can you provide some examples of marker interface?
- 21. How many objects will be created below:
- 22. Can you differentiate between Checked Exception and Unchecked exception?
- 23. What is the difference between ArrayList and LinkedList? How will you decide which one you need to use?
- 24. What is difference between wait and [sleep in java](https://java2blog.com/java-thread-sleep-example/ “sleep in java”)?
- 25. You have started three threads from main threads.You need to make sure main thread complete last. How will you do it?
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:
1 2 3 4 |
String str1= new String("John"); String str2= new String("John"); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; class MyRunnable implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName() +" in run method"); } public static void main(String[] args) { MyRunnable runnable=new MyRunnable(); Thread t1=new Thread(runnable,"T1"); Thread t2=new Thread(runnable,"T2"); Thread t3=new Thread(runnable,"T3"); t1.start(); t2.start(); t3.start(); System.out.println("Main thread ends here"); } } |
When you run above program, you might get below output:
T2 in run method
T1 in run method
T3 in run method
With the help of join method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; class MyRunnable implements Runnable{ @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() +" in run method"); } public static void main(String[] args) { MyRunnable runnable=new MyRunnable(); Thread t1=new Thread(runnable,"T1"); Thread t2=new Thread(runnable,"T2"); Thread t3=new Thread(runnable,"T3"); t1.start(); t2.start(); t3.start(); try { t1.join(); t2.join(); t3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Main thread ends here"); } } |
When you run above program, you will get below output
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:
- Core java interview questions
- Java Collections interview questions
- OOPs interview questions in java
- Java Multithreading interview questions
- Exceptional handling interview questions in java
- Java Serialization interview questions in java
- Method overloading and overriding interview questions
- web services interview questions
- restful web services interview questions
- Data structure and algorithm Interview Questions
- Spring interview questions
- Hibernate interview questions
awesome java interviewe questions….
Awesome questions thank you very much
Very Good set of questions thank you.