In this article, I am sharing 30 core Java technical questions, from screening and phone round of interviews. In telephonic interviews, questions are short, fact based and Interviewer expects some keyword in answers. Accordingly, I have given very short answers to these question, only main points; just to make this as revision post. I am expecting every Java programmer to know answers of all these Java technical questions, if he has more than 4 to 5 years experience. it's only freshers, and junior developers who needs to do bit of research to understand topics well. I have tried to include all classical and hugely popular, frequently asked questions from different topics like String, multi-threading, collection, design pattern, object oriented design, garbage collection, generics and advanced Java concurrency questions. Since core Java interviewer's normally don't ask questions on JSP, Servlets and other JEE technologies, I have not included them into this list. Sole purpose of this list is to given freshers and less experienced developers an idea of what kind of core Java technical questions are asked on phone interviews. For curious ones, I have also included links for more detail answers and discussions.
1. Why String is immutable in Java? (Security, String pool implementation, see more here)
2. Can abstract class have constructor in Java? (Yes, detailed answer is here)
3. Which two methods is overridden by an Object, intended to be used as key in HashMap?
(equals and hashCode, read more)
4. Difference between wait and sleep in Java?(wait release lock, sleep keep it, for details see here)
5. Difference between List and Set in Java(List is ordered, allows duplicates and indexed, Set is unordered, don't allow duplicates, for more detailed answer, see this post)
6. How do you make a class Immutable in Java?(Make it final, final fields without setter, state is only set in constructor, no leak of internal reference, copy data for mutable members, read more)
7. Which data type you should used to represent currency in Java? (long or BigDecimal, if you say double, you need to convince them about rounding and how do you avoid floating point issues. for more detailed discussion, see this post)
8. When to use abstract class and interface in Java? (Use interface for type declaration, use abstract class if evolution is concern, for few more points, see this post)
9. Difference between Hashtable and HashMap in Java? (former is thread-safe and doesn't allow null, later is not thread-safe, former is also slow because of whole locking of Map, while HashMap is fast because of no locking, read more)
10. Difference between ArrayList and LinkedList in Java? (former is fast, backed by array, while later is backed by linked-list, queue, former also supports index based access at O(1), while later provides search at cost of O(n) time, for in-depth discussion, see here)
11. Difference between Overloading and Overriding in Java?( former take place at compile time, later happens at runtime, only virtual method can be overridden, static, final and private method can't be overridden in Java. for more in-depth discussion, see this post)
12. What kind of reference types are exists in Java? Differences?(Strong reference, Weak references, Soft reference and Phantom reference. Except strong, all other reference allows object to be garbage collected. For example, if an object hash only weak reference, than it's eligible for GC, if program needs space)
13. Difference between checked and unchecked exception in Java? (former is checked by compiler and it's handling is enforced by mandating try-catch or try-finally block. Later is not checked by compiler but can be caught using try-catch or try-finally block. For example, java.io.IOException, java.sql.SQLException are checked exception, while java.lang.NullPointerException and java.lang.ArrayIndexOutOfBoundsException are example of unchecked exception in Java, for better answer see here)
14. Does Java array is instance of Object? (Yes, and this is stark difference from array in C/C++, though it doesn't have any method, it has an attribute called length, which denotes size of array, see here to know more about array in Java)
15. Does List<Number> can hold Integers? (Yes)
16. Can we pass ArrayList<Number> to a method which accepts List<Number> in Java? (Yes)
17. Can we pass ArrayList<Integer> to a method which accepts List<Number>? (No) How to fix that? (use wildcards e.g. List<? extends Number> to know more about bounded and unbounded wildcards and other generics questions see this post)
18. What is volatile variable in Java? (guarantees happens before relationship, variable's value is read by main memory, for detail answer see here)
19. Difference between CountDownLatch and CyclicBarrier in Java? (former can not be reused once count reaches zero, while later can be reused even after barrier is broken, for in-depth discussion, see this post)
20. Does BlockingQueue is thread-safe? (Yes, take() and put() method of this class guarantees thread-safety, no need to externally synchronize this class for adding and retrieving objects, here is an example of this class to solve producer consumer problem in Java)
21. Why wait and notify method should be called in loop? (to prevent doing task, if condition is not true and thread is awake due to false alarms, checking conditions in loop ensures that processing is only done when business logic allows)
22. What is difference between "abc".equals(unknown string) and unknown.equals("abc")? (former is safe from NullPointerException,see here for more best practices and tips to avoid NPE in Java)
23. What is marker or tag interface in Java? (an interface, which presence means an instruction for JVM or compiler e.g. Serializable, from Java 5 onwards Annotation is better suited for this job, to learn more and answer in detail see this discussion)
24. Difference between Serializable and Externalizable interface in Java? (later provides more control over serialization process, and allow you to define custom binary format for your object, later is also suited for performance sensitive application, see here for more details)
25. Can Enum types implement interface in Java? (Yes)
26. Can Enum extend class in Java? (No, because Java allows a class to only extend one class and enum by default extends java.lang.Enum, see here for more Enum interview questions)
27. How to prevent your class from being subclassed? (Make it final or make constructor private)
28. Can we override Static method in Java? Compilation error? (No, it can only be hidden, no compilation error)
29. Which design pattern have you used recently? (give any example except Singleton and MVC e.g. Decorator, Strategy or Factory pattern)
30. Difference between StringBuffer and StringBuilder in Java? ( former is synchronized, and slow, while later is not synchronized and fast, for more details see this post)
That's all on this list of 30 core Java technical questions asked in phone interviews to freshers, junior Java developers and Java programmers up-to experience ranging from 1 to 2 years. As I said before, use this list for quick revision, especially if you are in hurry. Give short, to-the-point and specific answers, until Interviewer ask for more or insist to elaborate by asking follow-up questions. Since most of the phone interviews are done to screen candidates, mostly similar questions are asked to all candidates. By the way, always do a short research on company before your phone interview, you might find some of the questions previously asked on Google itself.
All the best Guys
1. Why String is immutable in Java? (Security, String pool implementation, see more here)
2. Can abstract class have constructor in Java? (Yes, detailed answer is here)
3. Which two methods is overridden by an Object, intended to be used as key in HashMap?
(equals and hashCode, read more)
4. Difference between wait and sleep in Java?(wait release lock, sleep keep it, for details see here)
5. Difference between List and Set in Java(List is ordered, allows duplicates and indexed, Set is unordered, don't allow duplicates, for more detailed answer, see this post)
6. How do you make a class Immutable in Java?(Make it final, final fields without setter, state is only set in constructor, no leak of internal reference, copy data for mutable members, read more)
7. Which data type you should used to represent currency in Java? (long or BigDecimal, if you say double, you need to convince them about rounding and how do you avoid floating point issues. for more detailed discussion, see this post)
8. When to use abstract class and interface in Java? (Use interface for type declaration, use abstract class if evolution is concern, for few more points, see this post)
9. Difference between Hashtable and HashMap in Java? (former is thread-safe and doesn't allow null, later is not thread-safe, former is also slow because of whole locking of Map, while HashMap is fast because of no locking, read more)
10. Difference between ArrayList and LinkedList in Java? (former is fast, backed by array, while later is backed by linked-list, queue, former also supports index based access at O(1), while later provides search at cost of O(n) time, for in-depth discussion, see here)
11. Difference between Overloading and Overriding in Java?( former take place at compile time, later happens at runtime, only virtual method can be overridden, static, final and private method can't be overridden in Java. for more in-depth discussion, see this post)
12. What kind of reference types are exists in Java? Differences?(Strong reference, Weak references, Soft reference and Phantom reference. Except strong, all other reference allows object to be garbage collected. For example, if an object hash only weak reference, than it's eligible for GC, if program needs space)
13. Difference between checked and unchecked exception in Java? (former is checked by compiler and it's handling is enforced by mandating try-catch or try-finally block. Later is not checked by compiler but can be caught using try-catch or try-finally block. For example, java.io.IOException, java.sql.SQLException are checked exception, while java.lang.NullPointerException and java.lang.ArrayIndexOutOfBoundsException are example of unchecked exception in Java, for better answer see here)
14. Does Java array is instance of Object? (Yes, and this is stark difference from array in C/C++, though it doesn't have any method, it has an attribute called length, which denotes size of array, see here to know more about array in Java)
15. Does List<Number> can hold Integers? (Yes)
16. Can we pass ArrayList<Number> to a method which accepts List<Number> in Java? (Yes)
17. Can we pass ArrayList<Integer> to a method which accepts List<Number>? (No) How to fix that? (use wildcards e.g. List<? extends Number> to know more about bounded and unbounded wildcards and other generics questions see this post)
18. What is volatile variable in Java? (guarantees happens before relationship, variable's value is read by main memory, for detail answer see here)
19. Difference between CountDownLatch and CyclicBarrier in Java? (former can not be reused once count reaches zero, while later can be reused even after barrier is broken, for in-depth discussion, see this post)
20. Does BlockingQueue is thread-safe? (Yes, take() and put() method of this class guarantees thread-safety, no need to externally synchronize this class for adding and retrieving objects, here is an example of this class to solve producer consumer problem in Java)
21. Why wait and notify method should be called in loop? (to prevent doing task, if condition is not true and thread is awake due to false alarms, checking conditions in loop ensures that processing is only done when business logic allows)
22. What is difference between "abc".equals(unknown string) and unknown.equals("abc")? (former is safe from NullPointerException,see here for more best practices and tips to avoid NPE in Java)
23. What is marker or tag interface in Java? (an interface, which presence means an instruction for JVM or compiler e.g. Serializable, from Java 5 onwards Annotation is better suited for this job, to learn more and answer in detail see this discussion)
24. Difference between Serializable and Externalizable interface in Java? (later provides more control over serialization process, and allow you to define custom binary format for your object, later is also suited for performance sensitive application, see here for more details)
25. Can Enum types implement interface in Java? (Yes)
26. Can Enum extend class in Java? (No, because Java allows a class to only extend one class and enum by default extends java.lang.Enum, see here for more Enum interview questions)
27. How to prevent your class from being subclassed? (Make it final or make constructor private)
28. Can we override Static method in Java? Compilation error? (No, it can only be hidden, no compilation error)
29. Which design pattern have you used recently? (give any example except Singleton and MVC e.g. Decorator, Strategy or Factory pattern)
30. Difference between StringBuffer and StringBuilder in Java? ( former is synchronized, and slow, while later is not synchronized and fast, for more details see this post)
All the best Guys
No comments:
Post a Comment