Java interview questions for 4 to 7 years experience
Tags: java, programming, interview
Published: 2019-04-22 17:03:47

---
title: Java interview questions for 4 to 7 years experience
published: true
description: 
canonical_url: https://java2blog.com/java-interview-questions-for-5-years-experience/
tags: #java #programming #interview

---

In this post, we will see <a href="https://java2blog.com/java-interview-questions/" target="_blank" rel="noopener">Java interview questions</a> for 5 to 6 years experience.
When you have 5 years of experience as <a href="https://java2blog.com/core-java-tutorial-for-beginners-experienced/" target="_blank" rel="noopener">java</a> developer, you need to have good understanding of <a href="https://www.java2blog.com/java-collections-interview-questions/">collections</a>, <a href="https://www.java2blog.com/java-multithreading-interview-questions-and-answers/">multithreading</a> concepts.

<img src="https://thepracticaldev.s3.amazonaws.com/i/6cbw1ztv8nunbra4v1ek.png">

If you are looking for below queries then this post will help you as well.
<ul>
 	<li>Java interview questions for 4 years experience</li>
 	<li>Java interview questions for 6 years experience</li>
 	<li>Java interview questions for 7 years experience</li>
</ul>
Here are some questions which are most asked for 5 years experience java programmers. You might find some of the questions very easy but believe me most developers failed to answer these questions.
<h3><span style="color: #800000;">1. Guess the output of below program.</span></h3>
<pre class="java" name="code">package org.arpit.java2blog;
class A
{
	void m1()
	{
		System.out.println("In m1 A");
	}
}
class B extends A
{
	void m1()
	{
		System.out.println("In m1 B");
	}
	void m2()
	{
		System.out.println("In m2 B");
	}
}
public class Test {

	public static void main(String[] args) {
		A a=new B();
		a.m2();
	}
}
</pre>
<strong>What will be the output?</strong>
<strong>A. In m2 B</strong>
<strong>B. Compile time error</strong>
<strong>C. Runtime error</strong>
<div id="toc_container">
B. Compile time error
There will be compile time error.Even though we are assigning B's object to A's reference we can call only methods which are in A from A's reference.
</div>
<h3><span style="color: #800000;">2. Guess the output of below program.</span></h3>
<pre class="java" name="code">package org.arpit.java2blog;

class A
{
	void m1() throws ArrayIndexOutOfBoundsException
	{
		System.out.println("In m1 A");
	}
}
class B extends A
{
	void m1() throws IndexOutOfBoundsException
	{
		System.out.println("In m1 B");
	}
}
public class Test {

	public static void main(String[] args) {
		A a=new B();
		a.m1();
	}
}
</pre>
<strong>What will be the output?</strong>
<strong>A. In m1 B</strong>
<strong>B. Compile time error</strong>
<strong>C. Runtime error</strong>
<div id="toc_container">
A. In m1 B
This will work fine as ArrayIndexOutOfBoundsException and IndexOutOfBoundsException are Runtime exceptions and there is no rule for runtime exceptions while method overriding.
</div>
<h3><span style="color: #800000;">3. Guess the output of below program.</span></h3>
<pre class="java" name="code">package org.arpit.java2blog;

import java.io.IOException;

class A
{
	void m1() throws IOException
	{
		System.out.println("In m1 A");
	}
}
class B extends A
{
	void m1() throws Exception
	{
		System.out.println("In m1 B");
	}
}
public class Test {

