info@meritcampus.com    +91-85006-22255
...
`

City Distances
What will be the output of the following program?
`import java.util.*;public class CityDistances {    static List<City> cities = new ArrayList<City>();    public static void main(String[] args) {        cities.add(new City("Delhi", 15, 15));        cities.add(new City("Mumbai", 5, 50));        cities.add(new City("Kolkata", 25, 50));        cities.add(new City("Chennai", 25, 80));        cities.add(new City("Bangalore", 15, 70));        printDistance("Delhi", "Mumbai");        printDistance("Delhi", "Hyderabad");        printDistance("Kolkata", "Chennai");        printDistance("Kolkata", "Bangalore");        printDistance("Hyderabad", "Bangalore");    }    private static void printDistance(String name1, String name2) {        City first = null, second = null;        for (City city : cities) {            if (city.equals(name1)) { first = city; }            if (city.equals(name2)) { second = city; }        }        if (first != null && second != null && first != second) {            int distance = (int) Math.sqrt((first.x - second.x) * (first.x - second.x) + (first.y - second.y) * (first.y - second.y));            System.out.print(first.name.charAt(0) + "->" + second.name.charAt(1) + "=" + distance + ",");        } else {            System.out.print("NA,");        }    }}class City {    int x; int y; String name;    public City(String name, int x, int y) {        this.x = x; this.y = y; this.name = name;    }}`

 A. D->M=14,NA,K->C=0,K->B=14,NA, B. D->M=36,NA,K->C=30,K->B=22,NA, C. D->u=36,NA,K->h=30,K->a=22,NA, D. Some other output or Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

B.is the answer. we are printing the distance b/w the cities.the first letters of cites by using charAt position and the distance by sqrt of diff of their distances.

Posted by Shabaaz Shaikz    2014-04-02 12:53:48

Ans D. Output is NA,NA,NA,NA, . city object is checked for equals to with string object i.e., name1 and name2 which returns false and hence every time only else block is executed.

Posted by Nikhil Sulibhavi    2014-04-02 12:54:45

Ans is B.. here we calculating the distances between the cities by using x and y values.. hyderabad is not there in the list so it vl be taken as null and prints NA

Posted by Om Prakash Om    2014-04-02 12:55:36

Answer is D. In the printDistance() method, we are iterating over the ArrayList of City objects using for-each loop. Then we are comparing each City object with String objects passed to the method i.e., name1 and name2. All classes in Java inherit from the Object class, directly or indirectly and the default implementation of equals() provided by Object class compares memory locations and only returns true if two reference variables are pointing to same memory location. So here .equals() always returns false which leads to City references city1 and city2 not getting initialized at all (they always point to null). So else block will get executed in every call to printDistance() from main. So the answer is NA,NA,NA,NA,NA,   If the comparision statements were like ---------> if ((city.name).equals(name1)) and if ((city.name).equals(name2)), then the answer would be (C).

Posted by Shaileshwar Sharma    2014-04-02 16:52:48

Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-03 04:35:06