Top Java programs for interview preparation

Guess the output of below program

package com.arpit.java2blog;

 
import java.util.HashMap;
 
public class HashMapMain {
 
    public static void main(String[] args) {
        HashMap<Country,String> countryCapitalMap=new HashMap<>();
        Country indiaCountry= new Country("India",10000);
        countryCapitalMap.put(indiaCountry, "Delhi");
        indiaCountry.name="Dummy";
        System.out.println("Capital of India is: "+countryCapitalMap.get(indiaCountry));
    }
}
 
class Country
{
    String name;
    long population;
 
    Country(String name,long population)
    {
        this.name=name;
        this.population=population;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getPopulation() {
        return population;
    }
    public void setPopulation(long population) {
        this.population = population;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + (int) (population ^ (population >>> 32));
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Country other = (Country) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (population != other.population)
            return false;
        return true;
    }
}


Output is:


Capital of India is: null
We have put key as Country object here, so hashcode will be calculated and object will be inserted into the hashmap. Once we changed country name, hashcode will not return same value, so we won’t be able to get value from HashMap



- Guess the output of below program




package com.arpit.java2blog;
 
import java.util.HashSet;
 
public class Country {
public String name;
 
@Override
public int hashCode() {
  return 31;
}
 
@Override
public boolean equals(Object obj) {
  return true;
}
 
public static void main(String args[]) {
  Country countryOne = new Country();
  Country countryTwo = new Country();
  countryOne.name = "India";
  countryTwo.name = "Nepal";
  HashSet<Country> countrySet = new HashSet<>();
  countrySet.add(countryOne);
  countrySet.add(countryTwo);
  System.out.println(countrySet.size());
}
}


Output will be:

As equals method always return true and hashcode return constant as 31.So when you try to put countryTwo in HashSet, when it will check for equals method, it will always return true, so countryTwo won’t be added to HashSet




- Guess the output of below program



public class MethodOverloadingExample {
 
public void methodTest(Object object)
{
  System.out.println("Calling object method");
}
 
public void methodTest(String object)
{
  System.out.println("Calling String method");
}
 
public static void main(String args[])
{
  MethodOverloadingExample moe=new MethodOverloadingExample();
  moe.methodTest(null);
}
}


Output will be:

Calling String method
When we have two overloaded version of same method, JVM will always call most specific method.

Comments