	public static void main(String[] args) {
		A a=new B();
		try {
			a.m1();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
</pre>
<strong>What will be the output?</strong>
<strong>A. In m1 B</strong>
<strong>B. Compile time error</strong>
<strong>C. Runtime error</strong>
<div id="toc_container">
B. Compile time error
As IOException and Exception are checked exception, so you can not broaden the scope of Exception while method overriding.
</div>
<h3><span style="color: #800000;">4. What will happen in case of below program?</span></h3>
<pre class="java" name="code">class A
{
	synchronized void m1()
	{
		System.out.println("In m1 A");
	}
	void m2()
	{
		System.out.println("In m2 A");
	}
}
</pre>
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?
<div id="toc_container">
Yes, T2 will be able to access m2 as it does not require lock to access m2 method./div>
<h3><span style="color: #800000;">5. What will happen in case of below program?</span></h3>
<pre class="java" name="code">class A
{
	synchronized void m1()
	{
		System.out.println("In m1 A");
	}
	synchronized void m2()
	{
		System.out.println("In m2 A");
	}
}
</pre>
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?
<div id="toc_container">
No, T2 will not be able to access m2 as it requires lock to access m2 method which is already taken by T1 thread.
</div>
<h3><span style="color: #800000;">6. What will happen in case of below program?</span></h3>
<pre class="java" name="code">class A
{
	synchronized static void m1()
	{
		System.out.println("In m1 A");
	}
	synchronized void m2()
	{
		System.out.println("In m2 A");
	}
}
</pre>
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?
<div id="toc_container">
Yes, T2 will be able to access m2 as it requires object level lock to access m2 method and T1 thread has taken class level lock.You can read more about <a href="https://java2blog.com/object-level-locking-vs-class-level-locking-java/" target="_blank" rel="noopener">Object level lock vs Class level lock</a>.
</div>
<h3><span style="color: #800000;">7. Guess the output of below program.</span></h3>
<pre class="java" name="code">package org.arpit.java2blog;

import java.util.HashSet;

public class Customer {

	String name;
	int age;
	
	Customer(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	public static void main(String[] args) {
		
		Customer c1= new Customer("John",20);
		Customer c2= new Customer("John",20);
		
		HashSet&lt;Customer&gt; customerSet=new HashSet&lt;&gt;();
		customerSet.add(c1);
		customerSet.add(c2);
		System.out.println(customerSet.size());
	}

	// getters and setters
}
</pre>
<div id="toc_container">
Output will be 2 as we did not implement hashcode and equals method in Customer class.</div>
<h3><span style="color: #800000;">8. Guess the output of below program.</span></h3>
<pre class="java" name="code">package org.arpit.java2blog;

public class Employee {
	String name;
	int age;
	
	public Employee(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
}
</pre>
Main class
<pre class="java" name="code">package org.arpit.java2blog;

import java.util.HashMap;
import java.util.Map;

public class HashMapMain {

	public static void main(String[] args) {
		Employee emp1=new Employee("Martin",27);
		Map&lt;Employee,String&gt; hm=new HashMap&lt;Employee,String&gt;();
		hm.put(emp1, "Verified");
		emp1.setName("John");
		System.out.println(hm.get(emp1));
	}
}
</pre>
<div id="toc_container">[showhide type="question8" more_text="Show Answer" less_text="Hide Answers"]
<strong>Output will be null.</strong>
We have implemented Employee's hashcode and equals method using name and age attributes, so when you put emp1 as key in hashmap, it will use hashcode and equals method and will be put in HashMap.
After putting emp1 in HashMop, we have changed name of the employee, so when you will try to retrieve element from HashMap using hm.get(emp1), you won't be able to get object which we have put earlier and it will return null.
[/showhide]</div>
<h3><span style="color: #800000;">9. How to decide young generation and old generation size for your application?</span></h3>
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 size of tenure space by putting high value of NewRatio.
<h3><span style="color: #800000;">10. What is garbage collection in java?</span></h3>
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 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
<h3><span style="color: #800000;">11. What are types of garbage collectors in java?</span></h3>
You can see <a href="https://java2blog.com/garbage-collection-java/" target="_blank">detailed answer over here.</a>
<h3><span style="color: #800000;">12. What is difference between Collection.synchronizedMap(map) and ConcurrentHashMap?</span></h3>
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 <a href="https://www.java2blog.com/concurrenthashmap-in-java/">ConcurrentHashMap over here</a>.
<h3><span style="color: #800000;">13. What will happen when you run below code</span></h3>
<pre class="java" name="code">package org.arpit.java2blog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class IterateMapMain {

	public static void main(String args[])
	{
		// HashMap with Country as key and capital as value
		HashMap&lt;String,String&gt; map=new HashMap&lt;String,String&gt;();
		map.put("India","Delhi");
		map.put("Japan","Tokyo");
		map.put("France","Paris");
		map.put("Russia","Moscow");

		
		// Iterating java iterator
		System.out.println("Iterating java Iterator");
		Iterator&lt;String&gt; countryKeySetIterator=map.keySet().iterator();
		while(countryKeySetIterator.hasNext()){
			String countryKey=countryKeySetIterator.next();
			map.put("Nepal", "KathMandu");
			System.out.println(countryKey);

		}
		System.out.println("-----------------------------");
	}

}
</pre>
<div id="toc_container">
<b>You will get below output</b>

Iterating java IteratorException in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1489)
at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1512)
at org.arpit.java2blog.IterateMapMain.main(IterateMapMain.java:24)
Japan

Whenever you try to change count of hashmap while iterating, it will throw java.util.ConcurrentModificationException because we call iterator.next,it checks for modCount and initial count, if it does not match it will throw ConcurrentModificationException.
</div>
<h3><span style="color: #800000;">14. Write a program to print odd even numbers using threads in sequence?</span></h3>
Here is the program to <a href="https://www.java2blog.com/print-even-odd-numbers-threads-java/">print odd and even numbers using threads in sequence</a>.
<h3><span style="color: #800000;">15. Which design pattern you have used in your project?</span></h3>
You can name few design patterns such as Singleton, Observer etc. which you might have used in your project.
<h3><span style="color: #800000;">16. What is double level locking in singleton design pattern?</span></h3>
Double level locking in single design pattern is used to make it thread-safe.
<pre class="java " name="code">public static Singleton getInstance()
	{ 
		if (_instance == null) { // Single Checked 
			synchronized (Singleton.class) 
			{ 
				if (_instance == null)// Double checked 

				{ 
					_instance = new Singleton(); 
				}
			} 
		} return _instance; 
	}
</pre>
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 synchronized block, as we have checked for null again,it will not create object again.
<h3><span style="color: #800000;">17. Write a program to implement producer-consumer problem using BlockingQueue?</span></h3>
You can find <a href="https://www.java2blog.com/blockingqueue-in-java/" target="_blank" rel="noopener">detailed answer over here</a>
<h3><span style="color: #800000;">18. Have you worked on Java 8? Can you share major changes in Java 8?</span></h3>
If you have worked on Java 8, you can share major changes such Stream, lambada expression, defaults method in interface etc.
<h3><span style="color: #800000;">19. Have you worked on Serialization? Can you tell difference between Serializable and Externalizable?</span></h3>
You can find <a href="https://www.java2blog.com/difference-between-serializable-and-externalizable-in-java/" target="_blank" rel="noopener">detailed answer over here</a>
<h3><span style="color: #800000;">20. How will you detect memory leak in your application?</span></h3>
There is no simple answer to this question. You can take thread dump via JVisualVM and do the analysis in <a href="https://www.eclipse.org/mat/" target="_blank" rel="nofollow noopener">eclipse memory analyzer tool</a>.
That's all about <a href="https://java2blog.com/java-interview-questions/" target="_blank" rel="noopener">Java interview questions for 5 to 6 years experience</a>.
You may also like:
<ul>
 	<li><a href="http://www.java2blog.com/core-java-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Core java interview questions</a></li>
 	<li><a href="http://www.java2blog.com/java-collections-interview-questions.html" target="_blank" rel="noopener noreferrer">Java Collections interview questions</a></li>
 	<li><a href="http://www.java2blog.com/java-string-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Java String interview questions</a></li>
 	<li><a href="http://www.java2blog.com/oops-interview-questions-answers-java.html" target="_blank" rel="noopener noreferrer">OOPs interview questions in java</a></li>
 	<li><a href="http://www.java2blog.com/java-multithreading-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Java Multithreading interview questions</a></li>
 	<li><a href="http://www.java2blog.com/java-exception-handling-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Exceptional handling interview questions in java</a></li>
 	<li><a href="http://www.java2blog.com/java-serialization-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Java Serialization interview questions in java</a></li>
 	<li><a href="http://www.java2blog.com/web-services-interview-questions.html" target="_blank" rel="noopener noreferrer">web services interview questions</a></li>
 	<li><a href="http://www.java2blog.com/restful-web-services-interview-questions.html" target="_blank" rel="noopener noreferrer">restful web services interview questions</a></li>
 	<li><a href="http://www.java2blog.com/spring-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Spring interview questions</a></li>
<li><a href="https://java2blog.com/spring-boot-interview-questions/" target="_blank" rel="noopener noreferrer">Spring boot interview questions</a></li>
 	<li><a href="http://www.java2blog.com/hibernate-interview-questions-and-answers.html" target="_blank" rel="noopener noreferrer">Hibernate interview questions</a></li>
<li><a href="https://java2blog.com/no-compiler-provided-environment-perhaps-running-jre-rather-jdk/" target="_blank" rel="noopener noreferrer">No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?</a></li>
<li><a href="https://java2blog.com/could-not-reserve-enough-space-for-object-heap/" target="_blank" rel="noopener noreferrer">Could not reserve enough space for object heap in java</a></li>
</ul>