Posted By

evandrix on 11/27/10


Tagged

java Interview


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

alexvezeau


Java Interview Questions


 / Published in: Java
 

  1. Java Interview Questions I
  2. Here are some java questions I have been asked in interviews recently:
  3. Explain what the following JVM args are used for:
  4. -Xss
  5. -Xmx
  6. -Xms
  7. -XX:PermSize
  8. -XX:NewSize
  9. -server vs -client
  10. 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:
  11. public void f(int i){
  12. Integer a = i ;
  13. double d = i ;
  14. }
  15. 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]
  16. What are the main collections found in the Collections API?
  17. What is the difference between a List and a Set?
  18. How does a HashMap work?
  19. 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.
  20. What happens when the following code is executed?
  21. List<String> list = new ArrayList<String>() ;
  22. list.add("foo");
  23. list.add("bar");
  24. list.add("baz");
  25.  
  26. for(String s : list){
  27. list.remove(s);
  28. }
  29. Answer: A ConcurrentModificationException is thrown.
  30. Given the following class:
  31. public class Person {
  32.  
  33. private String name;
  34.  
  35. public Person(String name){
  36. this.name = name;
  37. }
  38.  
  39. public boolean equals(Object o){
  40. return name.equalsIgnoreCase(((Person)o).name);
  41. }
  42.  
  43. public int hashCode(){
  44. return 0;
  45. }
  46.  
  47. public static void main(String[] args) {
  48. Set<Person> set = new HashSet<Person>();
  49. set.add(new Person("David"));
  50. set.add(new Person("John"));
  51. set.add(new Person("DAVID"));
  52. set.add(new Person("Fred"));
  53.  
  54. Map<Person, String> map = new HashMap<Person, String>();
  55. map.put(new Person("David"),"David");
  56. map.put(new Person("John"),"John");
  57. map.put(new Person("DAVID"),"DAVID");
  58. map.put(new Person("Fred"),"Fred");
  59. }
  60. }
  61. i) What is the size of the set and map? What is the result of map.get(new Person("David"))?
  62. ii) Delete the equals method and repeat i)
  63. iii) Delete the hashCode method and repeat i)
  64. iv) Delete both the equals and hashCode methods and repeat i)
  65. Answers:
  66. i) 3, 3, DAVID
  67. ii) 4, 4, null
  68. iii) 4, 4, null
  69. iv) 4, 4, null
  70.  
  71. Describe the different kinds of exceptions?
  72. What happens when a RuntimeException is thrown but not caught by anything?
  73. How are JNI Exceptions handled?
  74. What does the following code print?
  75. public static void main(String[] args) {
  76. try {
  77. try {
  78. throw new Exception();
  79. }
  80. catch (Exception e) {
  81. System.out.println("FooI");
  82. throw e;
  83. }
  84. finally {
  85. System.out.println("FinallyI");
  86. }
  87. }
  88. catch (Exception e) {
  89. System.out.println("FooIII");
  90. }
  91. finally {
  92. System.out.println("FinallyII");
  93. }
  94. }
  95. Answer: FooI FinallyI FooIII FinallyII
  96.  
  97. Java Interview Questions II
  98. Here are a few more java questions I have been asked in interviews recently:
  99. Garbage Collection:
  100. How does Garbage Collection work?
  101. Where does garbage collection start from?
  102. When do static fields get garbage collected?
  103. When does a thread get garbage collected?
  104. What is a potential problem with the following code? How can it be resolved?
  105. public void swap(Account a, Account b){
  106. synchronized (a) {
  107. synchronized (b) {
  108. double d = a.getBalance();
  109. double e = b.getBalance();
  110. a.setBalance(e);
  111. b.setBalance(d);
  112. }
  113. }
  114. }
  115. 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.
  116. How many occurences of "NO" does the following program print?
  117. public class Main {
  118.  
  119. public static void test(String s, String t) {
  120. if (s == t) {
  121. System.out.println("YES");
  122. }
  123. else {
  124. System.out.println("NO");
  125. }
  126. if (s.equals(t)) {
  127. System.out.println("YES");
  128. }
  129. else {
  130. System.out.println("NO");
  131. }
  132. }
  133.  
  134. public static void main(String[] args) {
  135. String s = new String("HELLO");
  136. String t = new String("HELLO");
  137. test(s, t); // no, yes
  138.  
  139. s = "HELLO";
  140. t = "HELLO";
  141. test(s, t); // yes, yes
  142.  
  143. s = s + "!";
  144. t = t + "!";
  145. test(s, t); // no, yes
  146.  
  147. s = "HELLO" + "!";
  148. t = "HELLO" + "!";
  149. test(s, t); // yes, yes
  150. }
  151. }
  152. What does the following code print?
  153. String s = "Welcome!";
  154. s.substring(3, 7);
  155. System.out.println(s);
  156. Answer: Welcome!
  157. What are the possible outputs of the following program? What changes can you make to print out "HelloI HelloII HelloIII HelloIV" in order?
  158. public static void main(String[] args) {
  159. Thread t1 = new Thread(new Runnable(){
  160. public void run() {
  161. System.out.println("HelloI");
  162. System.out.println("HelloII");
  163. }
  164. });
  165. Thread t2 = new Thread(new Runnable(){
  166. public void run() {
  167. System.out.println("HelloIII");
  168. System.out.println("HelloIV");
  169. }
  170. });
  171. t1.start();
  172. t2.start();
  173. }
  174. Answer: Possible outputs are:
  175. HelloI HelloII HelloIII HelloIV
  176. HelloI HelloIII HelloIV HelloII
  177. HelloI HelloIII HelloII HelloIV
  178. HelloIII HelloIV HelloI HelloII
  179. HelloIII HelloI HelloII HelloIV
  180. HelloIII HelloI HelloIV HelloII
  181. To print them in order add t1.join() after t1.start().
  182. 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.
  183. What does the following code print?
  184. public class A {
  185.  
  186. private String s = "A1";
  187.  
  188. public A(){
  189. dump();
  190. s = "A2";
  191. dump();
  192. }
  193.  
  194. public void dump(){
  195. System.out.println(s);
  196. }
  197.  
  198. public static void main(String[] args) {
  199. A a = new B();
  200. }
  201. }
  202.  
  203. class B extends A {
  204.  
  205. private String s = "B1";
  206.  
  207. public B(){
  208. dump();
  209. s = "B2";
  210. dump();
  211. }
  212.  
  213. public void dump(){
  214. System.out.println(s);
  215. }
  216. }
  217. Answer:
  218. null
  219. null
  220. B1
  221. B2
  222. How would you increment a counter in a threadsafe way?
  223. How does AtomicInteger work? Does it use any internal synchronisation?
  224. What does the following code print?
  225. int i = -100;
  226. int a = ~i + 1;
  227. a >>= 2;
  228. System.out.println(a);
  229. Answer: 25
  230. Write a unix command to find all files containing the string "ErrorMessage".
  231. Answer: find . -type f -exec grep -l "ErrorMessage" {} \;

Report this snippet  

You need to login to post a comment.