Java Questions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Below you’ll find a list of Java interview questions and answers

I’ve gathered during my own job hunts. Before you continue,


please note that answers may not be 100% complete, up to
date, or optimized, but are in my experience sufficient for
interview situations.

OOP Questions
These questions apply not only to Java, but object oriented
languages in general.

1. Compare interfaces and abstract classes

Both are used to achieve abstraction. Neither can be


instantiated. An abstract class can have concrete
implementations, an interface cannot. An interface is like a
contract — a structure that classes implementing it must
adhere to. Abstract classes are used to hide internals and
expose relevant functionality — one doesn’t have to know how
an abstract class does something in order to use it.

2. Explain inheritance

Making one object the child of another gives the child all
properties and methods (with respect to access modifiers) of
its parent and ancestors. That is inheritance — it is used to
achieve code reusability and polymorphism. In Java, as in
most modern languages, an object can only have one parent —
Java does not support multiple inheritance. Inheritance
creates tight coupling between the parent and its children,
which is why dependency injection is often a preferred
alternative, as it allows related pieces of code to be decoupled.

3. Explain polymorphism

Polymorphism means an object can belong to two or more


types. It can be achieved
through inheritance and interfaces. For example,
a Cat object extending Mammal and implementing LandAnimal is an
instance of all three of those types. Method overloading and
overriding is used to achieve flexible behavior when
implementing a polymorphic class hierarchy.

4. Compare association, aggregation, and composition

These terms are used to describe relational structures in a


class hierarchy. Association describes how objects relate to/are
associated with each other (1–1, 1-N, N-1, N-M). It can be
achieved through aggregation and composition. Aggregation
describes “is-a” relationships — Each object can exist
independently. Composition describes “has-a” relationships
— Objects cannot exist independently.
5. How would you model an ER Diagram for a social media
website?

There are many ways to answer this one. A first approach


could look something like this:
User
* Id: Long
* Name: String

Post
* Id: Long
* Likes: Integer

Thread
* Id: Long
* Post: ForeignKey<Post>

Java Questions
6. What can you tell me about memory management and
garbage collection in Java?

Both are automatically handled by the JVM. The garbage


collector periodically collects variables without any references.
Programmers can tell the garbage collector to schedule
garbage collection with System.gc(), but it’s not guaranteed when
that will happen. The two most important memory areas in the
JVM are the stack and the heap. They are used for different
purposes. The stack is used to hold method frames and local
variables, and is not shared between threads. Objects are
always allocated memory from the heap, which
is shared between all threads in the JVM. The stack is usually
much smaller than heap memory.

7. What can you tell me about Generics in Java?

Generics can be used in conjunction with classes and methods.


They’re used to specify a single declaration for a set of
related methods, or a single class declaration for a set of
related types. Generics are checked at compile-time for type
safety. Two examples are ArrayList<T> and three classes
representing daily, monthly, and yearly charts that extend an
abstract Chart and can be specified with <? extends Chart>.

8. What can you tell me about Java Optionals?

They encapsulate optional values, and are used to make code


more readable, stable, and avoid having to deal
with null values, thus avoiding NullPointerExceptions.

9. Explain the difference between  stream()and  parallelStream().

A regular stream is always synchronized, whereas a parallel


stream can execute operations asynchronously across CPU
cores. Unless running a heavy operation on a lot of objects, one
should use regular streams, as parallel streams have a high
overhead for setting up multithreading.
10. What is  ClassLoader  in Java?

The part of the JVM that loads bytecodes for classes at


runtime.

11. When is it appropriate to use a transient variable in Java?

Use transient variables, when you want to make a


variable non-serializable in a class that implements
the Serializable interface.

12. Can private methods be overwritten in Java?

No, because private methods are not visible in the subclass.

13. What is the difference between lists and sets in Java?

They differ in how their items’ ordering and uniqueness.


Lists are ordered and allow duplicate values. Sets are
unordered and do not allow duplicate elements.

14. Which two methods do you have to override for an object


to be usable as a key in a hash map?

To be usable as a key in a hash map, an object needs to


be comparable and define a hash function. You have to
overwrite equals() and hashCode().
15. What’s the difference between method overloading and
overriding in Java?

Overriding happens in a subclass. Overloading happens in
the same class.

16. How do you prevent a class from being sub-classed in


Java?

You can either make the constructor of the class private, or


mark the class as final.

17. What’s the difference between  this and  super in Java?

refers to the current instance of an object. super refers to an


this 

instance of the parent/superclass.

18. Will  3*0.1 == 0.3 return true or false?

