Time is changing and so is Java interviews. Gone
are the days, when knowing the difference
between String and StringBuffer can help you to
go till the second round of interview, questions
are becoming more advanced and interviewers are
asking more deep questions.
Since I like to explore interview questions, I have
got this huge list of questions with me, which
contains lots and lots of questions from different
topics. I have been preparing this MEGA list from
quite some time and now It's ready to share with
you guys. It contains interview questions not only
from classic topics like threads, collections,
equals and hashcode, sockets but also from NIO,
array, string, java 8 and many more.
Important Topics for Java Interviews
To give you an
idea, this list of Java interview questions includes
following topics:
1. Multithreading, concurrency and thread basics
2. Date type conversion and fundamentals
3. Garbage Collection
4. Java Collections Framework
5. Array
6. String
7. GOF Design Patterns
8. SOLID design principles
9. Abstract class and interface
10. Java basics e.g. equals() and hashcode
11. Generics and Enum
12. Java IO and NIO
13. Common Networking protocols
14. Data structure and algorithm in Java
15. Regular expressions
16. JVM internals
17. Java Best Practices
18. JDBC
19. Date, Time, and Calendar
20. XML Processing in Java
21. JUnit
22. Programming
Multithreading, Concurrency and Thread
basics Questions
1) Can we make array volatile in Java?
Ans : Yes, you can make an array volatile in
Java but only the reference which is pointing to
an array, not the whole array.
2) What are practical uses of volatile modifier?
Ans : One of the practical use of the volatile variable is
to make reading double and long atomic. Another
use of the volatile variable is to provide a memory
barrier, just like it is used in Disrupter framework.
3) What guarantee volatile variable provides?
Ans : volatile variables provide the guarantee about
ordering and visibility .
4) Which one would be easy to write?
synchronization code for 10 threads or 2 threads?
Ans : In terms of writing code, both will be of same
complexity because synchronization code is
independent of a number of threads. Choice of
synchronization though depends upon a number
of threads .
5) What is a thread local variable in Java?
Ans : Thread-local variables are variables confined to a
thread, its like thread's own copy which is not
shared between multiple threads. Java provides a
ThreadLocal class to support thread-local
variables. It's one of the many ways to achieve
thread-safety.
6) The difference between sleep and wait in
Java?
Ans : Though both are used to pause currently running
thread, sleep() is actually meant for short pause
because it doesn't release lock, while wait() is
meant for conditional wait and that's why it
release lock which can then be acquired by
another thread to change the condition on which
it is waiting.
Date types and Basic Java Interview Questions
1)What is the right data type to represent a
price in Java?
Ans : BigDecimal if memory is not a concern and
Performance is not critical, otherwise double with
predefined precision.
2) How do you convert bytes to String?
Ans : you can convert bytes to the string using string
constructor which accepts byte[], just make
sure that right character encoding otherwise
platform's default character encoding will be used
which may or may not be same.
3) Which class contains clone method?
Cloneable or Object?
Ans : java.lang.Cloneable is marker interface and
doesn't contain any method. clone method is
defined in the object class. It is also knowing that
clone() is a native method means it's
implemented in C or C++ or any other native
language.
4) Is ++ operator is thread-safe in Java?
Ans : No it's not a thread safe operator because its
involve multiple instructions like reading a value,
incriminating it and storing it back into memory
which can be overlapped between multiple
threads.
5) Difference between a = a + b and a += b ?
Ans : The += operator implicitly cast the result of
addition into the type of variable used to hold the
result. When you add two integral variable e.g.
variable of type byte, short, or int then they are
first promoted to int and them addition happens.
If result of addition is more than maximum value
of a then a + b will give compile time error but
a += b will be ok as shown below
byte a = 127;
byte b = 127;
b = a + b; // error : cannot convert
from int to byte
b + = a; // ok
JVM Internals and Garbage Collection
Interview Questions
1) What is the size of int in 64-bit JVM?
Ans : The size of an int variable is constant in Java, it's
always 32-bit irrespective of platform. Which
means the size of primitive int is same in both
32-bit and 64-bit Java virtual machine
2) What is the size of an int variable in 32-bit
and 64-bit JVM?
Ans : The size of int is same in both 32-bit and 64-bit
JVM, it's always 32 bits or 4 bytes.
3) How do you find if JVM is 32-bit or 64-bit
from Java Program?
Ans : You can find that by checking some system
properties like sun.arch.data.model or os.arch.
4) Can you guarantee the garbage collection
process?
Ans : No, you cannot guarantee the garbage collection,
though you can make a request using System.gc()
or Runtime.gc() method.
5) What is the difference between stack and
heap in Java?
Ans : Stack and heap are different memory areas in the
JVM and they are used for different purposes. The
stack is used to hold method frames and local
variables while objects are always allocated
memory from the heap. The stack is usually much
smaller than heap memory and also didn't shared
between multiple threads, but heap is shared
among all threads in JVM.
Basic Java concepts Interview Questions
1) What's the difference between "a == b" and
"a.equals(b)"?
Ans : The a = b does object reference matching if both a
and b are an object and only return true if both
are pointing to the same object in the heap space,
on the other hand, a.equals(b) is used for logical
mapping and its expected from an object to
override this method to provide logical equality.
2)Difference between final, finalize and
finally?
Ans : The final is a modifier which you can apply to
variable, methods and classes. If you make a
variable final it means its value cannot be
changed once initialized. finalize is a method,
which is called just before an object is a garbage
collected, giving it last chance to resurrect itself,
but the call to finalize is not guaranteed. finally is
a keyword which is used in exception handling
along with try and catch. the finally block is
always executed irrespective of whether an
exception is thrown from try block or not.
3) What is a compile time constant in Java?
Ans : public static final variables are also known as a
compile time constant, the public is optional
there. They are replaced with actual values at
compile time because compiler know their value
up-front and also knows that it cannot be
changed during run-time.
Java Collections Framework Interview Questions
1) Difference between poll() and remove()
method?
Ans : Both poll() and remove() take out the object from
the Queue but if poll() fails then it returns null but
if remove fails it throws Exception.
2) Difference between ArrayList and LinkedList
in Java?
Ans : The obvious difference between them is that
ArrrayList is backed by array data structure,
supprots random access and LinkedList is backed
by linked list data structure and doesn't
supprot random access. Accessing an element
with the index is O(1) in ArrayList but its O(n) in
LinkedList.
3) How do you print Array in Java?
Ans : You can print an array by using the
Arrays.toString() and Arrays.deepToString()
method. Since array doesn't implement toString()
by itself, just passing an array to
System.out.println() will not print its contents but
Arrays.toString() will print each element.
4) LinkedList in Java is doubly or singly linked
list?
Ans : It's a doubly linked list, you can check the code in
JDK. In Eclipse, you can use the shortcut , Ctrl + T
to directly open this class in Editor.
5) Which kind of tree is used to implement
TreeMap in Java?
Ans : A Red Black tree is used to implement TreeMap in
Java.
Recommended Books for Java Interviews
If you are looking for some goods to prepare for
your Java Interviews, You can take a look at
following books to cover both theory and coding
questions:
1. Java Programming Interview Exposed by
Markham
2.Cracking the Coding Interview: 150
Programming Questions and Solutions.
3. Programming Interviews Exposed: Secrets
to Landing Your Next Job.
are the days, when knowing the difference
between String and StringBuffer can help you to
go till the second round of interview, questions
are becoming more advanced and interviewers are
asking more deep questions.
Since I like to explore interview questions, I have
got this huge list of questions with me, which
contains lots and lots of questions from different
topics. I have been preparing this MEGA list from
quite some time and now It's ready to share with
you guys. It contains interview questions not only
from classic topics like threads, collections,
equals and hashcode, sockets but also from NIO,
array, string, java 8 and many more.
Important Topics for Java Interviews
To give you an
idea, this list of Java interview questions includes
following topics:
1. Multithreading, concurrency and thread basics
2. Date type conversion and fundamentals
3. Garbage Collection
4. Java Collections Framework
5. Array
6. String
7. GOF Design Patterns
8. SOLID design principles
9. Abstract class and interface
10. Java basics e.g. equals() and hashcode
11. Generics and Enum
12. Java IO and NIO
13. Common Networking protocols
14. Data structure and algorithm in Java
15. Regular expressions
16. JVM internals
17. Java Best Practices
18. JDBC
19. Date, Time, and Calendar
20. XML Processing in Java
21. JUnit
22. Programming
Multithreading, Concurrency and Thread
basics Questions
1) Can we make array volatile in Java?
Ans : Yes, you can make an array volatile in
Java but only the reference which is pointing to
an array, not the whole array.
2) What are practical uses of volatile modifier?
Ans : One of the practical use of the volatile variable is
to make reading double and long atomic. Another
use of the volatile variable is to provide a memory
barrier, just like it is used in Disrupter framework.
3) What guarantee volatile variable provides?
Ans : volatile variables provide the guarantee about
ordering and visibility .
4) Which one would be easy to write?
synchronization code for 10 threads or 2 threads?
Ans : In terms of writing code, both will be of same
complexity because synchronization code is
independent of a number of threads. Choice of
synchronization though depends upon a number
of threads .
5) What is a thread local variable in Java?
Ans : Thread-local variables are variables confined to a
thread, its like thread's own copy which is not
shared between multiple threads. Java provides a
ThreadLocal class to support thread-local
variables. It's one of the many ways to achieve
thread-safety.
6) The difference between sleep and wait in
Java?
Ans : Though both are used to pause currently running
thread, sleep() is actually meant for short pause
because it doesn't release lock, while wait() is
meant for conditional wait and that's why it
release lock which can then be acquired by
another thread to change the condition on which
it is waiting.
Date types and Basic Java Interview Questions
1)What is the right data type to represent a
price in Java?
Ans : BigDecimal if memory is not a concern and
Performance is not critical, otherwise double with
predefined precision.
2) How do you convert bytes to String?
Ans : you can convert bytes to the string using string
constructor which accepts byte[], just make
sure that right character encoding otherwise
platform's default character encoding will be used
which may or may not be same.
3) Which class contains clone method?
Cloneable or Object?
Ans : java.lang.Cloneable is marker interface and
doesn't contain any method. clone method is
defined in the object class. It is also knowing that
clone() is a native method means it's
implemented in C or C++ or any other native
language.
4) Is ++ operator is thread-safe in Java?
Ans : No it's not a thread safe operator because its
involve multiple instructions like reading a value,
incriminating it and storing it back into memory
which can be overlapped between multiple
threads.
5) Difference between a = a + b and a += b ?
Ans : The += operator implicitly cast the result of
addition into the type of variable used to hold the
result. When you add two integral variable e.g.
variable of type byte, short, or int then they are
first promoted to int and them addition happens.
If result of addition is more than maximum value
of a then a + b will give compile time error but
a += b will be ok as shown below
byte a = 127;
byte b = 127;
b = a + b; // error : cannot convert
from int to byte
b + = a; // ok
JVM Internals and Garbage Collection
Interview Questions
1) What is the size of int in 64-bit JVM?
Ans : The size of an int variable is constant in Java, it's
always 32-bit irrespective of platform. Which
means the size of primitive int is same in both
32-bit and 64-bit Java virtual machine
2) What is the size of an int variable in 32-bit
and 64-bit JVM?
Ans : The size of int is same in both 32-bit and 64-bit
JVM, it's always 32 bits or 4 bytes.
3) How do you find if JVM is 32-bit or 64-bit
from Java Program?
Ans : You can find that by checking some system
properties like sun.arch.data.model or os.arch.
4) Can you guarantee the garbage collection
process?
Ans : No, you cannot guarantee the garbage collection,
though you can make a request using System.gc()
or Runtime.gc() method.
5) What is the difference between stack and
heap in Java?
Ans : Stack and heap are different memory areas in the
JVM and they are used for different purposes. The
stack is used to hold method frames and local
variables while objects are always allocated
memory from the heap. The stack is usually much
smaller than heap memory and also didn't shared
between multiple threads, but heap is shared
among all threads in JVM.
Basic Java concepts Interview Questions
1) What's the difference between "a == b" and
"a.equals(b)"?
Ans : The a = b does object reference matching if both a
and b are an object and only return true if both
are pointing to the same object in the heap space,
on the other hand, a.equals(b) is used for logical
mapping and its expected from an object to
override this method to provide logical equality.
2)Difference between final, finalize and
finally?
Ans : The final is a modifier which you can apply to
variable, methods and classes. If you make a
variable final it means its value cannot be
changed once initialized. finalize is a method,
which is called just before an object is a garbage
collected, giving it last chance to resurrect itself,
but the call to finalize is not guaranteed. finally is
a keyword which is used in exception handling
along with try and catch. the finally block is
always executed irrespective of whether an
exception is thrown from try block or not.
3) What is a compile time constant in Java?
Ans : public static final variables are also known as a
compile time constant, the public is optional
there. They are replaced with actual values at
compile time because compiler know their value
up-front and also knows that it cannot be
changed during run-time.
Java Collections Framework Interview Questions
1) Difference between poll() and remove()
method?
Ans : Both poll() and remove() take out the object from
the Queue but if poll() fails then it returns null but
if remove fails it throws Exception.
2) Difference between ArrayList and LinkedList
in Java?
Ans : The obvious difference between them is that
ArrrayList is backed by array data structure,
supprots random access and LinkedList is backed
by linked list data structure and doesn't
supprot random access. Accessing an element
with the index is O(1) in ArrayList but its O(n) in
LinkedList.
3) How do you print Array in Java?
Ans : You can print an array by using the
Arrays.toString() and Arrays.deepToString()
method. Since array doesn't implement toString()
by itself, just passing an array to
System.out.println() will not print its contents but
Arrays.toString() will print each element.
4) LinkedList in Java is doubly or singly linked
list?
Ans : It's a doubly linked list, you can check the code in
JDK. In Eclipse, you can use the shortcut , Ctrl + T
to directly open this class in Editor.
5) Which kind of tree is used to implement
TreeMap in Java?
Ans : A Red Black tree is used to implement TreeMap in
Java.
Recommended Books for Java Interviews
If you are looking for some goods to prepare for
your Java Interviews, You can take a look at
following books to cover both theory and coding
questions:
1. Java Programming Interview Exposed by
Markham
2.Cracking the Coding Interview: 150
Programming Questions and Solutions.
3. Programming Interviews Exposed: Secrets
to Landing Your Next Job.
Tags:
Interview Questions