Revision: 36831
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 27, 2010 13:07 by evandrix
Initial Code
Java Interview Questions I Here are some java questions I have been asked in interviews recently: Explain what the following JVM args are used for: -Xss -Xmx -Xms -XX:PermSize -XX:NewSize -server vs -client What is the difference between the heap and the stack? Explain what is present on the heap and stack when the following method is called: public void f(int i){ Integer a = i ; double d = i ; } Answer: The primitive int 'i' is boxed into an Integer object and placed on the heap. So you have a "new Integer(i)" object present on the heap, and variables a, d and i on the stack (as well as the method f). [Further Reading] What are the main collections found in the Collections API? What is the difference between a List and a Set? How does a HashMap work? Given the following classes: Collection, Set, Map, List, ArrayList, Vector, LinkedList, Stack, HashMap, TreeMap, HashSet, TreeSet state which ones are interfaces, concrete classes and those that can be cast to Collection. What happens when the following code is executed? List<String> list = new ArrayList<String>() ; list.add("foo"); list.add("bar"); list.add("baz"); for(String s : list){ list.remove(s); } Answer: A ConcurrentModificationException is thrown. Given the following class: public class Person { private String name; public Person(String name){ this.name = name; } public boolean equals(Object o){ return name.equalsIgnoreCase(((Person)o).name); } public int hashCode(){ return 0; } public static void main(String[] args) { Set<Person> set = new HashSet<Person>(); set.add(new Person("David")); set.add(new Person("John")); set.add(new Person("DAVID")); set.add(new Person("Fred")); Map<Person, String> map = new HashMap<Person, String>(); map.put(new Person("David"),"David"); map.put(new Person("John"),"John"); map.put(new Person("DAVID"),"DAVID"); map.put(new Person("Fred"),"Fred"); } } i) What is the size of the set and map? What is the result of map.get(new Person("David"))? ii) Delete the equals method and repeat i) iii) Delete the hashCode method and repeat i) iv) Delete both the equals and hashCode methods and repeat i) Answers: i) 3, 3, DAVID ii) 4, 4, null iii) 4, 4, null iv) 4, 4, null Describe the different kinds of exceptions? What happens when a RuntimeException is thrown but not caught by anything? How are JNI Exceptions handled? What does the following code print? public static void main(String[] args) { try { try { throw new Exception(); } catch (Exception e) { System.out.println("FooI"); throw e; } finally { System.out.println("FinallyI"); } } catch (Exception e) { System.out.println("FooIII"); } finally { System.out.println("FinallyII"); } } Answer: FooI FinallyI FooIII FinallyII Java Interview Questions II Here are a few more java questions I have been asked in interviews recently: Garbage Collection: How does Garbage Collection work? Where does garbage collection start from? When do static fields get garbage collected? When does a thread get garbage collected? What is a potential problem with the following code? How can it be resolved? public void swap(Account a, Account b){ synchronized (a) { synchronized (b) { double d = a.getBalance(); double e = b.getBalance(); a.setBalance(e); b.setBalance(d); } } } Answer: A deadlock will occur if one thread calls swap(a,b) and another calls swap(b,a) at the same time. The easiest way to resolve this issue is to remove the two synchronized blocks and make the method synchronized instead. How many occurences of "NO" does the following program print? public class Main { public static void test(String s, String t) { if (s == t) { System.out.println("YES"); } else { System.out.println("NO"); } if (s.equals(t)) { System.out.println("YES"); } else { System.out.println("NO"); } } public static void main(String[] args) { String s = new String("HELLO"); String t = new String("HELLO"); test(s, t); // no, yes s = "HELLO"; t = "HELLO"; test(s, t); // yes, yes s = s + "!"; t = t + "!"; test(s, t); // no, yes s = "HELLO" + "!"; t = "HELLO" + "!"; test(s, t); // yes, yes } } What does the following code print? String s = "Welcome!"; s.substring(3, 7); System.out.println(s); Answer: Welcome! What are the possible outputs of the following program? What changes can you make to print out "HelloI HelloII HelloIII HelloIV" in order? public static void main(String[] args) { Thread t1 = new Thread(new Runnable(){ public void run() { System.out.println("HelloI"); System.out.println("HelloII"); } }); Thread t2 = new Thread(new Runnable(){ public void run() { System.out.println("HelloIII"); System.out.println("HelloIV"); } }); t1.start(); t2.start(); } Answer: Possible outputs are: HelloI HelloII HelloIII HelloIV HelloI HelloIII HelloIV HelloII HelloI HelloIII HelloII HelloIV HelloIII HelloIV HelloI HelloII HelloIII HelloI HelloII HelloIV HelloIII HelloI HelloIV HelloII To print them in order add t1.join() after t1.start(). You have a list of integers. Write an algorithm to find the maximum of the differences of each element with those ahead of it in the list. What does the following code print? public class A { private String s = "A1"; public A(){ dump(); s = "A2"; dump(); } public void dump(){ System.out.println(s); } public static void main(String[] args) { A a = new B(); } } class B extends A { private String s = "B1"; public B(){ dump(); s = "B2"; dump(); } public void dump(){ System.out.println(s); } } Answer: null null B1 B2 How would you increment a counter in a threadsafe way? How does AtomicInteger work? Does it use any internal synchronisation? What does the following code print? int i = -100; int a = ~i + 1; a >>= 2; System.out.println(a); Answer: 25 Write a unix command to find all files containing the string "ErrorMessage". Answer: find . -type f -exec grep -l "ErrorMessage" {} \;
Initial URL
Initial Description
Initial Title
Java Interview Questions
Initial Tags
java
Initial Language
Java