This will return false, because some floating point numbers


cannot be represented exactly.

19. What is the right data type to represent a price in Java?

if memory is not a concern and performance is not


BigDecimal 

critical, otherwise double with a predefined precision.


20. Why is String immutable in Java?

Because Java was designed on the assumption that strings will


be heavily used. Making it immutable allows for
some optimization around easily sharing the same string
between multiple clients.

21. What are some ways that you could sort a collection?

You could use an inherently sorted collection like TreeMap,


or Collections.sort(), or the Stream API.

22. Write a Java program for the Fizz Buzz problem.


Fizz buzz

23. Write an algorithm to check, if a string is a palindrome in


Java.
Palindromes

24. Write a Java program to check, if a number is even or


odd.
Even or Odd

25. Write a Java program with a memory overflow.


Naive Fibonacci

or
Endless Loop
26. Write a Java program to check, if a number is prime.
Primes

27. Implement a stack in Java.


Stacks

28. In a stack,  peek() is O(1). How would you achieve O(1)


lookup for  peek() in a linked list?

Linked lists typically keep a reference to the head node.


In peek()just return head.value.

29. Implement a queue in Java using a linked list.


Queues using Linked Lists

30. Implement a queue in Java using arrays.


Queues using Arrays

31. Implement a singly-linked list in Java.


Singly-linked List

32. in Java, how fast is direct lookup in a hash map


theoretically, and why is it often slower in reality?

Lookup in hash maps is supposed to be constant time, O(1),


as each element is mapped to a key, which is computed using
the key object’s hash code. However, if the hash function
returns the same result for two or more
inputs, collisions occur. In this case, each key is essentially
mapped to a linked list of N objects belonging to it, reducing
lookup speed to O(N).

33. Implement binary search in Java.


Arrays.binarySearch(sortedArray, key);

if you’re being sassy, or from scratch:


Binary search

34. Implement bubble sort in Java.


Bubble Sort

35. Given a string like  a**hf*kl9*, write a function that returns a


string with all asterisks appearing first.
Sort String

Below you’ll find more general technical and nontechnical


questions. How you respond to these depends entirely on you,
your experience, and the position you’re applying for.

General Technical Questions


36. How long have you been programming professionally?

37. Tell us the details of an interesting problem you worked


on. What made it interesting?
38. What is something you’re proud to have contributed to?

39. What is something that didn’t go so well at work / while


programming and how did you handle it / how would you
handle it in the future?

40. How do you feel about <insert


language/framework/library/tool from job posting>? What
made you apply?

41. Tell us about a time when you had to make a trade-off


between user experience and optimization / technical design?

42. What’s an example of a time, when you had make a


decision quickly? What were the reasons behind that
decision? Would you have done anything differently?

43. What is your favorite thing you’ve worked on recently?

44. What is the biggest technical challenge you have faced?

45. Describe a bug you fixed / a feature you implemented.

46. What are your favorite and least favorite (Java)


frameworks/libraries/tools, and why?
Nontechnical Questions
47. Tell us about yourself. What are your career goals and
past projects? Where do you see yourself in 2 / 5 / 10 years?

48. Where did you hear about this role?

49. Which parts of our Creed / Mission / Vision resonate the


most with you? Why us? What do you like about our
company?

50. Which of our products / projects would you be excited to


work on and why? Are there technologies you don’t want to
work with and why not?

51. How do you use our and / or our competitors’ products?


How would you improve on them?

52. What is your dream job? What’s your perfect work day
like?

53. How do you work with different teams/departments?

54. Do you have references?

55. Do you have any questions for us?


The last one is important. Here are some suggestions for
questions you may want to ask your interviewer:

 How are you financed? (especially important for


startups)

 What do you like about working here?

 How can I best prepare for this role before starting?

 Could you describe a typical work week?

 How big are teams?

 What would my immediate responsibilities be?

 Will there be opportunities to choose, what projects I


work on?

 What does the career path look like for this role?

 How does your company promote personal growth?

 Do you feel there are any skills currently lacking on


the team?

 What is the biggest change the company has gone


through in the last year?

 What’s the style of leadership?


 What is the rhythm of work here? Is there a
particular time of year, when it’s all hands on deck
and we’re pulling long hours, or is it fairly consistent
throughout the year?

 What type of background and experience are you


looking for in this position? What would your ideal
candidate be like?

 Is there anything that stands out to you that makes


you think I might not be the right fit for this
position?

 What is the timeline for making a decision on this


position? When should I get back in touch with you?

You might also like