Java interview questions for 5 years experience

Java interview questions for 5 years experience

In this post, we will see Java interview questions for 5 to 6 years experience.
When you have 5 years of experience as java developer, you need to have a good understanding of collections, multithreading concepts.

If you are looking for the below queries then this post will help you as well.

  • Java interview questions for 4 years experience
  • Java interview questions for 6 years experience
  • Java interview questions for 7 years experience

Here are some questions which are most asked for 5 years of experience java programmers. You might find some of the questions very easy but believe me most developers failed to answer these questions.

1. Guess the output of below program.

What will be the output?
A. In m2 B
B. Compile time error
C. Runtime error

2. Guess the output of below program.

What will be the output?
A. In m1 B
B. Compile time error
C. Runtime error

3. Guess the output of below program.

What will be the output?
A. In m1 B
B. Compile-time error
C. Runtime error

4. What will happen in case of below program?

There are two threads T1 and T2. T1 is accessing m1 method. Will T2 be able to access m2 method on the same instance at the same time?

5. What will happen in case of below program?

There are two threads T1 and T2. T1 is accessing m1 method. Will T2 be able to access m2 method on the same instance at the same time?

6. What will happen in case of below program?

There are two threads T1 and T2. T1 is accessing m1 method. Will T2 be able to access m2 method on the same instance at the same time?

7. Guess the output of below program.

Further reading: How HashSet works in java

8. Guess the output of below program.

Main class

9. How to decide young generation and old generation size for your application?

It depends on nature of application.
If you have lots of temporary objects then there will be lot of minor gc. You can provide arguments XX:NewRatio=1 to distribute 50% to young generation and 50% to old.
By default, NewRatio=2 hence young Generation is 1/3 of total heap.
Similarly, If you have too many long-lived objects, then you might need to increase the size of tenure space by putting high value of NewRatio.

10. What is garbage collection in java?

Garbage collection is the process of identifying used and unused objects on java heap and removing unused object from the heap.
A live object means an object is still being referred to some part of program. Unused object means object is not being referred by any part of program and is eligible for garbage collection.
Programmer does not have to do manual garbage collection like C or C++. Java takes care of

11. What are types of garbage collectors in java?

You can see a detailed answer over here.

12. What is difference between Collection.synchronizedMap(map) and ConcurrentHashMap?

When you make map thread safe by using Collection.synchronizedMap(map), it locks whole map object, but ConcurrentHashMap does not lock the whole map, it just locks part of it(Segment).
You can read more about ConcurrentHashMap over here.

13. What will happen when you run below code

14. Write a program to print odd even numbers using threads in sequence?

Here is the program to print odd and even numbers using threads in sequence.

15. Which design pattern you have used in your project?

You can name few design patterns such as Singleton, Observer etc. which you might have used in your project.

16. What is double level locking in singleton design pattern?

Double level locking in Singleton design pattern  is used to make it thread-safe.

Let’s say two threads(T1 and T2) checked for null and both reached at synchronized (Singleton.class). T1 gets the lock and create instance of Singleton and return. Now T2 enters in a synchronized block, as we have checked for null again, it will not create object again.

17. Write a program to implement producer-consumer problem using BlockingQueue?

You can find detailed answer over here

18. Have you worked on Java 8? Can you share major changes in Java 8?

If you have worked on Java 8, you can share major changes such as Stream, lambda expression, defaults method in interface etc.

19. Have you worked on Serialization? Can you tell difference between Serializable and Externalizable?

You can find detailed answer over here

20. How will you detect memory leak in your application?

There is no simple answer to this question. You can take thread dump via JVisualVM and do the analysis in eclipse memory analyzer tool.

You may also like:

That’s all about Java interview questions for 5 to 6 years experience.

Related Posts

  • Hibernate interview questions
    16 April

    Top 40+ Hibernate interview questions and answers

    Table of Contents1. What is ORM?2. What is Hibernate?3. What are advantages of Hibernate?4. Explain architecture of Hibernate?5. What are some core interfaces of hibernate?6. Explain brief about Session interface used in hibernate?7. Are session objects thread safe?8. Explain brief about SessionFactory object used in hibernate?9. What is Configuration class in hibernate?10. Explain brief about […]

  • Java Interview questions
    22 May

    Java interview questions

    Table of ContentsCore JavaCore java interview questionsCollections interview questionsMultithreading interview questionsJava 8 interview questionsOOPs interview questionsException Handling interview questionsString interview questionsSerialization interview questionsmethod overloading and method overriding interview questionsImmutable class interview questionsJava tricky interview questions Basic interview questions for fresher Interview questions for 5 to 7 years experiencedHow HashMap works in java? Difference between Interface […]

  • Solid principles in java
    09 November

    SOLID Principles in Java

    Table of ContentsWhat is the meaning of S.O.L.I.D?Single Responsibility Principle (SRP)Open Closed Principle Liskov Substitution Principle Interface Segregation PrincipleDependency Inversion PrincipleWrap Up In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym […]

  • Java interview questions for 2 years experience
    08 November

    Java interview questions for 2 years experience

    Table of Contents1.   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 […]

  • Java tricky interview questions
    11 September

    Java technical interview questions

    Table of Contents1 How HashMap works in java?2 Which methods you should implement for key Object in HashMap?3. Guess the output of below program.4. Guess the output of below code.5. What are differences between Comparator and Comparable?6. What is difference between Error and Exception?7. What is ClassLoader in java?8. What are types of ClassLoader in java?9. […]

  • Spring Boot Interview questions
    06 August

    Top 15 Spring Boot Interview questions

    Table of Contents1. What is Spring boot?2. Why did you use Spring boot in your application?3. Can you list advantages of Spring boot?4. What are disadvantages of Spring boot?5. How can you override default properties in Spring boot Project?5. How can you run Spring boot application on custom port?6. What is Spring boot starter and how […]

Comments

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.