/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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){ 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? 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"); list.remove(s); } Answer: A ConcurrentModificationException is thrown. Given the following class: public class Person { this.name = name; } return name.equalsIgnoreCase(((Person)o).name); } public int hashCode(){ return 0; } 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? try { try { } throw e; } finally { } } } finally { } } 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 { if (s == t) { } else { } if (s.equals(t)) { } else { } } 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? s.substring(3, 7); 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 void run() { } }); public void run() { } }); 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 { public A(){ dump(); s = "A2"; dump(); } public void dump(){ } A a = new B(); } } class B extends A { public B(){ dump(); s = "B2"; dump(); } public void dump(){ } } 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; Answer: 25 Write a unix command to find all files containing the string "ErrorMessage". Answer: find . -type f -exec grep -l "ErrorMessage" {} \;