-
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 aSet
? - 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 toCollection
. -
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: AConcurrentModificationException
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 ofmap.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
Thursday, June 17, 2010
Java Interview Questions I
Here are some java questions I have been asked in interviews recently:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.