Hello guys and ladies, in this article, I am sharing 30 core Java technical questions, from screening and phone rounds of interviews. In telephonic interviews, questions are short, fact-based and Interviewer expects a to-the-point reply and some keywords in answers. Accordingly, I have given very short answers to all these questions, only the main points; just to make this a revision post and not the main source for preparation. For thorough preparation, my master list of 140+ Java questions is a better one to start with. I am also expecting every Java programmer to know answers to all these Java technical questions if he has more than 4 to 5 years of experience. It's only freshers and junior developers who need to do a 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 patterns, object-oriented concepts, garbage collection, generics, and advanced Java concurrency questions, but if you think those are not enough, you can see the links which are my dedicated post on each topic.
I have tried to include all classical and hugely popular, frequently asked questions from different topics like String, multi-threading, collection, design patterns, object-oriented concepts, garbage collection, generics, and advanced Java concurrency questions, but if you think those are not enough, you can see the links which are my dedicated post on each topic.
For example, on multi-threading one, you will find 50+ questions or Java thread concepts, concurrency, and multi-threading. Those are ideal for preparations, and this post is ideal for revision.
Since core Java interviewers normally don't ask questions on JSP, Servlets, and other JEE technologies, I have not included them in this list.
The sole purpose of this list is to give 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 to more detailed answers and discussions.
I have not included questions based upon recent Java changes like Java 8, Java 9, Java 10, or Java 17, like the introduction of the lambda expression, method reference, and now the var in Java 10 but you can find a lot of such questions on this list of Java and Spring interview courses and books.
That is a big list with more than 200+ questions from different topics, and if you have some time in hand, you can use that to prepare well for your next Java interview.
1. Why is String immutable in Java? (Security, String pool implementation, see more here)
2. Can abstract class have a constructor in Java? (Yes, the detailed answer is here)
3. Which two methods are overridden by an Object, intended to be used as a key in HashMap?
(equals and hashCode, read more)
4. What is the difference between wait and sleep in Java? (wait for release lock, sleep keeps it, for details see here)
5. Difference between List and Set in Java?
The list is ordered, allows duplicates and indexed, Set is unordered, don't allow duplicates, for a more detailed answer, see this post)
6. How do you make a class Immutable in Java?
(Make it final, final fields without a setter, the state is only set in the constructor, no leak of an internal reference, copy data for mutable members, read more)
7. Which data type you should use to represent currency in Java?
You should answer 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 an abstract class if evolution is a concern, for few more points, see this post)
9. Difference between Hashtable and HashMap in Java?
Another, common Java question, former is thread-safe and doesn't allow null, later is not thread-safe, former is also slow because of the whole locking of Map, while HashMap is fast because of no locking, read more)
10. What is the difference between ArrayList and LinkedList in Java?
former is fast, backed by an array while later is backed by linked-list, queue, former also supports index-based access at O(1), while later provides search at the cost of O(n) time, for in-depth discussion, see here)
By the way, if you are really serious about doing well on Java programming interviews and don't have enough time for thorough preparation, then you should take help from a book like Java Programming Interview Exposed, one of the best ways to prepare for core Java interviews.
It covers all important topics including core Java, data structure and algorithm, frameworks like Spring and Hibernate, JDBC, JUnit, Design patterns, Android, Scala, and advanced concurrency stuff for experienced programmers.
Anyway, let's move on to the next question.
11. What is the difference between Overloading and Overriding in Java?
Former take place at compile-time, later happens at runtime, the only virtual method can be overridden, the 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 exist in Java? Differences?
(Strong reference, Weak references, Soft reference, and Phantom reference. Except strong, all other reference allows the object to be garbage collected. For example, if an object hash only weak reference, then it's eligible for GC if the program needs space) See here to learn more about weak reference and soft references in Java.
13. Difference between checked and unchecked exceptions in Java?
(former is checked by the compiler, and its handling is enforced by mandating try-catch or try-finally block. Later is not checked by the compiler but can be caught using a try-catch or try-finally block. For example, java.io.IOException, java.sql.SQLException is checked exception, while java.lang.NullPointerException and java.lang.ArrayIndexOutOfBoundsException is an example of the unchecked exception in Java, for better answer see here)
14. Does Java array is an instance of Object?
(Yes, and this is a stark difference from the array in C/C++ though it doesn't have any method, it has an attribute called length, which denotes the size of the array, see here to know more about array in Java)
15. Does List<Number> can hold Integers?
Yes, because Integer is a subclass of Number and generics allows that,
16. Can we pass ArrayList<Number> to a method that accepts List<Number> in Java?
Yes, Again, this is a tricky question, and you need in-depth knowledge of Generics to answer this question. If you want to know more about Generics, I suggest you join the Complete Java Masterclass course on Udemy. One of the best resources to learn Java in-depth.
17. Can we pass ArrayList<Integer> to a method that 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 a volatile variable in Java?
Special keyword to indicate that a variable can be read/write by more than one thread at a time. It guarantees happens-before relationship, the variable's value is read into main memory, for detail answer see here)
19. What is the difference between CountDownLatch and CyclicBarrier in Java?
Both are concurrency utility classes introduced in Java 5 and can be used to wait for other threads to finish their job before starting themselves. The key difference between CountDownLatch and CyclicBarrier is that the former cannot be reused once the count reaches zero while later can be reused even after the barrier is broken, for in-depth discussion, see this post)
20. Does BlockingQueue is thread-safe in Java?
(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 the producer-consumer problem in Java)
21. Why wait and notify method should be called in a loop?
This can be really difficult to answer if you have never used wait-notify because it seems pretty logical that an if-else block can be used, but that would be wrong.
As Joshua Bloch mentioned in his classic Effective Java book, we should always call wai-notify in the loop to prevent doing the task, if a condition is not true and the thread is awake due to false alarms, checking conditions in the loop ensures that processing is only done when business logic allows.
If you haven't read that book yet, I highly recommend it.
22. What is the difference between "ABC".equals(unknown string) and unknown? equals("ABC")?
This is again a tricky question and common idiom to avoid NullPointerException in Java. former is safe from NullPointerException see here for more best practices and tips to avoid NPE in Java)
23. What is a 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. What is the difference between the Serializable and Externalizable interface in Java?
This is another popular Java question. The Externalizable interface provides more control over the serialization process, and allow you to define a custom binary format for your object, later is also suited for the performance-sensitive application, see here for more details)
25. Can Enum types implement the interface in Java? (Yes)
26. Can Enum extend a 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, for curious developers here are three technical ways to prevent your class from being subclassed.)
28. Can we override a static method in Java? Compilation error?
(No, it can only be hidden, no compilation error, see here for more details)
29. Which design pattern have you used recently?
You can answer this question based upon experience, but you should give any example except Singleton and MVC, like Decorator, Strategy, or Factory pattern to make a better impression. If you don't know about design patterns then you can check the "From 0 to 1: Design Patterns - 24 That Matter - In Java" course by Loony Corn, An ex-Google, and Stanford.
30. What is the difference between StringBuffer and StringBuilder in Java?
Another very common question, which is mostly asked, is beginners. The short answer is that the 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 a hurry. Give short, to-the-point, and specific answers, until Interviewer asks for more or insists on elaborating by asking follow-up questions.
Since most of the phone interviews are done to screen candidates, mostly similar questions are asked of all candidates. By the way, always do a short research on the company before your phone interview, you might find some of the questions previously asked on Google itself.
Other Java Interview Questions and Articles you may like
Thanks for reading this article so far. If you like these Java interview questions or have seen them on your telephonic round of interviews, then please share this post with your friends and colleagues on Facebook, Twitter, Email, etc. If you have any questions or feedback, please drop a note.
All the best Guys
P. S. - If you are preparing hard for your Java interviews and need a few free online courses for further preparation then you can also check out this Java 8 Interview Preparation course on Udemy. It's completely free and you just need a free Udemy account to join this course.
Since core Java interviewers normally don't ask questions on JSP, Servlets, and other JEE technologies, I have not included them in this list.
The sole purpose of this list is to give 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 to more detailed answers and discussions.
I have not included questions based upon recent Java changes like Java 8, Java 9, Java 10, or Java 17, like the introduction of the lambda expression, method reference, and now the var in Java 10 but you can find a lot of such questions on this list of Java and Spring interview courses and books.
That is a big list with more than 200+ questions from different topics, and if you have some time in hand, you can use that to prepare well for your next Java interview.
Java Phone Interview Questions Answers
Anyway, without any further ado, here is a list of 30 frequently asked Java interview questions from the telephonic round:1. Why is String immutable in Java? (Security, String pool implementation, see more here)
2. Can abstract class have a constructor in Java? (Yes, the detailed answer is here)
3. Which two methods are overridden by an Object, intended to be used as a key in HashMap?
(equals and hashCode, read more)
4. What is the difference between wait and sleep in Java? (wait for release lock, sleep keeps it, for details see here)
5. Difference between List and Set in Java?
The list is ordered, allows duplicates and indexed, Set is unordered, don't allow duplicates, for a more detailed answer, see this post)
6. How do you make a class Immutable in Java?
(Make it final, final fields without a setter, the state is only set in the constructor, no leak of an internal reference, copy data for mutable members, read more)
7. Which data type you should use to represent currency in Java?
You should answer 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 an abstract class if evolution is a concern, for few more points, see this post)
9. Difference between Hashtable and HashMap in Java?
Another, common Java question, former is thread-safe and doesn't allow null, later is not thread-safe, former is also slow because of the whole locking of Map, while HashMap is fast because of no locking, read more)
10. What is the difference between ArrayList and LinkedList in Java?
former is fast, backed by an array while later is backed by linked-list, queue, former also supports index-based access at O(1), while later provides search at the cost of O(n) time, for in-depth discussion, see here)
By the way, if you are really serious about doing well on Java programming interviews and don't have enough time for thorough preparation, then you should take help from a book like Java Programming Interview Exposed, one of the best ways to prepare for core Java interviews.
It covers all important topics including core Java, data structure and algorithm, frameworks like Spring and Hibernate, JDBC, JUnit, Design patterns, Android, Scala, and advanced concurrency stuff for experienced programmers.
Anyway, let's move on to the next question.
11. What is the difference between Overloading and Overriding in Java?
Former take place at compile-time, later happens at runtime, the only virtual method can be overridden, the 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 exist in Java? Differences?
(Strong reference, Weak references, Soft reference, and Phantom reference. Except strong, all other reference allows the object to be garbage collected. For example, if an object hash only weak reference, then it's eligible for GC if the program needs space) See here to learn more about weak reference and soft references in Java.
13. Difference between checked and unchecked exceptions in Java?
(former is checked by the compiler, and its handling is enforced by mandating try-catch or try-finally block. Later is not checked by the compiler but can be caught using a try-catch or try-finally block. For example, java.io.IOException, java.sql.SQLException is checked exception, while java.lang.NullPointerException and java.lang.ArrayIndexOutOfBoundsException is an example of the unchecked exception in Java, for better answer see here)
14. Does Java array is an instance of Object?
(Yes, and this is a stark difference from the array in C/C++ though it doesn't have any method, it has an attribute called length, which denotes the size of the array, see here to know more about array in Java)
15. Does List<Number> can hold Integers?
Yes, because Integer is a subclass of Number and generics allows that,
16. Can we pass ArrayList<Number> to a method that accepts List<Number> in Java?
Yes, Again, this is a tricky question, and you need in-depth knowledge of Generics to answer this question. If you want to know more about Generics, I suggest you join the Complete Java Masterclass course on Udemy. One of the best resources to learn Java in-depth.
17. Can we pass ArrayList<Integer> to a method that 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 a volatile variable in Java?
Special keyword to indicate that a variable can be read/write by more than one thread at a time. It guarantees happens-before relationship, the variable's value is read into main memory, for detail answer see here)
19. What is the difference between CountDownLatch and CyclicBarrier in Java?
Both are concurrency utility classes introduced in Java 5 and can be used to wait for other threads to finish their job before starting themselves. The key difference between CountDownLatch and CyclicBarrier is that the former cannot be reused once the count reaches zero while later can be reused even after the barrier is broken, for in-depth discussion, see this post)
20. Does BlockingQueue is thread-safe in Java?
(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 the producer-consumer problem in Java)
21. Why wait and notify method should be called in a loop?
This can be really difficult to answer if you have never used wait-notify because it seems pretty logical that an if-else block can be used, but that would be wrong.
As Joshua Bloch mentioned in his classic Effective Java book, we should always call wai-notify in the loop to prevent doing the task, if a condition is not true and the thread is awake due to false alarms, checking conditions in the loop ensures that processing is only done when business logic allows.
If you haven't read that book yet, I highly recommend it.
22. What is the difference between "ABC".equals(unknown string) and unknown? equals("ABC")?
This is again a tricky question and common idiom to avoid NullPointerException in Java. former is safe from NullPointerException see here for more best practices and tips to avoid NPE in Java)
23. What is a 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. What is the difference between the Serializable and Externalizable interface in Java?
This is another popular Java question. The Externalizable interface provides more control over the serialization process, and allow you to define a custom binary format for your object, later is also suited for the performance-sensitive application, see here for more details)
25. Can Enum types implement the interface in Java? (Yes)
26. Can Enum extend a 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, for curious developers here are three technical ways to prevent your class from being subclassed.)
28. Can we override a static method in Java? Compilation error?
(No, it can only be hidden, no compilation error, see here for more details)
29. Which design pattern have you used recently?
You can answer this question based upon experience, but you should give any example except Singleton and MVC, like Decorator, Strategy, or Factory pattern to make a better impression. If you don't know about design patterns then you can check the "From 0 to 1: Design Patterns - 24 That Matter - In Java" course by Loony Corn, An ex-Google, and Stanford.
30. What is the difference between StringBuffer and StringBuilder in Java?
Another very common question, which is mostly asked, is beginners. The short answer is that the 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 a hurry. Give short, to-the-point, and specific answers, until Interviewer asks for more or insists on elaborating by asking follow-up questions.
Since most of the phone interviews are done to screen candidates, mostly similar questions are asked of all candidates. By the way, always do a short research on the company before your phone interview, you might find some of the questions previously asked on Google itself.
Other Java Interview Questions and Articles you may like
- The Java Developer RoadMap (roadmap)
- 100+ Data Structure-based Coding Problems for interviews (questions)
- 15 Spring Boot Interview Questions for Java Programmers (questions)
- 10 Tips to become a better Java Developer (tips)
- Difference between CyclicBarrier and CountDownLatch in Java? (answer)
- Top 5 Books to Master Concurrency in Java (books)
- 50+ Thread Interview Questions for Beginners (questions)
- 25 Software Design Coding Problems (questions)
- 21 String Algorithms Coding Problems for interviews (questions)
- 20+ Binary tree-based coding problems for an interview (questions)
- Top 20 Spring MVC Interview Questions (questions)
- How to do inter-thread communication in Java using wait-notify? (answer)
- 10 Advanced books for Experienced Programmers (books)
- Top 5 skills to Crack Coding interviews (article)
- 10 Advanced Core Java Courses for Experienced Programmers (courses)
Thanks for reading this article so far. If you like these Java interview questions or have seen them on your telephonic round of interviews, then please share this post with your friends and colleagues on Facebook, Twitter, Email, etc. If you have any questions or feedback, please drop a note.
All the best Guys
P. S. - If you are preparing hard for your Java interviews and need a few free online courses for further preparation then you can also check out this Java 8 Interview Preparation course on Udemy. It's completely free and you just need a free Udemy account to join this course.
very very good yar
ReplyDelete"give any example except Singleton " ....What do you mean by this?..Can you eloborate why not answer singleton if asked about which design pattern you know?
ReplyDeleteAuthor mean to say that the Singleton and MVC patterns are most commonly used design patterns which comes by default with frameworks like Spring , Struts. So listing other design patterns would be added advantage.
ReplyDelete@Anonymous, Yes that's correct. Giving examples of other GOF patters like Visitor, Template or Factory are perceived better that candidate has good knowledge of OOP design patterns.
ReplyDeleteHere are some more good Java interview questions for telephonic round of interview
ReplyDeleteFor Core Java Developers, I would say to prepare well for DS and Algo, you can check some sample Data structure questions here, once you are good at that, just prepare some basic Java questions for telephonic round. Once you are done that you are ready, but if you want more confidence, you should check this Mega list of Java questions, which contains core Java questions including multi-threading, exception handling, collections, GC, design pattern and OOP questions from last 5 years of Java interviews.
ReplyDelete